Functions 1 parameter 2 return values
This presentation is the property of its rightful owner.
Sponsored Links
1 / 29

Functions 1 parameter, 2 return-values PowerPoint PPT Presentation


  • 71 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Functions 1 parameter, 2 return-values

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Functions 1 parameter 2 return values

Functions1 parameter, 2 return-values

"Conversion of time format"

One problem. 5 steps to solve it.


Convert time step1

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

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:

32512 (sec)

= 9.0311 (hr)

= 9 full hr

3600 (sec/hr)

0.0311 (hr) * 60 (min/hr)

= 1.8667 (min)

= 1 full minute

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


Convert time step3

Convert Time. Step3.

hrs

Keyboard

** MAGIC **

Screen

Full time

min

sc


Convert time step 4a

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

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


Overall flow

Overall Flow

separate()

CALL it once to solve for the minutes.

Main script file

CALL it once to solve for the seconds.


I o of the function

I/O of the function

  • One parameter, 2 return-values

One value

separate()

The whole part

The decimal part


Step 4b function definition

Step.4b. Function Definition

  • Open a new script file, and type the following:

The keyword function tells Matlab this is a function definition.


Function definition

Function Definition

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.


Function definition1

Function Definition

The parameter goes on the right, in between ()


Function definition2

Function Definition

The documentation shows how to use the function, what it does, and any assumptions made.


Saving the function definition

Saving the function definition

SAVE THE FUNCTION.

MATLAB offers automatically the correct name, i.e. the name of the function itself.

Confirm by clicking Save.


Function definition3

Function Definition

What would be the code if this was before you learned what a function was?

Suppose: value = 2.45

whole = ?

decimal = ?


Function definition4

Function Definition


Function definition5

Function Definition

2.45

2

.45

2.45 - 2


Function definition6

Function Definition

2.45

2

.45


Function definition7

Function Definition

9.0311

??

??

Same code, different input. 


Function definition done

Function definition: done!

  • Function: done!

  • But before we call our boss and colleague engineers to put the main script file together, we need to test!


Step5 testing

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())

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


Step5 testing mistake

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.

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…


Step5 testing1

Step5. Testing

  • Note you can also test the documentation!!! (and should)

    >> doc separate <enter>


Back to step4b

Back to step4b.

  • Now that the function works, your boss/colleagues/you may put together the main script file.


Step 4b main file

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

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.


Functions 1 parameter 2 return values

Write the FUNCTION CALLS. These lines "call upon the execution" of the code inside the definition, with various inputs each time.

1st call

2nd call


The 1 st call converts the hrs

The 1st call converts the hrs

whole = floor(value);

decimal = value-whole;


The 2 nd call converts the min

The 2nd call converts the min

  • Same function: different ARGUMENT

whole = floor(value);

decimal = value-whole;


Step5 testing2

Step5. Testing

  • F5 yields:

  • 5.200000e+001 is the scientific notation for 52.

  • Due to round off issues, the final number of seconds has round off issues.

  •  Call the client to figure out how s/he wants this handled!


Use the function in another project

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.


Wrapping up

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.


  • Login