Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.

Slides:



Advertisements
Similar presentations
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Advertisements

Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Procedures and Control Flow CS351 – Programming Paradigms.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Chapter 6: User-Defined Functions I
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Run time vs. Compile time
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Chapter 6: User-Defined Functions I
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS 2104 Prog. Lang. Concepts Subprograms
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Programming Languages and Paradigms Imperative Programming.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 13 Thanks for lecture slides: Prentice Hall, Inc., 2. C++
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
7. Runtime Environments Zhang Zhizheng
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
ISBN Chapter 10 Implementing Subprograms.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Dr. Philip Cannata 1 Functions and Recursion Programming Languages.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
CS314 – Section 5 Recitation 9
Run-Time Environments Chapter 7
Functions.
Chapter 6: User-Defined Functions I
Principles of programming languages 8: Types
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Principles of programming languages 4: Parameter passing, Scope rules
User-Defined Functions
CMPE 152: Compiler Design October 2 Class Meeting
CSC 533: Programming Languages Spring 2015
Subroutines – parameter passing
Chapter 6: User-Defined Functions I
UNIT V Run Time Environments.
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design March 7 Class Meeting
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano

How do we get to values from variables? In imperative languages variables represent locations (via their declarations). By accessing the locations we get the values. How we relate an occurrence of a variable, its declaration, and its location is specified by the scope rule and the parameter passing method of the language. Parameter passing methods call by value, call by reference, call by name Correspondence between an occurrence of a variable and its declaration Static scope, dynamic scope

Procedures Procedure is a construct for giving a name to a piece of code. The piece is referred to as the procedure body. When the name is called, the body is executed. Each execution of the body is called an activation. Function is a procedure that returns a value. People may not distinguish between procedures and functions.

Procedure calls and function calls A function call is an expression, while a (non- function) procedure call is a statement. (ex.) r * sin (angle) sin (angle) is an function call expression. It can appear anywhere an expression can appear as far as the syntax is concerned. (ex.) read (ch) read (ch) is a procedure call statement. It can appear anywhere a statement can appear as far as the syntax is concerned.

Syntax of procedure calls Syntax of procedure (or function) calls (in prefix notation) ( ) The parameters are called actual parameters. (ex.) angle is an actual parameter in the function call sin (angle). (ex.) ch is an actual parameter in the procedure call read (ch). In procedure calls, parentheses are usually necessary even if there is no parameters (in C, Modula-2, Java, etc.). In Pascal we do not write parentheses if there is no parameters. (ex.) begin while eoln do readln; read(ch) end Eoln is a function call expression, readln is a procedure call statement, and read(ch) is a procedure call statement.

Syntax of procedure declarations Procedure (or function) declarations consists of the following. Name of the procedure Names and types of parameters --- The parameters are called formal parameters. Result type (in function declarations) Local declarations and statements Designers of languages freely define the syntax of procedure declarations as far as the above four are clear.

An procedure declaration in Pascal procedure getch; begin read (ch) end; The name of the above procedure is getch. It has no parameters.

A function declaration in Pascal The name of the function is f. The parameter has the type of integer and the result type is integer. In Pascal, the assignment f:= …determines the return value of the function. Usual languages like C, Modula-2, Java, etc. provide return statements. function f (x : integer) : integer; var square : integer; begin square := x * x; f := square + 1 end;

Recursive functions function f (n : integer) : integer; begin if n = 0 then f := 1 else f := n * f (n-1); end; The function f computes the factorial of the given parameter. For example, the computation of f(3) can be illustrated as follows. f (3) = 3 * f (2) = 6 f (2) = 2 * f (1) = 2 f (1) = 1 * f (0) = 1 f (0) = 1

Parameter passing methods function square (x : integer) : integer; begin square := x * x end; In the function x is a parameter. For example, the value of a function call expression square(2)is 4, which is obtained by evaluating x * x in the state where 2 is assigned to x.The situation is simple when the actual parameter is a number, but there are various ways when the actual parameter is a variable or an element of an array.

Parameter passing methods There are three major ways for parameter passing. Call-by-value Call-by-reference Call-by-name

Call by value The value of actual parameters are passed to the corresponding formal parameters. Suppose a procedure (or function) p has a formal parameter x. The execution (or evaluation) of procedure call p(e) is performed as follows. (1) x := e (2) execute the body of the procedure p (3) return the result (when p is a function). (Note) When the variable x is also declared in the caller, the formal parameter x is different from that. (ex.) When evaluating square (2+3), x := is firstly performed, thus 5 is assigned to the formal parameter x. Then x * x is evaluated to 25, which is the value of the function call expression square (2+3).

