procedures - CSC 324: Principles of Programming Languages...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSC 324: Principles of Programming Languages Procedural Language Design Issues READING: Sebesta 4.3,4.4,4.84.10; 8.18.5,8.11; 9.19.5 c circlecopyrt Suzanne Stevenson 2001 1 Part VI: Procedural Language Design Issues Procedures: A Control Abstraction A block of code that can be called (imperative) A lambda expression (functional) A horn clause (logic programming) Procedures modularize program structure 2 Components of a Procedure 1. Name 2. Formal parameters, optionally with types parameter (formal parameter) Local variable whose value is received from caller argument (actual parameter) The info passed from caller to callee 3. Body, which is a syntactic construct in the language: Block, i.e., declarations and statements Expression Conjunction of terms 4. Optional result, optionally with a type 3 Procedure Implementation Issues The general notion of a procedure leaves a number of points unspecified: How to pass parameters when the procedure is called How to maintain local state and control information How to access non-local names within a procedure body 4 Parameter Passing Matching arguments with parameters: 1. Positional association: Arguments are associated with parameters left to right 2. Keyword association: Arguments are given tags, eg: procedure plot (x,y: real; penup: boolean) ... plot(0.0, 0.0, penup=>true) plot(penup=>true, x=>0.0, y=>0.0) 5 Parameter Passing 3. Optional arguments: E.g., C printf(. . . ) Extra arguments are packaged into some structure Passed to special parameter 6 Passing Modes How to treat arguments (pass-by-x/call-by-x): 1. Pass by value (Java, C, C++, Pascal, Ada, Scheme, Algol68) 2. Pass by result (Ada) 3. Pass by value-result (some Fortrans, Ada) 4. Pass by reference (Java objects, C++ with & , some Fortrans, Pascal with var , COBOL) 5. Pass by name (Algol 60) 7 Example for Passing Modes { c : array[1..10] of integer; m,n integer; procedure r (i , j : integer ) begin i := i + 1; j := j + 2 end r; ... m := 2; n := 3; r(m,n); // call 1 write m, n ; // print 1 m := 2; c[1] := 1; c[2] := 4; c[3] := 8; r(m,c[m]); // call 2 write m,c[1],c[2],c[3]; // print 2 } 8 Pass by Value Initial values of parameters copied from current values of arguments Final values of parameters are lost at return time (like local variables). Example: at call 1: i = 2 j = 3 print 1: at call 2: i = 2 j = 4 print 2: Benefit : Arguments protected from changes in procedure. Problem : Requires copying of values: costs time and space, especially for large aggregates. 9 Pass by Result No initial values of parameters Final values of parameters are copied back to arguments Example: does not work, as written For output values only. Used to indicate that a parameter is intended solely for returning a result....
View Full Document

This note was uploaded on 02/05/2011 for the course CS 24 taught by Professor Cathy during the Summer '10 term at University of Toronto.

Page1 / 50

procedures - CSC 324: Principles of Programming Languages...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online