functions 1 parameter, 2 return-values

29
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1

Upload: alaric

Post on 15-Feb-2016

43 views

Category:

Documents


1 download

DESCRIPTION

Functions 1 parameter, 2 return-values. "Conversion of time format" One problem. 5 steps to solve it. Convert Time. Step1. Problem: Given a time as a whole amount of seconds, convert it to hr:min:sc format using the method shown hereafter. Convert Time. Step2. myTime = 32512 seconds - PowerPoint PPT Presentation

TRANSCRIPT

Page 3: Functions 1 parameter, 2 return-values

Convert Time. Step2.myTime = 32512 seconds

To calculate the hours, divide by 3600 (seconds/hr)

Find the minutes by converting the 0.0311 (hr) to minutes:

Find the seconds by converting the 0.8667 (min) to seconds:

3

32512 (sec)

3600 (sec/hr)= 9.0311 (hr) = 9 full hr

0.0311 (hr) * 60 (min/hr) = 1.8667 (min) = 1 full minute

0.8667(min) * 60 (sec/min) = 52 (sec)

Page 5: Functions 1 parameter, 2 return-values

Convert Time. Step 4a. Algorithm

%prompt user for number of full seconds%divide by 3600 to get the hours%separate whole part and decimal part%multiply the decimal part by 60 to get the minutes%separate whole part and decimal part%multiply that decimal part by 60 to get the seconds%display result

5

Code doing the same operation twice, on a different number, but SAME CODE. Let's create a function!

Page 9: Functions 1 parameter, 2 return-values

Function Definition

9

When more than one return value are present, they MUST be in square brackets.

Important: MATLAB does not return a vector with 2 values. This is just how MATLAB wants us to indicate multiple return values. It does return 2 separate values.

Page 19: Functions 1 parameter, 2 return-values

Step5. Testing. Once again, jump to the Command Window to

experiment:(Imagine you were calling a built-in function.. It works the

same way as with any other keyword, except you created the keyword separate())

19

Two separate scalars, NO VECTOR, even if we use [ ].

Page 20: Functions 1 parameter, 2 return-values

Step5. Testing (MISTAKE) Remember that MATLAB only stores 1 value by default,

and uses the default variable name ans. This is the same, whether it is a built-in or programmer

defined function.

20

MATLAB does get two values returned by the function, BUT there are no variables COLLECTING them. MATLBA uses the default ans for the first one, but ignores the second return-value since it does not have a 2nd default variable name…

Page 23: Functions 1 parameter, 2 return-values

Step.4b. Main File%prompt user for number of full seconds%divide by 3600 to get the hours%separate whole part and decimal part%multiply the decimal part by 60 to get the minutes%separate whole part and decimal part%multiply that decimal part by 60 to get the seconds%display result

23

Time to create the main script, and actually solve our conversion problem.

REMEMBER: keep the main script file and the function definition in the same folder.

Page 28: Functions 1 parameter, 2 return-values

Use the function in another project! BankOfAmerica ® has a program called "keep the

change". When a transaction is made on the debit card, the

amount is rounded up to the nearest whole amount. The difference is automatically deposited in a savings account.

For example: transaction = $4.65. $5 is debited from the checking account $1 - $0.65 = $0.35 is credited to the savings account

Create a main script file that uses separate() to solve and indicate how much is placed in the savings account.

28

Page 29: Functions 1 parameter, 2 return-values

Wrapping Up Please make sure you typed this example as we went along. Returning two or more return-values makes us use [ ]. Do not use [ ]

when there is only 1 value returned. It clutters the screen, and has no advantage.

MATLAB does not return an array, it is just the notation that requires the symbols.

CAUTION: the order of the variables matter. Be courageous not to name all the variables the same everywhere.

Parameters are the variables inside the parentheses IN the function definition.

Arguments are the variables inside the parentheses IN the function call statement.

29