An example that does not work procedure nget (c : char); begin read (c) end; When this procedure is called, a keybord input is assinged to the parameter c. When executing nget (ch), the value of ch is not affected.

Another example that does not work procedure swap (x : integer; y : integer); var z : integer; begin z := x; x := y; y := z end; Executing the procedure call swap (a,b) does not change the value of variables a and b. The values of a and b are assigned to the formal parameters x and y respectively and the the value of x and y are swapped, so the values of a and b are not affected.

Call by reference A formal parameter becomes a synonym for the actual parameter. (The location of the formal parameter becomes the location of the actual parameter. ) Pascal has call by value and call by reference. procedure p (x : integer; var y : integer); … Formal parameters with var is call by reference and ones without var is call by value. In the above example, the second argument of p must be an expression that has some location (i.e., that can appear in the LHS of an assignment), such as a variable or an element of array.

Procedure for swapping in Pascal procedure swap (var x : integer; var y : integer); var z : integer; begin z := x; x := y; y := z end; In swap, x and y are call by reference. For example, execution of swap (i, A[i]) is performed as follows. (1) Make the location of x same as that of i. (2) Make the location of y same as that of A[i]. (3) z := x; x := y; y := z Suppose the value of i is 2, the value of A[2] is 99. Then the execution of the procedure call is effectively same as the execution of z := 2; i := 99; A[2] = z, so the values of i and A[2] are swapped.

Exercise program test; var x : integer; var y : integer; procedure swap (var x: integer; var y : integer); var z : integer; begin z := x; x := y; y := z end; begin x := 3; y := 4; swap (x,y); writeln (x); writeln (y) end. Show the result (display output) when executing the following Pascal program. The procedure writeln prints the value of the parameter and a newline character.

About the language C The language C supports only the call by value as parameter passing. Instead, C provides pointers so that we can simulate call by reference by passing pointers to functions as their parameters. void swap (int * px, int * py) { int z; z = *px; *px = *py; *py = z; } The following program fragment swaps the values of variables a and b. int a = 1, b = 2; swap (&a, &b);

Call by name Actual parameters are textually substituted for the formal parameters. Name conflicts are avoided by renaming the local variables in the procedure body. Algol60 is call by name by default. program {computation of inner product} var i, n, z : integer; a, b : array [0..9] of integer; procedure sum (x : integer); begin while i < n do begin z := z + x; i := i + 1 end end; begin n := 10; i := 0; z := 0; sum (a[i] * b[i]); writeln (z) end. Arguments are evaluated when necessary. It is called lazy evaluation. 。 Lazy evaluation is call by name or call by need (an argument is evaluated once). If there is no side-effect, these two strategies yields the same result.

(supplement) Call-by-value-result In Call-by-value-result (also called as copy-in/copy-out), the actual parameters are initially copied into the formals and the locations of the actuals are computed and saved. The final values of the formals are copied back out to the saved locations. (ex.) program {a subtle example} i, j : integer; procedure foo (x, y : integer); begin i := y end; begin i := 2; j := 3; foo (i, j) end In call-by-reference the value of i would become 3, while in call-by-value-result the value of i (and j) would not be changed. Ada has three ways, in, out, and in out, for parameter passing. in out is for call-by-value-result.

Scope rules The scope rules of a language determine the correspondence between an occurrence of a name (such as variables, types, procedures, etc.) and its declaration. In static scope (also called as lexical scope), scopes of names can be determined statically (i.e., in compile- time). In dynamic scope, the binding of name occurrences to declarations is done dynamically (i.e., at run time).

An example program L; var n : char; procedure W; begin writeln(n) end; procedure D; var n : char; begin n := ‘D’; W end; {body of the program L} begin n := ‘L’; W; D end.

Static scope In static scope, intuitively, an occurrence of a name x corresponds to the inner-most declaration of x. Almost all languages like Pascal and C are static scope. In static scope, the previous example would result in L Principle: Consistent renaming of local names in the source program should have no effect on the computation. By following the principle, the scope rule becomes static scope.

Dynamic scope Under dynamic scope, an occurrence of a name is bound to a declaration at run time. In dynamic scope, the previous example would result in L D Emacs lisp uses dynamic scope. (Idea) In dynamic scope, an occurrence of a name x corresponds to the x that is located in the closest activation record in the stack.