Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.

Slides:



Advertisements
Similar presentations
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Advertisements

C Language.
Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
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.
Programming Languages and Paradigms
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
CPSC 388 – Compiler Design and Construction Parameter Passing.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 7: User-Defined Functions II
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 :
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
Runtime Environments Source language issues Storage organization
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
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.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Subprograms subroutines, procedures, functions, methods.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSC 8505 Compiler Construction Runtime Environments.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Functions.
Chapter 7: User-Defined Functions II
Principles of programming languages 4: Parameter passing, Scope rules
Application Binary Interface (ABI)
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
Subroutines – parameter passing
Parameter Passing in Java
Topic 3-a Calling Convention 1/10/2019.
Simulating Reference Parameters in C
Chapter 7: User-Defined Functions II
Runtime Environments What is in the memory?.
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine parameter passing methods include: call-by-value - input parameters (declared with "IN") in Ada - default method for parameters in C, C++, and Pascal - parameters of primitive type in Java call-by-result - output parameters (declared with "OUT") in Ada

Subroutines – parameter passing Parameter passing methods (cont’d): call-by-value-result - in/out parameters (declared with "IN OUT") in Ada call-by-reference - large array parameters in Ada - array parameters in C and C++ - reference parameters (declared with "&") in C++ - reference parameters (declared with "VAR") in Pascal - object parameters in Java - default method for parameters in Fortran

Subroutines – parameter passing Parameter passing (cont’d) call-by-value – copy values of actual parameters into memory locations of formal parameters before executing the body of the subroutine; do nothing on return main a = 1 a: 1 b = 2 b: 2 call subr(a,b) pass 1,2 via stack print a,b print 1,2 subr(x,y) copy 1,2 into x,y ^ x = x + 1 x: /1/ 2 | y = x + y y: /2/ 4 | return '

Subroutines – parameter passing Parameter passing (cont’d) call-by-result - do nothing prior to executing the body of the subroutine; copy the final values of the formal parameters into the memory locations of the actual parameters on return main a = 1 a: 1 b = 2 b: 2 call subr(a,b) pass nothing receive ?,? from subr into a,b a: /1/ ? b: /2/ ? print a,b print ?,? subr(x,y) copy ?,? into x,y ^ x = x + 1 x: /?/ ? | y = x + y y: /?/ ? | return pass ?,? back via stack '

Subroutines – parameter passing Parameter passing (cont’d) call-by-value-result - perform copying of values both before and after executing the body of the subroutine main a = 1 a: 1 b = 2 b: 2 call subr(a,b) pass 1,2 via stack receive 2,4 from subr into a,b a: /1/ 2 b: /2/ 4 print a,b print 2,4 subr(x,y) copy 1,2 into x,y ^ x = x + 1 x: /1/ 2 | y = x + y y: /2/ 4 | return pass 2,4 back via stack --'

Subroutines – parameter passing Parameter passing (cont’d) call-by-reference – pass the addresses of the actual parameters – copy these addresses into the memory locations of the formal parameters – on each reference to a formal parameter in the body of the subroutine, perform an indirect reference to the corresponding actual parameter; o i.e. the formal parameter is an alias of the actual parameter, thus both the formal and actual parameter "name" refer to the same object o changes made using the formal parameter are being executed on the object passed as the actual parameter

Subroutines – parameter passing Parameter passing (cont’d) call-by-reference (cont’d) main a = 1 a: 1 a: /1/ 2 action in subr b = 2 b: 2 b: /2/ 4 action in subr call subr(a,b) pass &a,&b via stack print a,b print 2,4 subr(x,y) copy &a,&b into x,y ^ x = x + 1 x: &a thus a = a + 1 | y = x + y y: &b thus b = a + b | return '

Subroutines – parameter passing Consider the following code: int a = 5; /* global variable */ int subr( int b, int c ){ a = 4*a; b = b + 3; c = c + 2; return( a + b + c ); } void main(void){ int d = 1, e; e = subr( a, d ); } Show final values after calls to subr() for the variables listed below, by column, according to the specified parameter passing methods. (18 pts. total) b:call by value b:call by value-result b:call by reference c:call by value c:call by value-result c:call by reference a d e

Subroutines – parameter passing consider Fortran's call-by-reference applied to a constant parameter - what should the following code print? SUBROUTINE ADDONE( I ) I = I + 1 RETURN END... ADDONE( 2 ) WRITE(6,10) 2 10 FORMAT('CONSTANT 2 =',I1)

Subroutines – parameter passing (tradeoffs) optimizations can pass parameters in registers (not using stack) can sometimes not save/restore registers when executing a leaf routine advantagesdisadvantages Call-by- value loads and stores operate directly on formal parms copying overhead Call-by- reference no copying, allows subr to change the values of the actual parms indirect reference through addresses in formal parms to actual parms (i.e., you have to chase pointers)