Skip to content

MATLAB - Void Function

In many programming languages, a “void function” is one that does not return a value. In MATLAB, every function can return output arguments, but they don’t have to. A function that is designed not to return a value, but instead to perform an action, is called a function with side effects. We can think of these as “procedure-style” or “void-like” functions.

The primary purpose of such functions is to interact with the environment outside of the function itself, such as:

  • Displaying Information: Printing text or results to the Command Window.
  • Creating Visualizations: Generating plots or figures.
  • File I/O: Saving data to a file or reading from one.
  • Modifying State: Changing the properties of a handle object (an advanced topic).

The syntax is the same as a regular function, but you simply omit the output arguments and the []= part of the function definition.

function functionName(inputArg1, inputArg2, ...)
% This function performs an action but does not return a value.
% -- Function body goes here --
end % No output arguments are defined

Best Practice: The function’s name should be a verb that clearly describes its action, like plotData, saveResults, or printSummary.

Example 1: A Function to Display a Formatted Message

Section titled “Example 1: A Function to Display a Formatted Message”

This function takes a name and a score, and prints a formatted message. It returns no value.

% Save this code in a file named 'printScore.m'
function printScore(playerName, score)
% Displays a player's score in a formatted way.
fprintf('Update: Player ''%s'' has reached a score of %d.\n', playerName, score);
end

You would call this function from the Command Window or another script:

printScore('Alex', 1500)
Update: Player 'Alex' has reached a score of 1500.

A very common use case is to encapsulate plotting logic into a reusable function.

% Save this code in a file named 'plotMyData.m'
function plotMyData(x_data, y_data, plotTitle)
% Creates a customized line plot for the given data.
figure; % Create a new figure window
plot(x_data, y_data, '-o', 'LineWidth', 1.5);
title(plotTitle);
xlabel('Measurement Index');
ylabel('Value');
grid on;
legend('My Data');
end

To use it:

time = 1:10;
sensorReadings = [2.1, 3.4, 4.1, 3.9, 5.0, 5.2, 6.1, 5.8, 6.5, 7.2];
plotMyData(time, sensorReadings, 'Sensor Readings Over Time');

Executing this code does not return any variable to the workspace. Instead, its side effect is to produce a new figure window containing a line plot of the sensorReadings data. The plot has a title, axis labels, a grid, and a legend, as defined within the function.

Functions That Return vs. Functions That Act

Section titled “Functions That Return vs. Functions That Act”

It’s crucial to distinguish between a function that calculates a value and one that acts. Consider the difference:

% Function that CALCULATES (Good Practice)
function s = calculateSum(a, b)
s = a + b;
end
% Function that ACTS (Procedure-Style)
function displaySum(a, b)
sumResult = a + b;
fprintf('The sum is: %g\n', sumResult);
end

Key Difference: calculateSum gives the result back to the caller, who can then decide to display it, save it, or use it in another calculation. displaySum is less flexible because it can only display the result to the console. Rule of thumb: If the primary purpose is to compute a value, return it. If the primary purpose is to perform an action like plotting or saving, use a procedure-style function.

  • Pass-by-Value: By default, MATLAB passes arguments by value. This means a function cannot change the caller’s input variables unless they are handle objects. This prevents unexpected side effects.
  • Modularity and Reusability: Encapsulating actions like plotting or saving into functions makes your main script cleaner and the actions reusable. For example, plotMyData could be called multiple times with different datasets.