Parameter passing Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
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)
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Chapter 5 Functions.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Functions:Passing Parameters by Value Programming.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Sequences Jordi Cortadella Department of Computer Science.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
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.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
Vectors Jordi Cortadella Department of Computer Science.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
C++ Programming Lecture 12 Functions – Part IV
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
User-Defined Functions (cont’d) - Reference Parameters.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Functions Jordi Cortadella Department of Computer Science.
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Functions.
Reasoning with invariants
- Standard C Statements
For loops, nested loops and scopes
Multiple Files Revisited
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
CS150 Introduction to Computer Science 1
Multiple Files Revisited
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
Functions and Recursion
Dale Roberts, Lecturer IUPUI
Presentation transcript:

Parameter passing Jordi Cortadella Department of Computer Science

Functions Functions are defined as follows: Introduction to Programming© Dept. CS, UPC2 int gcd(int x, int y) { // Code } Parameters Type of result Name of the function It must include a return statement

Functions Sometimes, no result is returned. Introduction to Programming© Dept. CS, UPC3 void factors(int x) { // Code } No result

Function declarations Function declarations may appear before or after the main program. Introduction to Programming© Dept. CS, UPC4 #include using namespace std; int f() { // Code for f } int main() { // Code for the main program } void p(int a) { // Code for p }

Function declarations A function can only be used if previously declared. A function can be declared and used before its code is defined. Introduction to Programming© Dept. CS, UPC5 double volume_sphere(double radius); void some_geometry() {... double V = volume_sphere(1.0);... } double volume_sphere(double radius) { return 4Piradiusradiusradius/3; }

Use of functions Once a function has been declared, it can be used. – Functions are used as operations within expressions. – Procedures (void functions) are used as statements. Introduction to Programming© Dept. CS, UPC6 i = times(3, i + 2) + 1;... factors(i);...

Functions: parameter passing When a function is called, the arguments are passed to the function, so that its code can be executed: times(3, i + 2) +... int times(int x, int y) { … } Each argument must have the same type as its corresponding parameter. Introduction to Programming© Dept. CS, UPC7

Functions: parameter passing In general, any expression can be the argument of a function: Introduction to Programming© Dept. CS, UPC8 double maximum(double a, double b);... z = maximum(x, y);... r = maximum(3, gcd(s - 4, i) + alpha);... m = maximum(x, maximum(y + 3, 2Piradius));

Functions: parameter passing An object (a variable) is associated with a value and a memory location. In C++, there are two methods for parameter passing: – Passing the value (call-by-value). This is denoted by just declaring the type and the name of the parameter. – Passing the memory location (call-by-reference). This is denoted by adding the symbol & next to the parameter type. void p(int x, int& y) {... } Introduction to Programming© Dept. CS, UPC9 Call-by-value Call-by-reference

Functions: parameter passing Call-by-value makes a copy of the argument at the beginning of the function. It is equivalent to having, a statement that assigns the value of each argument to the corresponding parameter: Introduction to Programming© Dept. CS, UPC10 times(3, i + 2) is equivalent to: int times(int x, int y) { x = 3; y = i + 2; int p = 0;... }

Functions: parameter passing The effect of call-by-reference is that the parameter becomes the same object (variable) as the argument, i.e., the parameter becomes an alias of the argument. Example: function to swap the value of two variables Introduction to Programming© Dept. CS, UPC11 void exchange(int& x, int& y) { int z = x; x = y; y = z; }

Functions: parameter passing Introduction to Programming© Dept. CS, UPC12 exchange(a, b) is equivalent to having: void exchange(int& x, int& y) { int z = a; a = b; b = z; }

Functions: parameter passing int x, divisor; bool p;... cin >> x; p = is_prime(x + 3, divisor);... Introduction to Programming© Dept. CS, UPC13 bool is_prime(int n, int& d) { d = 2; bool prime = (n != 1); while (prime and d < n) { if (n%d == 0) prime = false; else d = d + 1; } return prime; } n: d: prime: x: divisor: p: true 3 false // Pre: n >= 1 // Returns whether n is prime. // If it is not prime, d is a divisor. copy reference Warning: we do not recommend the use of non-void functions with reference parameters in this course. false

Functions: parameter passing Use call-by-value to pass parameters that must not be modified by the function. Use call-by-reference when the changes made by the function must affect the variable to which the parameter is bound. In some cases, call-by-reference is used to avoid copies of large objects, even though the parameter is not modified. Introduction to Programming© Dept. CS, UPC14

Functions: parameter passing To define a function that, given two integers x and y, returns their quotient and remainder, we can write: Introduction to Programming© Dept. CS, UPC15 void div(int x, int y, int& q, int& r) { q = x/y; r = x%y; }

Functions: parameter passing Be careful. After the definition: void exchange(int x, int y) { int z = x; x = y; y = z; } the statement exchange(a,b) would not have any effect on a and b. Introduction to Programming© Dept. CS, UPC16

Functions: parameter passing A call-by-value parameter can receive any expression as an argument. A call-by-reference parameter can only be bound to variables. Introduction to Programming© Dept. CS, UPC17 void exchange (int& a, int& b);... exchange(a, b + 4); Incorrect parameter passing.

Summary Call-by-value and call-by-reference are two different mechanisms for passing parameters to functions. A call-by-reference parameter must be used when: – The parameter must be modified by the function, or – The call-by-value mechanism would be too expensive (large copy). This aspect will be further discussed when introducing functions for vectors. Introduction to Programming© Dept. CS, UPC18