Basic
Computer
Programming
Dr. Deniz Kutluay
User-Defined Functions
and Function Files
• CREATING A FUNCTION FILE
• STRUCTURE OF A FUNCTION FILE
• LOCAL AND GLOBAL VARIABLES
• SAVING A FUNCTION FILE
• USING A USER-DEFINED FUNCTION
• COMPARISON BETWEEN SCRIPT FILES AND
FUNCTION FILES
• ANONYMOUS FUNCTIONS
• FUNCTION FUNCTIONS
• SUBFUNCTIONS
• NESTED FUNCTIONS
Mission & Goals
Mission
Listen to me carefully, and practice some examples using Matlab by
following me
Goal #1 Goal #2 Goal #3
To learn To comprehend To understand nested
construction rules function types and functions in detail.
of functions. relations.
Basic Computer Programming Dr. Deniz Kutluay
User-Defined Functions
and Function Files
A user-defined function is a MATLAB
program that is created by the user,
saved as a function file, and then used like
a built-in function.
The function can be a simple single
mathematical expression or a complicated
and involved series of calculations.
The input and the output can be one or
several variables, and each can be a
scalar, a vector, or an array of any size.
001
Basic Computer Programming Dr. Deniz Kutluay
Creating a function
file
The editor contains several pre-typed lines
that outline the structure of a function
file.
The first line is the function definition line,
which is followed by comments the
describe the function.
Next comes the program and the last line
is an end statement, which is optional.
002
Basic Computer Programming Dr. Deniz Kutluay
Structure of a function
file
Function Definition Line:
The first executable line in a function file
must be the function definition line.
Otherwise the file is considered a script
file.
▪ Defines the file as a function file
▪ Defines the name of the function
▪ Defines the number and order of the
input and output arguments
003
Basic Computer Programming Dr. Deniz Kutluay
Structure of a function
file
Function Definition Line:
The name can be made up of letters,
digits, and the underscore character (the
name cannot include a space).
The rules for the name are the same as the
rules for naming variables.
It is good practice to avoid names of built-
in functions in Matlab.
004
Basic Computer Programming Dr. Deniz Kutluay
Structure of a function
file
Input and Output Arguments:
The input and output arguments are used
to transfer data into and out of the
function.
it is possible to have a function that has
no input arguments.
The output arguments transfer the
output from the function file.
Function files can have zero, one, or
005
several output arguments.
Basic Computer Programming Dr. Deniz Kutluay
Structure of a function file
006
Basic Computer Programming Dr. Deniz Kutluay
Structure of a function
file
The H1 Line and Help Text Lines:
➢ When a user types lookfor a_word in the
Command Window, MATLAB searches for
a_ word in the H1 lines of all the
functions, and if a match is found, the H1
line that contains the match is displayed.
➢ When the user types help function_name
in the Command Window the H1 line and
the help text are displayed.
007
Basic Computer Programming Dr. Deniz Kutluay
Local and global variables
➢ All the variables in a function file are local. This means that the variables are
defined and recognized only inside the function file.
➢ The function file does not recognize variables with the same names as have been
assigned values outside the function.
➢ 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.
008
Basic Computer Programming Dr. Deniz Kutluay
Saving a function file
A function file must be saved before it can
be used.
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.
009
Basic Computer Programming Dr. Deniz Kutluay
Using a user-defined
function
A user-defined function is used in the
same way as a built-in function.
The function can be called from the
Command Window, from a script file, or
from another function.
To use the function file, the folder where
it is saved must either be in the current
folder or be in the search path.
010
Basic Computer Programming Dr. Deniz Kutluay
Comparison between script files and function files
➢ Both script and function files are saved with the extension .m
➢ The first executable line in a function file is (must be) the function definition line.
➢ The variables in a function file are local. The variables in a script file are recognized in
the Command Window.
➢ Script files can use variables that have been defined in the workspace.
➢ Function files can accept data through input arguments and can return data through
output arguments.
➢ When a function file is saved, the name of the file should be the same as the name
of the function. 011
Basic Computer Programming Dr. Deniz Kutluay
Anonymous functions
An anonymous function is a simple (one-
line) user-defined function that is defined
without creating a separate function file
(m-file).
Anonymous functions can be constructed
in the Command Window, within a script
file, or inside a regular user-defined
function.
012
Basic Computer Programming Dr. Deniz Kutluay
Anonymous functions
Example of an anonymous function with
one independent variable:
The function The function can then be used for different values of x
can be defined (in the Command Window)
as an anonymous function for x as a scalar.
013
Basic Computer Programming Dr. Deniz Kutluay
Anonymous functions
Example of an anonymous function with
one independent variable:
If x is expected to be an array, with the
function calculated for each element, then
the function must be modified for
element-by-element calculations.
014
Basic Computer Programming Dr. Deniz Kutluay
Anonymous functions
Example of an anonymous function with
several independent variables:
The function
can be defined as an anonymous function.
Then the anonymous function can be used
for different values of x and y.
015
Basic Computer Programming Dr. Deniz Kutluay
Function functions
There are many situations where a
function (Function A) works on (uses)
another function (Function B).
This means that when Function A is
executed, it has to be provided with
Function B.
A function that accepts another function
is called in MATLAB a function function.
016
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Using Function Handles for Passing a
Function into a Function Function:
Function handle:
❑ For built-in and user-defined functions, a
A function handle is a MATLAB value that function handle is created by typing the symbol
is associated with a function. @ in front of the function name.
It can be passed as an argument into ✓ For example, @cos is the function handle of
another function. the built-in function cos.
Function handles can be used with built-in ❑ As anonymous functions their name is already
functions, user-defined functions, and a function handle.
anonymous functions.
017
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Using Function Handles for Passing a
Function into a Function Function:
Writing a function function that accepts a
function handle as an input argument:
A user-defined function function, named
funplot, that makes a plot of a function
(any function f(x) that is imported into
it) between the points x = a and x = b.
The input arguments are (Fun,a,b), where
Fun is a dummy name that represents the
imported function, and a and b are the end
018
points of the domain.
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Writing a function function that accepts a
function handle as an input argument:
As an example, the function First, a user-defined function is written for f(x).
over the domain [0.5, 4] is passed into the
user-defined function funplot. This is done
in two ways:
1- Passing a user-defined function into a
function function:
019
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Writing a function function that accepts a
function handle as an input argument:
First, a user-defined function is written for f(x).
1- Passing a user-defined function into a
function function:
Next, the function Fdemo is passed into the user-defined
function function funplot.
Note that a handle of the user-defined
function Fdemo is entered (the handle is
@Fdemo) for the input argument Fun in the
user-defined function funplot.
020
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Writing a function function that accepts a
function handle as an input argument:
1- Passing a user-defined function into a
function function:
021
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Writing a function function that accepts a
function handle as an input argument:
2- Passing an anonymous function into a
function function:
To use an anonymous function, the
function
Note that the name of the anonymous function
first has to be written as an anonymous FdemoAnony is entered without the@ sign for the input
function, and then passed into the user- argument Fun in the user-defined function funplot (since
defined function funplot. the name is already the handle of the anonymous function).
022
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Using the feval command with built-in functions
Using a Function Name for Passing a
Function into a Function Function
The feval command:
The feval command evaluates the value of
a function for a given value (or values) of Using the feval command with user-defined functions
the function's argument (or arguments).
023
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Using a Function Name for Passing a
Function into a Function Function
Writing a function function that accepts a
function by typing its name as an input
argument:
The function is the same as the function
funplot from previous section, except that
the command feval is used for the
calculations with the imported function.
024
Basic Computer Programming Dr. Deniz Kutluay
Function functions
Using a Function Name for Passing a
Function into a Function Function
Passing a user-defined function into
another function by using a string
expression:
A user-defined function named Fdemo, is
passed into the user-defined function
funplotS.
➢ Note that the name Fdemo is typed in
a string for the input argument Fun in
the user-defined function funplotS.
025
Basic Computer Programming Dr. Deniz Kutluay
Subfunctions
A function file can contain more than one
user-defined function.
The functions are typed one after the
other.
Each function begins with a function
definition line.
The first function is called the primary
function and the rest of the functions are
called subfunctions.
026
Basic Computer Programming Dr. Deniz Kutluay
Subfunctions
The name of the function file that is saved
should correspond to the name of the
primary function.
Each of the functions in the file can call
any of the other functions in the file.
Outside functions, or programs (script
files), can call only the primary function.
Each of the functions in the file has its
own workspace. In other words, the
primary function and the subfunctions
cannot access each other's variables. 027
Sample Problem 7.4:
Average and standard deviation
Write a user-defined function that
calculates the average and the
standard deviation of a list of
numbers.
Use the function to calculate the
average and the standard deviation
of the following list of grades:
80 75 91 60 79 89 65 80 95 50 81
Sample Problem 7.4:
Average and standard deviation
Write a user-defined function that
calculates the average and the
standard deviation of a list of
numbers.
Use the function to calculate the
average and the standard deviation
of the following list of grades:
80 75 91 60 79 89 65 80 95 50 81
Basic Computer Programming Dr. Deniz Kutluay
Nested functions
A nested function is a user-defined
function that is written inside another
user defined function.
The portion of the code that corresponds
to the nested function starts with a
function definition line and ends with an
end statement.
An end statement must also be entered at
the end of the function that contains the
nested function.
028
Basic Computer Programming Dr. Deniz Kutluay
Nested functions
The format of a user-defined function A
(called the primary function) that contains
two nested functions B and C at the same
level.
➢ The three functions can access the
workspace of each other.
➢ The three functions can call each other.
029
Basic Computer Programming Dr. Deniz Kutluay
Nested functions
As an example, the following user-defined
function (named statNest),with two
nested functions at the same level, solves
Sample Problem 7-4.
Note that the nested functions are using
variables (n and me) that are defined in
the primary function.
030
Basic Computer Programming Dr. Deniz Kutluay
Nested functions
Using the user-defined function statNest
in the Command Window for calculating
the average of the grade data gives:
031
Basic Computer Programming Dr. Deniz Kutluay
Nested functions
Nested functions can also contain nested
functions.
The following shows an example for the
format of a user-defined function with
four nested functions in two levels.
The following rules apply to nested
functions:
• A nested function can be called from a
level above it. (In the preceding
example, function A can call B or D, but
not C or E.) 032
Basic Computer Programming Dr. Deniz Kutluay
Nested functions
• A nested function can be called from a
nested function at the same level
within the primary function. (In the
preceding example, function B can call
D, and D can call B.)
• A nested function can be called from a
nested function at any lower level.
033
Basic Computer Programming Dr. Deniz Kutluay
Nested functions
• A variable defined in the primary
function is recognized and can be
redefined by a function that is nested
at any level within the primary
function.
• A variable defined in a nested function
is recognized and can be redefined by
any of the functions that contain the
nested function.
034
Thank
You
Dr. Deniz Kutluay
Electrical-Electronics Engineering
Design Architect at Vestel
deniz.kutluay@vestel.com.tr
deniz.kutluay@cbu.edu.tr