Lesson 4

# Functions

0

A MATLAB “function” is a MATLAB program that performs a sequence of operations specified in a text file (called an m-file because it must be saved with a file extension of *.m).

A function accepts one or more MATLAB variables as inputs, operates on them in some way, and then returns one or more MATLAB variables as outputs and may also generate plots or just generates plots only. This functions could be Pre-defined/inbuilt or user defined to solve a given problem.

In this chapter we focus on the following areas.

1. Predefined/built-in MATLAB functions

2. User defined functions overveiw

3. Function definition

4. Function Calls

## Predefined/built-in functions in MATLAB

Matlab has a variety of predefined functions readily available for use by simply using their names and providing the right number and type of arguments. For example, the natural logarithm is readily available

## User defined functions overview

User-defined functions are functions that the programmer defines, and then uses, in either the Command Window or in a script. These functions are not pre-programmed in MATLAB and can be defined by using an m-file. How do I create an m-file?

Since an m-file is nothing more than a text file it can be created using any text editor however, MATLAB provides its own editor that provides some particular features that are useful when writing/editing functions.

To open a new m-file: In the MATLAB command window, go to FILE on the toolbar, select NEW, then select M-FILE. This opens the MATLAB editor/debugger and gives an empty file in which you can create whatever m-file you want. Refer to chapter 1 .

## Function Definition

General form of a function definition for a function that calculates and returns one value looks like this: for the function called functioname defined in the script.

The input arguments in parentheses, which correspond to the arguments that are passed to the function in the function call. A comment that describes what the function does (this is printed when help is used).

The body of the function, which includes all statements and eventually must put a value in the output argument. end at the end of the function (though not considered necessary in many cases it is considered good style)

Let us create a function that calculates the area of the a circle given the radius.

A radius of a circle is passed to the function in the input argument rad. The function calculates the area of this circle and stores it in the output argument area. When the function is called in the command window and a radius passed to it the area of the circle with that radius is calculated.

It is important to note that User Defined function do not affect variables in the workspace. There work spaces for User defined functions are always isolated from all the other workspace.

User defined function can have more than one inputs as well as outputs. Suppose we are required to write a function in MATLAB the accepts the dimension of any rectangle and returns its area and perimeter. This function would have two inputs; length and width. In addition it will have two outputs area and perimeter. Let us call this function rectaPerimArea()

## Function Call

The function call to the functions consists of the function, followed by the arguments in parentheses. The functions receives an input argument and returns a result as shown in calculating the area of the circle in the example above. The following happens when a function call is executed.

• When a function call is encountered, control is passed to the function itself and the function begins execution.

• The arguments are passed to the function.

• The function executes it statements.

• The function calculates a single value and return it.

• The control is passed back to the expression that called it in the first place, which then continues.

During function call all inputs must be provided for the function to be executed. However, it is not mandatory that all outputs have to be declared. In case, no output is declared the first output parameter is always returned. Consider the function rectaPerimArea() called in the command window for the following scenarios.

NOTE: when writing user defined functions in MATLAB avoid use names that are similar to the the inbuilt functions in MATLAB.

The You Tube video below show a detailed approach on how to create and use User Defined functions in MATLAB.

## Exercise

1. Create a user defined function to find the factorial of a number.

2. Create a user defined function to find simple Interest, with Principal amount, no. of years and rate as parameters. I = PRT

3. Write a function that take the side length as the input and then calculate the total surface area and volume of a cube .