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.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

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.
Parameter Passing CMSC 331 Extracted with only very minor changes from
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
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.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Parameters Chapter EighteenModern Programming Languages, 2nd ed.1 Spring 2012.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
CPSC 388 – Compiler Design and Construction Parameter Passing.
1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.
Advanced Programming Parameter Passing Giuseppe Attardi Università di Pisa.
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 :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Parameters Chapter EighteenModern Programming Languages, 2nd ed.1.
Chapter EighteenModern Programming Languages1 Parameters.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
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.
C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 06 Parameters.
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.
Overview Parameter passing modes.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
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,
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Chapter 8 :: Subroutines and Control Abstraction
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
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,
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
Basic Semantics Associating meaning with language entities.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Subprograms subroutines, procedures, functions, methods.
Programming Languages and Paradigms Imperative Programming.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
1 Subroutines Structure of Programming Language. 2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
Chapter EighteenModern Programming Languages1 Parameters.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
ISBN Chapter 9 Subprograms. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
CSE 3302 Programming Languages
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
CS314 – Section 5 Recitation 9
Functions.
Structure of Programming Languages
Principles of programming languages 4: Parameter passing, Scope rules
Programming Fundamentals Lecture #7 Functions
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Parameter Passing (Ch 18)
CSC 533: Programming Languages Spring 2015
Parameter transmission
PZ09B - Parameter transmission
Parameter transmission
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Subprograms General concepts Parameter passing Functions
Parameter transmission
Presentation transcript:

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 of Programming Languages Reading: “Concepts in Programming Languages”, Chapter 7

slide 2 Procedural Abstraction uProcedure is a named parameterized scope Allows programmer to focus on a function interface, ignoring the details of how it is computed uValue-returning functions Example: x = (b*b - sqrt(4*a*c))/2*a uNon-value returning functions Called “procedures” (Ada), “subroutines” (Fortran), “void functions/methods” (C, C++, Java) Have a visible side effect: change the state of some data value not defined in the function definition Example: strcpy(s1,s2) Contains local variable declarations and statements Can pass arguments into the scope Can be overloaded (e.g., binary +)

slide 3 System Calls uOS procedures often return status codes Not the result of computing some function, but an indicator of whether the procedure succeeded or failed to cause a certain side effect

slide 4 Arguments and Parameters uArgument: expression that appears in a function call uParameter: identifier that appears in function declaration uParameter-argument matching by number and position Exception: Perl. Instead of being declared in a function header, parameters are available as elements of special int h, i; void B(int w) { int j, k; i = 2*w; w = w+1; } void A(int x, int y) { bool i, j; B(h); } int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); }

slide 5 Parameter Passing Mechanisms uBy value uBy reference uBy value-result uBy result uBy name

slide 6 Pass by Value uCaller passes r-value of the argument to function Compute the value of the argument at the time of the call and assign that value to the parameter Reduces “aliasing” –Aliasing: two names refer to the same memory location uFunction cannot change value of caller’s variable uAll arguments in C and Java are passed by value To allow caller’s variables to be modified, pointers can be passed as arguments –Example: void swap(int *a, int *b) { … } Is there a contradiction here?

slide 7 Pass by Reference uCaller passes l-value of the argument to function Compute the address of the argument and assign that address to the parameter Increases aliasing (why?) uFunction can modify caller’s variable via the address it received as argument int h, i; void B(int* w) { int j, k; i = 2*(*w); *w = *w+1; } void A(int* x, int* y) { bool i, j; B(&h); } int main() { int a, b; h = 5; a = 3; b = 2; A(&a, &b); }

slide 8 ML Example function f (x) = { x = x+1; return x; } var y = 0; print (f(y)+y); fun f (x : int ref) = ( x := !x+1; !x ); val y = ref 0 : int ref; f(y) + !y; fun f (z : int) = let val x = ref z in x := !x+1; !x end; val y = 0 : int ; f(y) + y; pseudo-codeStandard ML pass-by-ref pass-by-value

slide 9 Pass by Reference in C++ uSpecial “reference type” indicates that l-value is passed as argument Recall that in C, only r-values can be arguments u& operator is overloaded in C++ When applied to a variable, gives its l-value When applied to type name in parameter list, means pass the argument by reference l-values for C++ reference types are completely determined at compile-time (why is this important?)

slide 10 Two Ways To Pass By Reference void swap (int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int x=3, y=4; swap(&x, &y); C or C++ void swap (int& a, int& b) { int temp = a; a = b; b = temp; } int x=3, y=4; swap(x, y); C++ only Which one is better? Why?

slide 11 Pass by Value-Result uPass by value at the time of the call and/or copy the result back to the argument at the end of the call (copy-in-copy-out) Example: “in out” parameters in Ada uReference and value-result are the same, except when aliasing occurs: Same variable is passed for two different parameters Same variable is both passed and globally referenced from the called function

slide 12 Pass by Name uTextually substitute the argument for every instance of its corresponding parameter in the function body Originated with Algol 60 but dropped by Algol’s successors -- Pascal, Ada, Modula uExample of late binding Evaluation of the argument is delayed until its occurrence in the function body is actually executed Associated with lazy evaluation in functional languages (e.g., Haskell)

slide 13 Jensen’s Device uComputes in Algol 60 begin integer i; real procedure sum (i, lo, hi, term); value lo, hi; integer i, lo, hi; real term; begin real temp; temp := 0; for i := lo step 1 until hi do temp := temp + term; sum := temp end; print (sum (i, 1, 100, 1/i)) end passed by name becomes 1/i when sum is executed

slide 14 Macro uTextual substitution #define swap(a,b) temp=a; a=b; b=temp; … int x=3, y=4; int temp; swap(x,y); uLooks like a function definition, but … Does not obey the lexical scope rules (i.e., visibility of variable declarations) No type information for arguments or result Textually expands to temp=x; x=y; y=temp;

slide 15 Problems with Macro Expansion #define swap(a,b) temp=a; a=b; b=temp; … if (x<y) swap(x,y); Textually expands to if (x<y) temp=x; x=y; y=temp; Why not #define swap(a,b) { int temp=a; a=b; b=temp; }? Instead #define swap(a,b) do { int temp=a; a=b; b=temp; } while(false); Fixes type of swapped variables