Download presentation
Presentation is loading. Please wait.
1
Procedure Activations
Chapter 5 CS 331
2
Procedures: Basic Terminology
Procedures take zero or more arguments, and then do something e.g printf(“Hello”), exit, Functions take zero or more arguments, and return a value of a given type e.g. sqrt(2.), random(), fopen(“foo”) Procedures and functions extend the language (like assembler macros?)
3
Parameters When declared, a procedure or function is given zero or more formal parameters usually a procedure has a certain fixed number of parameters in C, varargs is used to get around this, e.g. for printf When the procedure is invoked, the arguments are used
4
Arguments and Parameters
The formal parameter of foo is x The argument of foo in line 5 is 2 In C, everything is a function :-( float foo(x: int) { return sqrt(x); } main(int argc, char **argv) { printf(“%f”, foo(2));
5
Parameter Passing Call by value
used in C Call by reference (a.k.a. call by address) available in Pascal et al procedure foo(var J: integer) (* J might be changed *) Call by value-result like call by reference, except parameter can’t change until the procedure ends Call by name (historical interest only)
6
Activation Records Each invocation of a procedure (unless it’s inline) is associated with an in-memory data structure that indicates Where the local variables are The state of the call stack, i.e. to where should control return after this invocation ends? Prologue and epilogue code is generated to manage local storage, and do copying of parameters
7
Lexical vs. Dynamic Scope
In lexical scope, identifiers are declared in accordance to nesting in the program text Can be determined unambiguously at compile-time In dynamic scope, identifiers are declared in accordance to when the declarations are encountered during execution Discuss example on pages
8
Activation Trees Each node represents an activation of a procedure
Nodes are listed left to right in order of invocation Nodes can be “decorated” with values of parameters, local variables, etc. Exercise 5.2 a,b,c due Tuesday 3/29 Ignore tail-recursion elimination & quicksort
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.