Passing Parameters. Different Techniques Pass by Value Pass by Reference Pass by Result Pass by Value-Result Pass by Name.

Slides:



Advertisements
Similar presentations
C Language.
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.
ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Overview Fundamentals of Subprograms (Sec )
CPSC 388 – Compiler Design and Construction Parameter Passing.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
ISBN Chapter 6 Data Types: Structured types.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Multiple-Subscripted Array
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
LAB-12 2-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Runtime Environments Compiler Construction Chapter 7.
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Subprograms subroutines, procedures, functions, methods.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
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.
Copyright Curt Hill Arrays in C/C++ More on usage.
CSC 8505 Compiler Construction Runtime Environments.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
ISBN Chapter 10 Implementing Subprograms.
Using a Pointer as an Array Name C allows us to subscript a pointer as though it were an array name Here's an example: #define N 100 int a[N], i, sum =0,
Multidimensional Arrays As Parameter void fun ( int matrix [] [10] ) {…} void main ( ) { int mat[5][10]; … fun(mat); } void fun (float *mat_ptr, int num_rows,
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Organization of Programming Languages
Pointers and References
C Passing arrays to a Function
Chapter 9 :: Subroutines and Control Abstraction
Multiple Dimension Arrays
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Pointers & Functions.
Multidimensional Arrays
A simple function.
Runtime Environments What is in the memory?.
Pointers & Functions.
Pointers and References
Presentation transcript:

Passing Parameters

Different Techniques Pass by Value Pass by Reference Pass by Result Pass by Value-Result Pass by Name

Pass By Value Make a copy of the value Send the copy to the function Result NOT transmitted back void test(int A) {A++;} //2 void main() { int B=5; //1 test(B); cout << B;//3 } AB //1 //2 // (entry)

Pass By Result No input value Send the copy to the function Result transmitted back on return void test(int A) {A++;} //2 void main() { int B=5; //1 test(B); cout << B;//3 } AB //1 //2 // (entry) ?5 5 ?

Pass By Value Result Copy of input value Use the copy in the function Result transmitted back on return void test(int A) {A++;} //2 void main() { int B=5; //1 test(B); cout << B;//3 } AB //1 //2 // (entry)

Pass By Reference Send address of original Use pointer inside function No need to do explicit return void test(int A) {A++;} //2 void main() { int B=5; //1 test(B); cout << B;//3 } AB //1 //2 // (entry) *B

Pass By Name Conceptual model is to substitute name of the actual parameter for the formal parameter Functions like reference in MOST cases When passing parameters like i,a[i] different results occur

Procedure BIGSUB integer GLOBAL; integer array LIST[1:2]; procedure SUB(PARAM) begin PARAM:=1; GLOBAL:=GLOBAL+1; PARAM:=5; end; begin LIST[1]:= 2; LIST[2]:= 2; GLOBAL :=1; SUB(LIST[GLOBAL]) end; Looks like only 2 changes in the procedure. First assignment to PARAM appears to have no effect! Substitute actual for formal Procedure BIGSUB integer GLOBAL; integer array LIST[1:2]; procedure SUB(PARAM) begin LIST[GLOBAL] :=1; GLOBAL:=GLOBAL+1; LIST[GLOBAL] :=5; end; begin LIST[1]:= 2; LIST[2]:= 2; GLOBAL :=1; SUB(LIST[GLOBAL]) end; Actually updates LIST[1] -> 1 LIST[2] -> 5 and GLOBAL -> 2

Review the examples in Section 8.5.8

2-d arrays as parameters Recall from earlier notes that 2-d arrays are –column major or –row major Formula for accessing elements for row major requires row size in dimension Loc (a[I,J]) = base address (a) (I-lb1)*size of row + (J-lb2)*size element size of row=number columns allocated *size element

Addressing Storage is row-major or column-major order (1,2)(1,1) (2,2)(2,1) (3,2)(3,1) (1,2) (1,1) (2,2) (2,1) (3,2) (3,1) (1,2) (1,1) (2,2) (2,1) (3,2) (3,1) int A[2,3];

C/C++ does row major void fun( int matrix[][10]) { …} void main () { int mat[5][10]; … fun(mat); … } Original dimension 5x10 Parameter only showing number of columns Row major is sized by column dimension. ONLY WORKS IF COLUMN SIZE IS 10!

Other ways of handling 2-d arrays Some languages such as Ada store a run-time property for the dimension float sumer(float mat[][] ) { float sum=0.0; int row; for(row=0;row<mat.length;row++)...