1 / 8

Procedure Activations

Procedure Activations. Chapter 5 CS 331. 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”)

gwyn
Download Presentation

Procedure Activations

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 161-162

  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

More Related