ENGR 1181 MATLAB 14: Functions 1
Preparation Material

ENGR 1181 | MATLAB 14: Functions 1Preparation Material

Learning Objectives

  1. Demonstrate the proper use of functions in their programs
  2. Explain how functions can be used in multiple places and as an organizational tool
  3. Describe purpose and structure of a function definition line

Topics

Students will read Chapter 7.1-7.7 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading.

Students will learn a basic understanding of user-defined functions and how to properly code these functions in MATLAB. This material contains the following:

  1. Key Definitions
  2. Function Fundamentals
  3. Creating a Function File
  4. Function Syntax
  5. General Function Tips

1. Key Definitions

User-Defined Function – a program created by the user that can be used like a built-in function. Functions have an input and an output, and can be used many times with different input values.

Function File – a separate MATLAB file that contains the code for a user-defined function.

2. Function Fundamentals

Functions work by taking input values and performing operations upon these values (such as addition or multiplication). After the function performs operations on the input values, it then returns the newly updated values as output values.

3. Creating a Function File

A function file is created in the Editor/Debugger Window. This is the same window that is used to create script files. To create a function file, begin by clicking New (top left in MATLAB), and then select ‘Function’ or ‘Script’.

Selecting Function will create a script file with the following contents (see below). This option provides the basic structure for a function. Notice that MATLAB does not define input arguments, output arguments, or the function name. The code highlighted in yellow will need to be updated by the coder in order to create a working function (covered in the Function Syntax section).

The second option for creating a function is to select Script on the New menu in MATLAB. This will create a blank script file. The user must then manually enter the function definition line, and other required syntax, such as the end statement.

4. Function Syntax

In order to create a working function, a specific syntax must be followed. The very first line of the MATLAB file must contain a function definition line. A function definition line is a line of code that tells MATLAB the function’s name, defines the input arguments, and defines the output arguments. The function definition line is the first line of code seen below. In addition to the function definition line, all functions require an ‘end’ statement. This is similar to for-loops and if-statements.

The text highlighted in yellow above must be changed before the function will operate.

Untitled is where the name of the function is placed.

input_args is where the names of the variables that will be storing the input values are placed. For example, if an input value is 2 and input_args has been replaced with X, then X will be set equal to 2. X^2 can now be used within the function.

output_args is where the names of the variables that the function sends back to the user are placed. For example, if output_args is replaced with Y, then the function will send the user the value stored in Y after the function has completed its calculations.

Below is a function with acceptable syntax:

In the previous function, the following highlighted items were changed:

Untitled has been changed to calculateY. calculateY is now the name of the function, and will be used to ‘call’ the function. The function file must be saved as this name or it will run. For example, this function file is saved as calculateY.m.

Input_args has been changed to X. The variable X will store the input value(s) given to the function by the user.

Output_args has been replace with Y. The variable Y holds the value(s) that will be sent back to the user after the function has done its calculations.

Here is the command window after running the calculateY function with an input of 3:

The first line is the function call. The code z=calculateY(3) tells MATLAB to run the calculateY function with an input value of 3, and then store the result in z. This is similar to other common function calls such as x=sin(2).

Within the calculateY function, the input argument has been set to X. In this case, MATLAB will set X = 3. X can now be used as a variable (containing the value of 3) throughout the rest of the function code.

After setting X = 3, MATLAB will then move down to the next line of code in the function. In this case it will move onto Y = X^2. Since X = 3 from the user input, MATLAB will set Y = 3^2. This results in Y=9.

Next, MATLAB will reach the end statement at the bottom of the function. At this point, MATLAB will return the values stored within the output argument variables. In this example, the output argument is Y, so MATLAB will return the value stored within Y, which is 9.

Finally, MATLAB sets z = 9. It is important to note that MATLAB does not return the variable Y. It only returns the value stored within Y. This means that within the main script file or command window, the variable Y will still have no value.

In summary:

In this example the input value is 3. Within the function file, X is set equal to 3. Next, the X=3 value is used to calculate the value of Y = X^2. This results in Y=9. The function then reaches the end statement and return the value of Y, which is equal to 9. Lastly, z will be set equal to the return value of 9. The final result is z=9.

Here is an example in which there are two input arguments and two output arguments

In this case, Y and Z are sent back to the user, and the user must enter values for both X1 and X2

This is a command window run of the function calculateYZ

In this case, X1 is equal to 3 and X2 is set equal to 4. Next, Y is calculated to be 9 and Z is calculated to be 16 (using the values of X1 and X2). Finally, the values 9 and 16 are returned to the user and set equal to a and b respectively.

5. General Function Tips

Here are some key tips to remember for functions:

·  Use a function to do tasks that need to be repeated a large number of times.

·  Function files must be saved within the same workspace folder that contains the script file, which is calling the functions.

·  A function is not automatically saved. Script files will save when you select “run”, but a function file will not. The user must manually save a function file.

·  The first line of the function file must be the function definition line.

·  Functions must always have an ‘end’ statement.

·  All variables inside of the function are local. This means that these variables and their values only exist within the function, and nowhere else.

·  User-defined functions are used in the same way as built-in functions. Examples of built-in functions include: sin(), tan(), and disp(), max(), etc.

·  When saving a function file, the file must be saved with a name that is identical to the function name in the function definition line.

·  A function can have any number of input arguments and output arguments. This also includes situations in which there are no input arguments, or no output arguments.

·  Remember to save a function before running it. If it is not saved, MATLAB will run the OLD version of the function.

2