## Introduction: Creating a Function in MATLAB

MATLAB is a tool that engineers and other professionals can use to quickly and efficiently analyze data, make calculations, and display information. One of the many ways that the user can interact with MATLAB is through the use of functions. Functions receive specific information, known as inputs, and use the inputs to calculate other information to display, known as outputs. This instructable will detail the process of creating a function and will guide the user through creating a function of their own.

To complete this instructable, you will need:

1. A computer with access to MATLAB

2. 5-10 minutes

## Step 1: Open MATLAB

First, open the start menu by pressing the Windows button in the bottom left corner of the screen, or by pressing the Windows key on your keyboard. Then, type MATLAB into the search bar and select "MATLAB R2013a." Note that the exact name of the program may vary slightly if your version of MATLAB is older. You should still see the same symbol as shown in the image even if your version is older, so you should use this to find the right program.

## Step 2: Open Script Window

When MATLAB opens, the user is taken to MATLAB's primary window, also known as the Command Window. In order to create a function, however, the user must open the Script Window. Select the highlighted button to open the Script Window. Scripts are saveable sequences of commands that can be run all at once. A function is a special kind of script that can take different inputs each time it is run. Note that in older versions of MATLAB, the highlighted button will not appear, and the user must select File > New > Script in order to open the Script Window.

## Step 3: The First Line

Once the Script Window opens, type "function f = make_a_square(x)" into line 1. The word "function" tells MATLAB that this script will be a function. The text between the word "function" and the = signifies the output of the function, in this case being "f". The text inside of the parentheses signifies the input, in this case being "x." Finally, the text directly to the left of the parentheses is the name of the function, which in this case is called "make_a_square."

## Step 4: Finishing the Function

Once the first line of the function is entered, complete the code by entering "f=x^2;" on line 2 and "end" on line 3. The second line tells MATLAB to take the input, square it, and set the new number as the output. The semicolon ending the line stops MATLAB from outputting that line later. Were the semicolon not used, the output would display twice when we used the function later on. Line 3 tells MATLAB that the function has concluded.

## Step 5: Saving the Function

Once your function is complete, save the function using the save button. The default name of the save file will be the same as the name of the function. **Make sure that you do not change this.** Changing the name of the save so that it is not the same as the name on line 1 can cause the function to not work properly. Note that, as in Step 2, older versions of MATLAB will not have the highlighted button, so saving must also be accomplished through the File menu.

## Step 6: Running the Function

Return to the Command Window. Enter "make_a_square(3)" and hit Enter on your keyboard to test your function. Your code should square the number 3, outputting 9. If the code does not do this, check to make sure that you spelled the function correctly in the Command Window. If this does not solve the problem, reopen your function in the Script Window and ensure that there are no errors. Once you code is outputting correctly, you will have successfully created your first MATLAB function!

## Discussions

I like the instructions. The differentiation between a script file and a function file was also useful, although I didn't see it at first.

Also, for anyone making their own Matlab function the _underscores between make_a_square are necessary for Matlab to save the file.

One thing that would improve the instructions would be the use of smaller images that can show the function up close and in greater detail. Otherwise good job.