Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.

14
Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009

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.

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.
Page 3: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.

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.

A function of motion…

Page 5: Advanced Functions Lecture 14: Supporting Material Dr Kathryn Merrick Tuesday 21 st April, 2009.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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