Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.
-
Upload
diana-davidson -
Category
Documents
-
view
216 -
download
1
Transcript of Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.
![Page 1: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/1.jpg)
Advanced Functions
Lecture 14: Supporting Material
Dr Kathryn Merrick
Tuesday 21st April, 2009
![Page 2: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/2.jpg)
![Page 3: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/3.jpg)
Overview
What happens during a function call
The workspace
The stack
Variable scope and modifiers
Formal and actual parameters
Ref: Text book Ch 7.
![Page 4: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/4.jpg)
A function of motion…
![Page 5: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/5.jpg)
Revision: Function Headers
function [distance, acceleration, avg_velocity] = simple_motion_calculator(init_velocity, final_velocity, time)
Function key word
Output or return values
Function name
Input parameters or arguments
![Page 6: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/6.jpg)
Revision: Calling a Function
[dist, acc, avg_vel] = simple_motion_calculator(10, 0, 5);
But what really happens during this function call…?
![Page 7: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/7.jpg)
Demo and Diagram 1:
Simple motion calculator
What happens during a function call?
![Page 8: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/8.jpg)
Formal Versus Actual Parameters
Formal parameters: The names given in the function header
Actual parameters: The values passed when the function is called
![Page 9: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/9.jpg)
Example: Formal and Actual Parameters
function [distance, acceleration, avg_velocity] = simple_motion_calculator(init_velocity, final_velocity, time)
[dist, acc, avg_vel] = simple_motion_calculator(10, 0, 5);
Formal Inputs
Actual Inputs
Formal Outputs
Actual Outputs
Call
Function Header
![Page 10: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/10.jpg)
Mapping Formal and Actual Parameters
Formal
distance
acceleration
avg_velocity
init_velocity
final_velocity
time
Actual
dist
acc
avg_vel
10
0
5
![Page 11: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/11.jpg)
Parameter Passing
Pass-by-value: The actual parameter is copied and the copy
passed to the function
Pass-by-reference: A pointer to the actual parameter is passed to
the function
MATLAB: Uses a combination of these approaches
![Page 12: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/12.jpg)
Demo and Diagram 2:
Meteor Simulator Revisited
Variables and Scope
![Page 13: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/13.jpg)
Variable Scope
Global variables: Always ‘in scope’
Local variables: Only in scope inside the function in which they
are declared
Persistent variables: Local scope, but value is saved (persists)
between function calls
![Page 14: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.](https://reader030.fdocuments.net/reader030/viewer/2022032708/56649e7f5503460f94b840d0/html5/thumbnails/14.jpg)
Summary
After today’s lecture you should be able to explain:
What happens in memory during a function call, in terms of workspaces and the stack
Variable scope for global, local and persistent variables
The difference between formal and actual parameters
The difference between pass-by-value and pass-by-reference