Sie sind auf Seite 1von 13

User-Defined Functions

Lecture 8

Textbook: chapter 7
User-Defined Functions
• y = f(x) x: input y: output

• Matlab built-in functions:


sin(x), abs(x), sqrt(x), exp(x), …

• When a function needs to be evaluated many times for


different values of arguments, it is convenient to
create a “user-defined” function.
Creating a Function File
• In the File menu, select New, and then select Function.
• The editor contains several pre-typed lines that outline the structure of a
function file:

function [ output_args ] = Untitled( input_args )


%UNTITLED Summary of this function goes here
% Detailed explanation goes here

end

• “end” is optional
• You can also open an empty script by selecting Script after New. The
window that opens is empty, without any pre-typed lines. The window can
be used for writing a script file or a function file.
Structure of a Function File
Function Definition Line
• The first executable line in a function file must
be the function definition line.
Input and Output Arguments
• Usually, there is at least one input and one output arguments,
although it is possible to have a function that has no input or
no output arguments. If there are more than one, the
input/output arguments are separated with commas.
Help Text Lines
• The help text lines are comment lines that contain an explanation of the
function and any instructions related to the input and output arguments.
• The help lines are typed between the function definition line and the first
non-comment line.
• Example:
function A = CircleArea(r)
% This function calculates the area of a circle
% inputs
% r: radius (m)
% outputs
% A: area (m^2)
A = pi*r^2;
end
- Save the file
- Type “help CircleArea” or “doc CircleArea”
Function Body
• The function body contains the computer program (code) that
actually performs the computations. The code can use all
MATLAB programming features.

function A = CircleArea(r)
% This function calculates the
area of a circle
% inputs
% r: radius (m)
% outputs
% A: area (m^2)
A = pi*r^2;
end
Saving a Function File
A function file must be saved before it can be used. This is done, as with a
script file, by choosing Save as . . . from the File menu, selecting a location
(many students save to a flash drive), and entering the file name. It is highly
recommended that the file be saved with a name that is identical to the
function name in the function definition line. In this way the function is called
(used) by using the function name. (If a function file is saved with a different
name, the name it is saved under must be used when the function is called.)
Function files are saved with the extension .m.
Examples:
Using a User-Defined Function
• Method 1: a function can be used with numbers as input arguments

myArea = CircleArea(2)

• Method 2: a function can be used with pre-assigned variables as input


arguments

myRadius = 5;
myArea = CircleArea(myRadius)
Local and Global Variables
• All the variables in a function file are local (the input and
output arguments and any variables that are assigned values
within the function file).
• This means that the variables are defined and recognized only
inside the function file.
• All of this means that a function file can have variables with
the same names as variables in the Command Window or in
script files. The function file does not recognize variables with
the same names as have been assigned values outside the
function. The assignment of values to these variables in the
function file will not change their assignment elsewhere.
Example
Write and Save This function:

function A = CircleArea(r)
% This function calculates the area of a circle
% inputs
% r: radius (m)
% outputs
% A: area (m^2)
A = pi*r^2;
End

In Command Window, type:

myRadius = 5;
myArea = CircleArea(myRadius);
disp(r);
disp(A);
Local and Global Variables
• Each function file has its own local variables, which are not shared with
other functions or with the workspace of the Command Window and the
script files.
• It is possible, however, to make a variable common (recognized) in several
different function files, and perhaps in the workspace too. This is done by
declaring the variable global with the global command, which has the
form:
global variable_name;
• The variable has to be declared global in every function file that the user
wants it to be recognized in. The variable is then common only to these
files.
• The use of long descriptive names (or all capital letters) is recommended
for global variables in order to distinguish them from regular variables.

Das könnte Ihnen auch gefallen