Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. 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 along 1. Ignoring return values 1
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.5 Normal weight = Overweight = Obesity = BMI of 30 or greater
Step2. I/O of the function 3 parameters, 2 return-values 3 bmiCalculator() Unit system Bmi value weight height Bmi category
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 [ ].
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
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 ( ).
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.
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. 88 Must use single % only without skipping one!.
2. Create the function-body 99 BMI Category depends on BMI value: Underweight <=18.5 Normal weight = Overweight = Obesity = BMI of 30 or greater
3. "Flow" of data Values will come from the arguments in the function-call.
3. "Flow" of data 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.
3. "Flow" of data MATLAB returns the values.
Make sure you have all this typed correctly. 13
3. Testing - Experiment Test the help! 14 Error from hitting F5 Then, testing the documentation
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.
16 >>[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
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
18 4. Code1 – old fashion 1. Ask for inputs. 2. Pass arguments to the function. 4. Show results. 3. Collect the return values.
4. Hit F5 to run This is a regular script file, so hitting F5 is valid. 19
4. Code 2 – dialog boxes questdlg() inputdlg() msgbox() 20
21 Don't hesitate to practice this code. It's fun! This is the function- call
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
5. Ignoring Return Values YES! DO NOT DELETE ANYTHING FROM THE FUNCTION. There is a way to ignore return-values. 23
5. Ignoring Return Values Not this way… 24 MATLAB ignored the 2 nd return value, and stored the first one in y.
25 5. Ignoring Return Values Suppose a stack of books What if you want the 3 rd book (the yellow one) from the top? You must collect the 1 st and 2 nd (red and green) before collecting the 3 rd (yellow). It is exactly the same with the return-values. They come out in order. To access the 3 rd return value the code must collect the 1 st and 2 nd.
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 clear 26 Try it! Look at the Workspace.
5. Ignoring Return Values Use the 'tilde' operator 27 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
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.
29 Option 2 cont. Press F5 on the previous script file to see results in the Command Window.
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