Download presentation
Presentation is loading. Please wait.
Published byDiana Davidson Modified over 9 years ago
1
Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009
3
Overview What happens during a function call The workspace The stack Variable scope and modifiers Formal and actual parameters Ref: Text book Ch 7.
4
A function of motion…
5
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
6
Revision: Calling a Function [dist, acc, avg_vel] = simple_motion_calculator(10, 0, 5); But what really happens during this function call…?
7
Demo and Diagram 1: Simple motion calculator What happens during a function call?
8
Formal Versus Actual Parameters Formal parameters: The names given in the function header Actual parameters: The values passed when the function is called
9
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
10
Mapping Formal and Actual Parameters Formal distance acceleration avg_velocity init_velocity final_velocity time Actual dist acc avg_vel 10 0 5
11
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
12
Demo and Diagram 2: Meteor Simulator Revisited Variables and Scope
13
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
14
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
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.