jordi cortadella department of computer sciencejordicf/teaching/fme/... · functions: parameter...

18
Parameter passing Jordi Cortadella Department of Computer Science

Upload: others

Post on 17-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Parameter passing

Jordi Cortadella

Department of Computer Science

Page 2: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Functions

Functions are defined as follows:

Introduction to Programming © Dept. CS, UPC 2

int gcd(int x, int y) {

// Code

}

ParametersType of result

Name of the function

It must include a return statement

Page 3: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Functions

Sometimes, no result is returned.

Introduction to Programming © Dept. CS, UPC 3

void factors(int x) {

// Code

}

No result

Page 4: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Function declarations

Function declarations may appear before or after the main program.

Introduction to Programming © Dept. CS, UPC 4

#include <iostream>using namespace std;

int f() {// Code for f

}

int main() {// Code for the main program

}

void p(int a) {// Code for p

}

Page 5: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 5

double volume_sphere(double radius);

void some_geometry() {... double V = volume_sphere(1.0);...

}

double volume_sphere(double radius) {return 4Piradiusradiusradius/3;

}

Page 6: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 6

i = times(3, i + 2) + 1;

...

factors(i);

...

Page 7: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 7

Page 8: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Functions: parameter passing

In general, any expression can be the argument of a function:

Introduction to Programming © Dept. CS, UPC 8

double maximum(double a, double b);

...

z = maximum(x, y);...

r = maximum(3, gcd(s - 4, i) + alpha);

...

m = maximum(x, maximum(y + 3, 2Piradius));

Page 9: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Functions: parameter passingAn 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, UPC 9

Call-by-value Call-by-reference

Page 10: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Functions: parameter passingCall-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, UPC 10

times(3, i + 2)

is equivalent to:

int times(int x, int y) {

x = 3; y = i + 2;

int p = 0;

...

}

Page 11: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 11

void exchange(int& x, int& y) {

int z = x;

x = y;

y = z;

}

Page 12: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Functions: parameter passing

Introduction to Programming © Dept. CS, UPC 12

exchange(a, b)

is equivalent to having:

void exchange(int& x, int& y) {

int z = a;

a = b;

b = z;

}

Page 13: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

Functions: parameter passing

int x, divisor;

bool p;

...

cin >> x;

p = is_prime(x + 3, divisor);

...

Introduction to Programming © Dept. CS, UPC 13

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:

6

9

2

true

3

false

false

// Pre: n >= 1// Returns whether n is prime.// If it is not prime, d is a divisor.

Warning: we do not recommend the use of non-voidfunctions with reference parameters in this course.

false

Page 14: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 14

Page 15: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 15

void div(int x, int y, int& q, int& r) {

q = x/y;

r = x%y;

}

Page 16: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 16

Page 17: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 17

void exchange (int& a, int& b);

...

exchange(a, b + 4);

Incorrect parameter passing.

Page 18: Jordi Cortadella Department of Computer Sciencejordicf/Teaching/FME/... · Functions: parameter passing •Use call-by-value to pass parameters that must not be modified by the function

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, UPC 18