12.2. Arguments#
We can pass information to a function in the form of arguments. When a function is declared, the input arguments are listed inside the brackets and separated by commas.
function function_name(argument)
% commands
end
To demonstrate this, enter the following code into the end of your program
function print_age(age)
fprintf("Age : %d \n", age)
end
and make a call to this function by entering the following code before the function declarations.
% Function arguments
fprintf("\n")
print_age(19)
Here we have declared a function called print_age()
that has a single input argument age
. We then call this function specifying the input value 19. Run your program and you should see the following added to the command window.
Age : 19
Lets say that Ellie has survived for another year and is now 20. To demonstrate this, enter the following code into your program.
print_age(20)
Rerun your program and you should see the following added to the command window.
Age : 20
So we can see that the value of the argument is used by the fprintf()
command in the function.
12.2.1. Multiple arguments#
A MATLAB function can have any number of arguments which are listed in the brackets separated by commas.
function function_name(argument1, argument2, ...)
% commands
end
To demonstrate this, enter the following code into the end of your program
% ------------------------------------------------------------------------
function print_name_and_age(first_name, last_name, age)
fprintf("Name : %s %s\n", first_name, last_name)
fprintf("Age : %d\n", age)
end
and make a call to this function by entering the following code before the function declarations.
% Multiple arguments
fprintf("\n")
print_name_and_age("Joel", "Miller", 56)
Run your program and you should see the following added to the command window.
Name : Joel Miller
Age : 56
12.2.2. Arbitrary arguments#
If you don’t know how many arguments will be passed into your function we can use arbitrary arguments using the varargin
keyword as the input argument. This means that the arbitrary argument will be a tuple (an ordered set of objects).
function function_name(varargin)
% commands
end
The individual elements in varargin
can be accessed using varargin{index}
and the number of elements in varargin
is stored in nargin
. To demonstrate this, enter the following code into the end of your program.
% ------------------------------------------------------------------------
function print_names(varargin)
for i = 1 : nargin
fprintf("%s\n", varargin{i})
end
end
and make a call to this function by entering the following code before the function declarations.
% Arbitrary arguments
fprintf("\n")
print_names("Ellie", "Joel")
Run your program and you should see the following added to the command window.
Ellie
Joel
So the print_names()
function has an arbitrary argument names
and by passing two strings into the function both of the strings are printed. Now enter the following code into your program.
print_names("Tommy", "Dina", "Jesse")
Rerun your program and you should see the following added to the command window.
Tommy
Dina
Jesse
So we can pass any number of strings into the print_names()
function.
Important
varagin
must come after all standard arguments in the function declaration.
12.2.3. Exercises#
Write a function called odd_or_even()
that outputs a message to the command window stating whether an inputted number is odd or even. Use your function on the following:
2
5
0
Write a function called my_isprime()
that checks whether an inputted integer is a prime and prints the result. Use your function to determine which of the following are prime:
3469
6137
Write a function called my_mean()
that takes in the inputs of an unknown number of values and prints the mean value. Use your function to output the mean of the following numbers correct to 4 decimal places:
1, 2, 3, 4
5, 3, 7, 5, 8, 2, 4, 2, 1