Subroutines – parameter passing

Slides:



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

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.
Programming Languages and Paradigms
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.
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.
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.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 8 :: Subroutines and Control Abstraction
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Runtime Environments Compiler Construction Chapter 7.
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.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Subprograms subroutines, procedures, functions, methods.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS 536 Parameter Passing 1. Roadmap Last Time – Storing variables Locals, non-locals, globals This Time – Propagating values from one function to another.
CSC 8505 Compiler Construction Runtime Environments.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Functions + Overloading + Scope
Functions.
Chapter 7: User-Defined Functions II
Chapter 10: Void Functions
Subroutines … passing data
Principles of programming languages 4: Parameter passing, Scope rules
User-Defined Functions
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Methods and Parameters
Pointers and References
CSC 253 Lecture 8.
Chapter 9 :: Subroutines and Control Abstraction
CSC 253 Lecture 8.
Chap. 8 :: Subroutines and Control Abstraction
Subroutines Web Programming.
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
Pointers & Functions.
Anatomy of a Function Part 3
Array and Method.
Parameter Passing in Java
Topic 3-a Calling Convention 1/10/2019.
Simulating Reference Parameters in C
Chapter 7: User-Defined Functions II
Chapter 9 Subprograms Fundamentals of Subprograms
Chapter 9: Pointers and String
Runtime Environments What is in the memory?.
Pointers & Functions.
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 call-by-value example 2 int a = 1; /* global variable */ int subr( int b, int c ) { a = 2*a; b = b + 2; c = c - 1; return( a * b * c ); } void main(void) { int d = 5, e; e = subr( a, d ); Show final values after calls to subr() b: call by value c: call by value 16. a _____ 17. d _____ 18. e _____ 2 5 24

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 call by value-result example 2 int a = 1; /* global variable */ int subr( int b, int c ) { a = 2*a; b = b + 2; c = c - 1; return( a * b * c ); } void main(void) { int d = 5, e; e = subr( a, d ); Show final values after calls to subr() b: call by value-result c: call by value-result 16. a _____ 17. d _____ 18. e _____ 3 4 24

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; 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 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 call by reference example 2 int a = 1; /* global variable */ int subr( int b, int c ) { a = 2*a; b = b + 2; c = c - 1; return( a * b * c ); } void main(void) { int d = 5, e; e = subr( a, d ); Show final values after calls to subr() b: call by reference c: call by reference 16. a _____ 17. d _____ 18. e _____ 4 4 64

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 20 8 23 d 1 3 3 e 31 31 49

Subroutines – parameter passing (tradeoffs) advantages disadvantages 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) optimizations can pass parameters in registers (not using stack) can sometimes not save/restore registers when executing a leaf routine