Skip to main content

Section 16.3 Function Variables vs User Variables

Before I show you how to use your function, it is important to note the distinction between the internal function variables and the variables that your user uses as inputs and outputs.
Key Concept: The user can use different variable names than the ones that you internally defined for your function.
This is a very important point and one that I see undergraduate programmers constantly confused about. To show you what I mean, let’s use your function in the command window (or you can create a script if you would like).
First, let’s define an array of radii (make sure you are following along with this example!):
r = 1:10;
Next, let’s use our new function sphere_vol() to calculate the volumes associated with those radii! Exciting

Example 16.3.1. sphere_vol() in action.

Here is an interactive code box that functions identically to the MATLAB command window.
Remember that our function’s internal variables were radii and volume. You do not need to use these exact variable names when using your function. In this case, we used the variables r for the radii and we stored the volumes in variable v. The internal function variables are just placeholders that tell the function how to manipulate the inputs and where to store the outputs. The names inside the function do not matter to the user.
Test- Your function is ready to use! Go ahead and test the function with a bunch of different radii. Can you break the function? Make it throw an error?
Repeat- Again, this is a pretty simple function, but when you make more complicated functions it is a good idea to revise your help text or your sketches as you test and find issues.
Adding a Folder to the Path
If you are the organized type, you can actually add folders to your MATLAB path. Returning to our garage analogy: you can think about it as throwing the impact_driver() function into a box, and throwing it in the garage (path).
Adding folders to the path is a good way to increase the usefulness of your functions. Right now, the sphere_vol() function we created will only work if you are currently in the directory that contains the file sphere_vol.m! If we add the folder that sphere_vol.m is contained into the path, we can call the sphere_vol() function from anywhere.
The idea is that once you create this cool new function, you might want to use it in scripts that are stored in different folders. You can only do this if you add the function file location to MATLAB’s path.
To show you how this works, let’s create a folder called “Functions” and store it somewhere on your computer that you will remember (MyDocuments? Somewhere else? You decide). Then navigate to the folder that contains this new folder in MATLAB. In the “Current Folder” browser you can then right-click on the “Functions” folder, and select the “Add to path >” option. Then select the “Selected Folder and Subfolders” option. Viola! Now any function file inside this particular folder will be accessible regardless of your current working directory. You told MATLAB where to look!
You have attempted of activities on this page.