Parameter Passing. Standard mechanisms. Call by value-result Call by name, result



Similar documents
Parameter Passing Methods

Class 16: Function Parameters and Polymorphism

Parameter passing in LISP

Chapter 5 Names, Bindings, Type Checking, and Scopes

Passing Arguments. A comparison among programming languages. Curtis Bright. April 20, 2011

Comp151. Definitions & Declarations

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

An Incomplete C++ Primer. University of Wyoming MA 5310

Java Interview Questions and Answers

The C Programming Language course syllabus associate level

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Goals for This Lecture:

Chapter 9. Subprograms

Introduction to ROOT

History OOP languages Year Language 1967 Simula Smalltalk

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

C++ program structure Variable Declarations

CS 106 Introduction to Computer Science I

Keil C51 Cross Compiler

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

6.s096. Introduction to C and C++

C++ INTERVIEW QUESTIONS

Functions and Parameter Passing

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Illustration 1: Diagram of program function and data flow

Habanero Extreme Scale Software Research Project

Chapter 13 Storage classes

Object Oriented Software Design II

A Memory Model for Static Analysis of C Programs

Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.

Chapter 7: Additional Topics

sys socketcall: Network systems calls on Linux

Chao Chen 1 Michael Lang 2 Yong Chen 1. IEEE BigData, Department of Computer Science Texas Tech University

Programmierpraktikum

Stack Allocation. Run-Time Data Structures. Static Structures

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Linux Driver Devices. Why, When, Which, How?

CUDA Debugging. GPGPU Workshop, August Sandra Wienke Center for Computing and Communication, RWTH Aachen University

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Object Oriented Software Design II

Qt Signals and Slots. Olivier Goffart. October 2013

Reach 4 million Unity developers

Distributed objects and object-based middleware

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Programming languages C

Object Oriented Software Design

High-Level Language. Building a Modern Computer From First Principles.

System Calls and Standard I/O

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

A deeper look at Inline functions

Object Oriented Software Design

Mutual Exclusion using Monitors

arrays C Programming Language - Arrays

COMPARISON OF OBJECT-ORIENTED AND PROCEDURE-BASED COMPUTER LANGUAGES: CASE STUDY OF C++ PROGRAMMING

MISRA-C:2012 Standards Model Summary for C / C++

CS193j, Stanford Handout #4. Java 2

Elemental functions: Writing data-parallel code in C/C++ using Intel Cilk Plus

Aspect Oriented Programming. with. Spring

The programming language C. sws1 1

1 Abstract Data Types Information Hiding

CSE 130 Programming Language Principles & Paradigms

Compiler Construction

Object Oriented Software Design II

Lecture 5: Java Fundamentals III

On the (un)suitability of Java to be the first programming language

กระบวนการเทคโนโลย สารสนเทศ ค อ การจ ดการข อม ลและสารสนเทศโดยใช เทคโนโลย คอมพ วเตอร และเทคโนโลย การส อสารโทรคมนาคม ซ งม ข นตอนท งหมด 6 ข นตอน ด งน

AP Computer Science Java Subset

Programming Languages

Lecture 9. Semantic Analysis Scoping and Symbol Table

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Your first-class upgrade to web development

U N C L A S S I F I E D

Floating Point C Compiler: Tips and Tricks Part I

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Parallelization: Binary Tree Traversal

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

The aim of this unit is to show other possibilities of MATLAB. There are no Exercises associated with this unit.

CS143 Handout 18 Summer 2012 July 16 th, 2012 Semantic Analysis

1 External Model Access

Objective-C Tutorial

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Data Structures and Algorithms

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CmpSci 187: Programming with Data Structures Spring 2015

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

UNIX Sockets. COS 461 Precept 1

Lecture 22: C Programming 4 Embedded Systems

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Sources: On the Web: Slides will be available on:

In this Chapter you ll learn:

Transcription:

Parameter Passing Standard mechanisms Call by value Call by reference Other methods Call by value-result Call by name, result Terms Function definition where the details of the function are presented (type, name, parameters, body) - only one Function call where the function is invoked (name, arguments) zero or more (not interesting if no calls) Parameters (formal parameters) names of local variables in function that are given values during call Local variables variables in function body that are not parameters Arguments (actual parameters) values provided for parameters

Terms Parameter passing method(s) used to determine how argument values relate to parameters Overloading when the same function name can have more than one set of parameters L-value location of variable R-value contents of variable (or result of expression) Terms Example Function definition int func1 (int a, char b, float& c) { int x, char y; Function call Parameters Local variables func1(5 * 3, A, z); Arguments

Call by Value Calling mechanism Arguments are evaluated for their values Local variables created for each parameter Values resulting from arguments copied to new parameter variables When function call ends, parameter variables are discarded During function execution, value of parameters may diverge from argument values (function does not affect arguments) Call by Value Call by Value used in C Most C++ parameters Variables are changed in functions only indirectly Pointer values are passed to functions Variables that the pointer point at may be changed in a function Characteristics: Variables may not directly be changed in function body (but changes in function do not change the values of arguments) Arguments can be complex expressions Mechanism is simple (easy to explain)

Call by Reference Calling mechanism Variable locations for arguments determined Parameter names added to the location for each argument When function call ends, extra names are discarded During function call, changes to referenced variables persist even after function ends Call by Reference Call by Reference used Pascal (var parameters) C++ (& parameters) Some versions of FORTRAN Characteristics: Changes to parameters change corresponding argument variables Arguments must be variables (cannot connect a reference to an expression)

Parameter Passing Example Call by Value int x = 1; // global x void func1 (int a) { // Location 2 x = 2; // Location 3 a = 5; // Location 4 Location 1: a undefined, x is 1 Location 2: a is 1, x is 1 Location 3: a is 1, x is 2 Location 4: a is 5, x is 2 void main () { // Location 1 func1(x); // Location 5 Location 5: x is 2 Parameter Passing Example Call by Reference int x = 1; // global x void func1 (int a) { // Location 2 x = 2; // Location 3 a = 5; // Location 4 Location 1: a undefined, x is 1 Location 2: a is 1, x is 1 Location 3: a is 2, x is 2 Location 4: a is 5, x is 5 void main () { // Location 1 func1(x); // Location 5 Location 5: x is 5

Call by Value-Result Calling mechanism Arguments are evaluated for their values Local variables created for each parameter Values resulting from arguments copied to new parameter variables When function call ends, values from parameters copied back to calling variables Operates somewhat like Call by Reference but differs under certain circumstances Used in some versions of FORTRAN, inout params in CORBA Parameter Passing Call by Value-Result int x = 1; // global x void func1 (int a) { // Location 2 a = 3; // Location 3 x = 4; // Location 4 Location 1: a undefined, x is 1 Location 2: a is 1, x is 1 Location 3: a is 3, x is 2 Location 4: a is 3, x is 4 void main () { // Location 1 func1(x); // Location 5 Location 5: x is 3

Other Mechanisms Call by name Used in Algol Functions are a bit like a complex macro, the argument values replace the corresponding parameter names in the function body and then the body executed Call by result Value of parameter copied back to argument at end of function Out params in CORBA What About Java? Everything is Call by Value What about when we pass objects? An object variable in Java always holds a reference (pointer) to an instance in Java When called a copy of the reference (or l- value) is made Changes to the object pointed at can be made But the pointer to the object can then also be changed