functions general example (+1return values, +1 parameters) 1. the client's wish 2. creating the...
TRANSCRIPT
![Page 1: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/1.jpg)
1
FunctionsGeneral Example
(+1return values, +1 parameters)
1. The client's wish
2. Creating the function1. Function's name, Parameter list, Return-info,
Documentation, and Code Body, saving it.
3. Testing in the command window
4. Testing with a main file
5. A new client comes along1. Ignoring return values
![Page 2: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/2.jpg)
2
1. The client's wish:
Create a function which receives 3 arguments (a height, a mass and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.
The client also gives these as indications:
BMI Category depends on BMI value:Underweight <=18.5Normal weight = 18.5-24.9Overweight = 25-29.9Obesity = BMI of 30 or greater
![Page 3: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/3.jpg)
Step2. I/O of the function
3 parameters, 2 return-values
3
bmiCalculator()
Unit system
Bmi value
weight
height
Bmi category
![Page 4: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/4.jpg)
2. Determine the return-info
Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.
4
Data calculated becomes return-info.When more than one variable comes back enclose the return list inside [ ] .
![Page 5: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/5.jpg)
2. Choose a function name
Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.
5
Name it so it represents what it does
![Page 6: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/6.jpg)
2. Create the parameter list
Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.
6
Inputs received become parameters.Separated by COMMAS all in ( ).
![Page 7: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/7.jpg)
2. Save the function
Save the function in the directory of your choice,
Hit F5 to run if you wish, but knowing that it'll crash right after changing your directory.
7
• But keep the filename MATLAB gives you by default.
DO NOT CHANGE IT, UNDER ANY CIRCUMSTANCES.
![Page 8: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/8.jpg)
2. Create the Documentation
Create a function which receives 3 arguments (a height a mass and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.
88Must use single % only without skipping one!.
![Page 9: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/9.jpg)
2. Create the function-body
99
BMI Category depends on BMI value:Underweight <=18.5Normal weight = 18.5-24.9Overweight = 25-29.9Obesity = BMI of 30 or greater
![Page 10: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/10.jpg)
3. "Flow" of data
1010
1. Values will come from the arguments in the function-call.
![Page 11: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/11.jpg)
3. "Flow" of data
1111
2. Using only the given parameters, this code executes, thus solving for the return values.
One day, if needed, you may define new variables to compute intermediate steps.
Remember that all these variables are deleted when the function is done with its job.
![Page 12: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/12.jpg)
3. "Flow" of data
1212
3. MATLAB returns the values.
![Page 13: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/13.jpg)
Make sure you have all this typed correctly.
13
![Page 14: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/14.jpg)
3. Testing - Experiment
Test the help!
14
Error from hitting F5
Then, testing the documentation
![Page 15: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/15.jpg)
3. Testing - Experiment
This is the ‘experimental’ stage!
15
Hardcoded arguments just to test!
The function returns two VALUES.
>>>>>> Remember to create two VARIABLES capable of holding those two values.
![Page 16: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/16.jpg)
1616
>>[x,y]=bmiCalculator(1.65,55,'metric')
1. PASS
2. USE and CALCULATE
3. STORE RESULTS IN RETURN VARIABLES
4. RETURN
5. COLLECT
"Flow" of
data
![Page 17: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/17.jpg)
17
4. Testing - Main file
There will now be 2 files in the current directory:
The algorithm is:
% prompt unit system% prompt height and weight% calculate bmi and status% display both
![Page 18: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/18.jpg)
18
4. Code1 – old fashion
1. Ask for inputs.
2. Pass arguments to the function.
4. Show results.
3. Collect the return values.
![Page 19: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/19.jpg)
4. Hit F5 to run
This is a regular script file, so hitting F5 is valid.
19
![Page 20: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/20.jpg)
4. Code 2 – dialog boxes
questdlg() inputdlg() msgbox()
20
![Page 21: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/21.jpg)
21
Don't hesitate to practice this code.It's fun!
This is the function-call.55 1.65
![Page 22: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/22.jpg)
Almost Wrapping Up
Meet with the client, give the software, go on vacations…
But another client just walks in, having heard of the bmiCalculator… His request is slightly different: "I just care about the category, not the actual value of the bmi.
Can you help me?"
22
![Page 23: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/23.jpg)
5. Ignoring Return Values
YES! DO NOT DELETE ANYTHING FROM THE FUNCTION. There is a way to ignore return-values.
23
![Page 24: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/24.jpg)
5. Ignoring Return Values
Not this way…
24
MATLAB ignored the 2nd return value, and stored the first one in y.
![Page 25: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/25.jpg)
25
5. Ignoring Return Values
Suppose a stack of books What if you want the 3rd
book (the yellow one) from the top?
You must collect the 1st and 2nd (red and green) before collecting the 3rd (yellow).
It is exactly the same with the return-values.• They come out in order.• To access the 3rd return value the code must collect the 1st and
2nd.
![Page 26: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/26.jpg)
5. Ignoring Return Values
>>>>>>> Every variable UP TO the one wanted must be collected in a variable. (not the ones after)
Trick: name the variable trash, dummy, ignore, a word that obviously reflects that it is not-to-be used later.
You may even delete that variable specifically using clear26
Try it! Look at the Workspace.
![Page 27: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/27.jpg)
27
5. Ignoring Return Values Use the 'tilde' operator
The ~ indicates "there is no need to store it". COMMA IS MANDATORY.
SAME function, DIFFERENT call. Yay!
MATLAB version 7.9 (R2009b) and up ONLY
![Page 28: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/28.jpg)
28
Previous version of MATLAB Previous versions of MATLAB do not allow the use
of the ~ symbol.
Use a dummy variable name to store the data. The name reminds YOU (programmer) that this variable is of no use later on, so choose wisely!
It is a MUST to collect all the return data to the left of the one wanted.
![Page 29: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/29.jpg)
29
Option 2 cont.
Press F5 on the previous script file to see results in the Command Window.
![Page 30: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,](https://reader035.vdocuments.net/reader035/viewer/2022070407/56649e355503460f94b24b95/html5/thumbnails/30.jpg)
Wrapping Up
Go type this example step-by-step and make it work. At most, this should take 15 minutes.
In order:
1. Create a function file.
2. Save it and change to the directory where it is.
3. Test and experiment in the command window
4. Create the main file afterwards
Only practice makes perfect, and the more functions you create, the easier codes will get:
INCLUDING the final project!30