# Intro to MATLAB Unit 3: FUNCTIONS

## Contents

## Recall: MATLAB Built-in Functions

Similar to a mathematical function, MATLAB functions define an output for any given input. We have been already using MATLAB built-in functions. There are built-in functions fomr many common operations such as sin(x), exp(x) sqrt(x).

However, MATLAB won't always have a function for what we want to do. At that point it becomes necessary to write our own.

## Writing your own MATLAB Functions

The functions that we create will behave just like the pre-defined MATLAB functions, except we will use the concepts we've been learning to define their behavior ourselves. **Using functions helps to divide and organize your code into smaller, more digestable sections.** Large computer programs are usually made up of many smaller functions which are used when necessary, instead of one large block of code.

Function files can be created in much the same way as script files, the main difference being the function definition. Typically only one function is defined per function file. All functions are defined using the same syntax. An example function definition would look like:

```
function y = exampleFunction(x)
```

Contrast this definition with our knowledge of the form of a mathematical function: f(x) = x + 4

--> The same main features are there, just in a different order.

There are three main parts to any function, though some of the parts are optional: input parameters, output parameters and the function body.

## Input Parameters

The data that the function will use is given to it via the input parameters. The input parameters are contained within parentheses following the function name. This is the same way that input parameters are passed to a function as well. sin(x) returns the sin of x, sqrt(x) returns the square root of x, etc. Input parameters are optional (i.e. it is possible to write a function without any input parameters). Can you think of any examples we've seen of this?

You can see that the MATLAB and mathematical functions defined above have a single input parameter named x. In both cases, this parameter is included inside parentheses. When the function is written, the input parameters can (and should) be used in the function body to perform the computations.

## Output Parameters

The variables to the left of the equals sign in the function definition are the output parameters. This is where the results of the function will be stored. Notice that it looks similar to a variable assignment statement, and indeed it can be thought of in this way.

Output parameters need to be assigned in the function body or they will not be particularly useful. Output parameters are also optional: functions which display a plot or text often do not need output parameters. It is possible to have multiple output parameters by including them within a vector. In the function definition example above, the y variable is the output parameter. The ''f(x)'' portion of our mathematical function acts as its output parameter. (often times ''f(x)'' is replaced with another variable, such as y).

## Function Body

This is the part that actually performs the desired computations.

Look at the full version of our example function and make sure you can recognize all of the parts.

## *Practice Problem!

Look at these example function definitions and see if you can figure out the number of input and output parameters they have:

%function [x, y, z] = pointLocation() %function displayPlot(x, y) %function [total, avg] = burritoCounter(V) %What would the function for f(x) = x + 4 look like?

## Comments in Functions

Comments are **extremely useful** , especially when working with functions. After each function definition, you should supply comments that describe the function: the inputs, outputs, and a brief description of what the function does.

These comments can be displayed with the help function:

```
help exampleFunction
```

## Calling a Function

After your function has been defined, it is useless to you unless you use it to do something. This is accomplished by calling your function. You call a function by typing the function name followed by the inputs in parentheses. If there are no inputs, use empty parentheses.

exampleFunction(1) exampleFunction(2) %USEFUL: if your functions are stored in a subfolder, use the following to %add the current folder and all its subfolders to the MATLAB path! addpath(genpath('.')) %You can also use variables as function inputs: a = 9.5; exampleFunction(a) %If you wish to store the result of the function to a variable then include %that variable in your statement: result = exampleFunction(7) result2 = exampleFunction(-5) %If your function has multiple outputs then you must use a vector to store %the output variables (dimensions must match!): [val index] = min([5 -2 8 9 -19 11 5])

As you can see, built-in MATLAB functions work the same way as our functions do (I did not write the min function). You can even see how some of the built-in MATLAB functions are defined.

## *Practice Problems!

Write function files for the following:

- Area of a circle
- Miles per gallon of a car (inputs: miles driven, gallons used)
- Area of a triangle (inputs: base and height)

Write a function that converts a person's height and weight from inches and pounds to centimeters and kilograms. The ratios are as follows:

%1 in = 2.54cm %1 lb = 0.454kg

Write a line of code that will call your function using your own height and weight.

Lecture notes for CSE200 (Fall 2015) at Washington University in St. Louis by Marion Neumann (based on materials from Doug Shook).