12.1. Declaring functions#
Functions in MATLAB are declared using the function
keyword.
function function_name()
% commands
end
Where function
is the name which is used be the program to refer to the function. A function name must begin with a character or underscore and not a number. Also care must be taken not to use the same name as a previously defined function (e.g., if you define a new function called max
then you won’t be able to use MATLAB’s built-in max
function).
Important
Functions in MATLAB must be declared at the end of the file and no non-function declaration commands can follow a function. Functions declaration may be contained in a file only containing function declarations where the filename must match the first function in the file.
Create a new MATLAB file called M5_Functions.m and save it to your OneDrive folder. Lets define a function that prints hello world
to the command window. Enter the following code into your file.
% 12. Functions
clear % Clear all variables
clc % Clear command window
% Declaring functions
hello()
% ------------------------------------------------------------------------
function hello()
fprintf("hello world \n")
end
Here we have declared a function called hello
that has no input argument (there is nothing between the brackets). The function prints the hello world
string. We have also made a call to this function. Run your program and you should see the following added to the command window.
hello world
12.1.1. Local variables#
Any variables defined within a function are known as local variables because they can only be accessed from commands within the function. It can be useful to think of a function as a black box in that any variables which are defined within a function cannot be used outside of the function unless they have been returned by the function.
To demonstrate local variables, declare a new function by entering the following code at the end of the file.
function print_name()
name = "Ellie Williams";
fprintf("%s\n", name)
end
and make a call to this function by entering the following code before the function declarations.
fprintf("\n")
print_name()
Run your program and you should see the following added to the command window.
Ellie Williams
Here the variable name
is declared inside the function and outputted to the command window using the fprintf()
command. Lets try to access name
outside of the function. To demonstrate this, enter the following code into your program.
fprintf("%s\n", name)
If you try to run your program you will see the following error message printed to the command window.
Unrecognized function or variable 'name'.
Error in M12_Functions (line 12)
fprintf("%s\n", name)
MATLAB does not know what name
is because it was declared inside of a function and is therefore a local variable. Comment out or delete this line so that your program runs ok.
12.1.2. Exercise#
Create a new MATLAB file called M12_Functions_exercises.m and save it to your OneDrive folder. Use it to answer the following exercises.
Write a function called loving_it()
that outputs the following message to the command window
I'm writing MATLAB programs and I'm loving it.