Functions (subprograms) - 1 of 4 Often you would like to do the same calculation in several different places inside a program, without involving the user. One way is to copy the code. When you update the calculation later you must do it in several places. A better way is to group the statements needed for the calculation to one unit, and give the unit a name. You are creating a function. When you write the name of the function in a program (or at Matlabs command line) the group of statements coupled with this name will be executed. To use a function from Matlabs command line Matlab require us to put the function in a file with the same name (and append the usual .m to the name). To make functions really useful two problems must be solved. First: The statements executed in the function will likely produce a result. The program that use the function must have a way to retrieve the result for further use. Second: The statements in the function often need some input data to do the calculation. The program that use the function must have a way to specify the input. Consider the math function sinus as an example of both. It need an input (angle), produce a result, and is often used in all kind of programs that do calculations.
Functions (subprograms) - 2 of 4 A function that need no input and produce no result is created like this in Matlab: function Name Sequence_Of_Statements end A function that need no input and produce one result is created like this in Matlab: function Result = Name Sequence_Of_Statements end A function that need no input and produce N results is created like this in Matlab: function [R_1, R_2, ..., R_N] = Name Sequence_Of_Statements end A function that need N inputs and produce one results is created like this in Matlab: function Result = Name(P_1, P_2, ..., P_N) Sequence_Of_Statements end How to write a function that need N inputs and produce M results is left as an exercise for you.
Functions (subprograms) - 3 of 4 In programming terms a result from functions is called a “return value”, and an input is called a “parameter”. To use a function we must write the name in a program. In programming terms we “call” the function. When we call a function we must supply a value for each parameter. The value can consist of any expression, and is assigned to the corresponding parameter. In programming terms an expression assigned to a parameter is an “argument” to the function. function positive = absolute(n) % calculate the absolute value of n if n < 0 positive = -n; else positive = n; end end % to call the function y = absolute(-56); disp(y); % call, and display the result directly disp(absolute(-23 * y)); % supply the parameter from user input disp(absolute(input(‘enter a number: ’)));
Functions (subprograms) - 4 of 4 Variables created inside a function will only be available there. This is true for the parameters too. Parameters are like normal variables that are automatically created and assigned values from the arguments when the function start. If the function is used several times in the same program all variables inside will be different and independent each time the function is called. Even when the function calles itself. Each call to a function will create its own set of variables, like it had its very own universe to store them in. This universe is dubbed “scope” in programming terms. It is created when the function is entered, and destroyed when the function is finished. The special statement return can be used to finish a function early, if the remaining statements in the function should not be executed, or do not have to be executed.
Recommend
More recommend