4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions...

51
Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion 4. Functions March 10, 2010 4. Functions Einf ¨ uhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 1 of 40

Transcript of 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions...

Page 1: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

4. Functions

March 10, 2010

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 1 of 40

Page 2: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Outline

• Recapitulation

• Functions Part 1—What is aProcedure?

• Call-by-value and Call-by-reference

• Functions Part 2—The Details

• Recursion

• An Example: gcd (greatest commondivisor)

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 2 of 40

Page 3: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 4: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 5: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 6: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 7: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 8: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 9: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 10: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 11: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 12: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recapitulation

• What is the difference between = and ==?

• What is a strict boolean expression?

• Why should an if-statement always be followed by a {?

• What is a scope?

• What is the meaning of a while-loop?

• What does a while-loop look like?

• Can we modify a parameter within a while-loop?

• What happens if a single integer is argument/guard of a while-loop?

• What is dangerous if we declare two integer variables in one row:

i n t a , b=10;

• What is the difference of the compiler and the linker?

• What is the difference of a declaration and the definition?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 3 of 40

Page 13: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

—4.1. Functions 1—

What do these two sayings have in common?

Common saying: Good things come in small packages.

Common saying: Never reinvent the wheel.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 4 of 40

Page 14: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Code Dublication

i n t main ( ) {double pr iceOfMouta inBike = 1200.0;double exchangeRateJanuary = 1.4272; / / Januarydouble exchangeRateFebruary = 1.3686; / / Februarydouble exchangeRateMarch = 1 ,3569; / / March/∗∗ We always have to pay 60% of t o t a l cost∗ t h i s month .∗ /

double f i r s tPayment = 0 .60 ;double t o t a l C o s t ;

t o t a l C o s t = ( f i r s tPayment∗pr iceOfMouta inBike )∗exchangeRateJanuary+ ( (1 .0+ f i r s tPayment )∗ pr iceOfMouta inBike )∗ exchangeRateFebruary ;

s td : : cout << ” p r i ce o f January : ” << t o t a l C o s t << std : : endl ;

t o t a l C o s t = ( f i r s tPayment∗pr iceOfMouta inBike )∗ exchangeRateFebruary+ ( (1 .0+ f i r s tPayment )∗ pr iceOfMouta inBike )∗ exchangeRateMarch ;

s td : : cout << ” p r i ce o f February : ” << t o t a l C o s t << s td : : endl ;}

There is two bugs in this code!

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 5 of 40

Page 15: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Code Dublication

• Duplicated code is difficult to maintain, to fix, and to extend.

vo id main ( ) {code pa r t Acode pa r t A

}

• Long code is difficult to read and to understand (Spaghetti code).

vo id main ( ) {code doing one th i ngcode doing something d i f f e r e n t

}

• Duplicated code is a loss of development time.

vo id main ( ) {code implementing Gauss quadrature

}

vo id main ( ) {another implementing Gauss quadrature

}

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 6 of 40

Page 16: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Functions

vo id compute (double pr ice , double f i rs tPayment , double rateA , double rateB

) {double t o t a l C o s t =

( f i r s tPayment∗p r i ce )∗ rateA+ ((1.0− f i r s tPayment )∗ p r i ce ) ∗ rateB ;

s td : : cout << t o t a l C o s t << s td : : endl ;}

i n t main ( ) {double p r i ce = 1200.0;double january = 1.4272; / / Januarydouble february = 1.3686; / / Februarydouble march = 1 ,3569; / / Marchdouble f i r s tPayment = 0 .60 ;

s td : : cout << ” January : ” ;compute (

pr ice , f i rs tPayment , january , feb ruary) ;s td : : cout << ” February : ” ;compute (

pr ice , f i rs tPayment , february , march) ;

}4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 7 of 40

Page 17: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

vo id foo ( i n t a ) {a += 20;s td : : cout << a ;

}

. . .foo ( 3 9 ) ;i n t b=20;foo ( b ) ;foo ( b+10) ;

• C/C++ mechanism: Function or procedure.

• A function is a helper/assistant of the whole program.

i n t a0 = 39; / / foo (39)a0 += 20;s td : : cout << a0 ;i n t b=20;i n t a1 = b ; / / foo ( b )a1 += 20;s td : : cout << a1 ;i n t a2 = b+10; / / foo ( b+10). . .

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 8 of 40

Page 18: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

vo id foo ( i n t a ) {a += 20;s td : : cout << a ;

}

. . .foo ( 3 9 ) ;i n t b=20;foo ( b ) ;foo ( b+10) ;

• C/C++ mechanism: Function or procedure.

• A function is a helper/assistant of the whole program.

i n t a0 = 39; / / foo (39)a0 += 20;s td : : cout << a0 ;i n t b=20;i n t a1 = b ; / / foo ( b )a1 += 20;s td : : cout << a1 ;i n t a2 = b+10; / / foo ( b+10). . .

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 8 of 40

Page 19: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Functions and Scopes

vo id foo ( i n t a ) {a += 20;s td : : cout << a ;b += 1;

}

vo id bar ( i n t b ) {foo ( b ) ;foo ( b+10) ;foo ( b+20) ;

}

What happens here?

• Functions define a scope due to the brackets { } (basis block).

• Variables of other scopes are not defined within the function.

• Code above consequently does not work.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 9 of 40

Page 20: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Functions and Scopes

vo id foo ( i n t a ) {a += 20;s td : : cout << a ;b += 1;

}

vo id bar ( i n t b ) {foo ( b ) ;foo ( b+10) ;foo ( b+20) ;

}

What happens here?• Functions define a scope due to the brackets { } (basis block).

• Variables of other scopes are not defined within the function.

• Code above consequently does not work.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 9 of 40

Page 21: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Global Variables

i n t b ;

vo id foo ( i n t a ) {a += 20;s td : : cout << a ;b += 1;

}

vo id bar ( i n t b ) {foo ( b ) ;foo ( b+10) ;foo ( b+20) ;

}

• C/C++ allows programmer to define (global variables).

• Good practice: avoid them due to side effects.

• Now, code does work.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 10 of 40

Page 22: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Functions and the Program Counter

Memory

PC

21:22; foo1 // first operation of foo23; foo224; foo325; 26;27; foo() // call of foo28: foo()

void foo() { foo1; foo2; foo3; } ... foo(); // call foo foo();

• Function call modifies program counter.

• Computer remembers where to jump back.

• Function code is only stored once in memory.

• Not only the program counter is “remembered”, but all the variables.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 11 of 40

Page 23: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

The Call-Stack

vo id foo ( ) {i n t a = 20;/ / do something wi th a

/ / a now equals 50;}

vo id bar ( ) {i n t a = 30;foo ( ) ;

}

• Function call modifies program counter.

• Computer remembers where to jump back.

• Function code is only stored once in memory.

• Not only the program counter is “remembered”, but all the variables.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 12 of 40

Page 24: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

The Return Statements

i n t increment ( i n t a ) {a = a+1;r e t u r n a ;

}

vo id bar ( ) {i n t a = 30;i n t b = increment ( a ) ;s td : : cout << b ;

}

• Functions can return a value due to the return command.

• Returned type has to equal the type of the function.

• If a function doesn’t return a value, the function’s type is void.

• Consequently, void is a new primitive datatype.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 13 of 40

Page 25: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

What is the main Function?

i n t main ( ) {/ / do something

}

vo id bar ( ) {/ / do something

}

double foo ( ) {/ / do something

}

• Any program is a collection of functions. They have to have unique signatures(names).

• At startup, the computer has to know where to set the program counter to.

• It always sets it to the main function.

• The main function returns 0 if it has been successful (error code). This is a UNIXconvention.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 14 of 40

Page 26: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Functions—Some Best Practices

i n t main ( ) {/ / do something

}

vo id bar ( ) {/ / do something

}

double foo ( ) {/ / do something

}

• One purpose, one function. Any function should concentrate solely on one job.

• One page, one function. With a less, one should be able to see the completefunction.

• A function without documentation does not exist.

• Functions should have meaningful names (verbs).

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 15 of 40

Page 27: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

—4.2. Call-by-reference vs. Call-by-value—

The Call-Stack and Variables

vo id foo ( ) {i n t a = 20;/ / do something wi th a

/ / a now equals 50;}

vo id bar ( ) {i n t a = 30;i n t b = 20;foo ( ) ;

}

• Computer encounters function call (foo()).• It stores away all variables and remembers where it has been called from.• It resets the program counter.• As soon as the function terminates, it jumps back to the place of invocation, and• it restores all variables.• This way, a function cannot modify an “outer” variable.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 16 of 40

Page 28: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Call-by-value

vo id foo ( i n t x ) {/ / do something wi th x/ / x now equals 50;

}

vo id bar ( ) {i n t a = 30;i n t b = 20;foo ( a ) ;

}

• Computer encounters function call (foo()) with argument a.

• It stores away all variables and remembers where it has been called from.

• Furthermore, it creates a copy of a.

• It resets the program counter.

• . . .

• This way, a function cannot modify an “outer” variable.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 17 of 40

Page 29: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Call-by-value

• In C/C++, parameters (arguments) are passed call-by-value.

• Functions work on copies of the original variables.

• The original variables are saved away on the call stack.

• As soon as the operation terminates, the old variable values are restored.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 18 of 40

Page 30: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

The Return Statement and Call-by-value

i n t foo ( i n t x ) {x = x∗2;r e t u r n x ;

}

vo id bar ( ) {i n t a = 30;i n t b = foo ( a ) ;

a = foo ( a ) ;}

The return statement takes the value of the function’s copy and writes it to the left-handside of the function invocation.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 19 of 40

Page 31: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Call-by-reference

i n t foo ( i n t & x ) {x = x∗2;r e t u r n x ;

}

vo id bar ( ) {i n t a = 30;i n t b = foo ( a ) ;

}

• The & operator is the reference operator.

• It tells C/C++ to pass the argument with call-by-reference semantics.

• Then, C/C++ does not create a copy, but the operation works on the originalvariable.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 20 of 40

Page 32: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

i n t foo ( i n t & x ) {x = x∗2;r e t u r n x ;

}

vo id bar ( ) {i n t a = 30;i n t b = foo ( a ) ;

a = foo ( a ) ;}

• The & operator is the reference operator.

• It tells C/C++ to pass the argument with call-by-reference semantics.

• Then, C/C++ does not create a copy, but the operation works on the originalvariable.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 21 of 40

Page 33: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Exercise: Swap Two Variables

vo id main ( ) {i n t a = 3 ;i n t b = 4;/ / in terchange a and ba = b ;b = a ;s td : : cout << a << std : : endl ;s td : : cout << b << std : : endl ;

}

• Fix the code above.

• Extract the swap functionality in afunction of its own.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 22 of 40

Page 34: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Efficient Codes

• Copy-by-value is save as it forbids side-effects. Whenever possible, usecopy-by-value (default).

• Copy-by-reference is faster.

Question: Can we write fast and save code?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 23 of 40

Page 35: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Const References

vo id foo ( i n t a , i n t b ) {. . .

}

vo id bar ( i n t & a , i n t & b ) {. . .

}

vo id e f f i c i e n t F o o ( const i n t & a , const i n t & b ) {. . .

}

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 24 of 40

Page 36: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

—4.3. const Modifier—

—an Excursus

In C++, we can mark any argument to be a const argument, i.e. the function is notallowed to modify it.

vo id foo ( const i n t a ) {. . .

}

• const allows compiler to optimise code aggressively.

• const allows you to write bug-safe code (user cannot modify a variable byaccident).

• const closes the performance gap between C/C++ and Fortran.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 25 of 40

Page 37: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Please, Use Const

• Remember the 80 : 20 rule—you never know the performance properties.

• Remember that it might help others and you to produce less bugs.

• Remeber the implications.

vo id foo ( i n t & a ) {. . .

}

vo id bar ( i n t a ) { / / t h i s f u n c t i o n i s very slow and has to be tunedfoo ( a ) ;/ / cont inue c a l c u l a t i o n s

}

. . .foo ( 1 0 ) ; / / t h i s does not work , ins tead we have to w r i t ei n t tmp = 10;foo ( tmp ) ;

We cannot optimise bar, as foo does not have a const signature.

The last example is an aritificial one, but we will encounter similar problems soon.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 26 of 40

Page 38: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

—4.4. Functions 2—

The Signature of a Function

i n t foo ( )i n t bar ( )i n t foo ( i n t a )i n t foo ( double x )i n t foo ( i n t a , const double& b , double x )

A function is unique• due to its name, and

• due to the number of arguments, and

• due to the type of the arguments, and

• due to the const modifiers of its arguments, and

• due to a const modifier (not addressed yet).

Everything besides the name defines the signature. The name and the signature makethe function unique. The return value however does not come into play. Some definitionmake the const modifier not part of the signature.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 27 of 40

Page 39: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Overloading

i n t foo ( )i n t foo ( i n t a ) {}i n t foo ( double a ) {}

. . .

foo ( ) ;foo ( 3 . 4 ) ;foo ( 4 ) ;

To overload a function means to offer it with different signatures. C/C++ thenautomatically tries to find the right signature. However, be aware of automatic typeconversions.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 28 of 40

Page 40: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Automatic Type Conversion

i n t foo ( )i n t foo ( i n t a ) {}i n t foo ( double a ) {}

. . .

i n t a = 10;double b = a ;foo ( a ) ;foo ( b ) ;foo ( a / 3 . 0 ) ;

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 29 of 40

Page 41: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Priority

i n t foo ( )i n t foo ( i n t a ) {}i n t foo ( double a ) {}i n t bar ( i n t a , i n t b ) {}i n t bar ( double a , double b ) {}. . .

i n t a = 10;double b = a ;foo ( a ) ;foo ( b ) ;foo ( a / 3 . 0 ) ;bar ( a , 3 ) ;bar ( b , 3 ) ;bar ( a , 3 . 0 ) ;

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 30 of 40

Page 42: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Rules for Overloading

• If the arguments match exactly without type conversion, use this definition.

• If there is no exact match but a match due to automatic type conversion, C/C++uses this definition.

• If there’s a const and a non-const argument, C/C++ uses the const variant (not forall compilers).

Besides, the automatic type conversion, also take care of return type conversion:

double foo ( i n t a ) {r e t u r n a / 3 ;

}

. . .foo ( 4 . 0 ) ; / / we l l , t h i s does not workfoo ( 4 ) ;

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 31 of 40

Page 43: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Default Arguments

vo id foo ( i n t a =3) ;vo id bar ( ) ;vo id bar ( i n t b =3) ; / / not al lowedvo id tee1 ( i n t c , double d = 2 . 0 ) ;vo id tee2 ( i n t c=2 , double d ) ; / / not al lowedvo id tee3 ( i n t c=2 , double d = 4 . 0 ) ;

. . .foo ( ) ;foo ( 7 ) ;tee3 ( ) ;tee1 ( 1 ) ;tee1 ( 1 , 4 . 0 ) ;

• C/C++ support default arguments.

• They may not interfere with overloaded functions.

• They may not be followed by arguments without default arguments.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 32 of 40

Page 44: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Return Statements

double foo ( double a , double h ) {i f ( h==0) {

r e t u r n 0 . 0 ;}i f ( a==0)

r e t u r n ; / / e r r o r/ / do something compl icated wi th a and hr e t u r n a ;

}

vo id foo ( double a , double h ) {. . .r e t u r n 1 ; / / e r r o r

}

• A return statements makes the application leave the current function.

• A return statement accepts one argument: the return value (if it is not a voidfunction).

• A return statement may be everywhere in the function. However, multiple returnstatements often are considered to be bad style.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 33 of 40

Page 45: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

—4.5. Recursion—

A Simple Game

vo id foo (i n t l e f t , i n t r i g h t , i n t t

) {/ / value o f v a r i a b l e si n t b = l e f t + r i g h t ;/ / value o f v a r i a b l e sb /= 2 ;/ / value o f v a r i a b l e si f ( b<t ) {

foo ( b , r i g h t , t ) ;/ / value o f v a r i a b l e s

}i f ( b>t ) {

foo ( l e f t , b , t ) ;/ / value o f v a r i a b l e s

}}

. . .foo (−12 ,40 ,7);

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 34 of 40

Page 46: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Recursion—Principles

vo id foo1 ( i n t a ) {i f ( a==0) {

r e t u r n ;}. . .foo1 ( a−1);

}

• Functions may call themselves (recursion).• Each function call has its own copy of all the variables.• This may not terminate, so we have to be careful. So code a proper termination

criterion.• The underlying pattern often is referred to divide and conquer (which is not a

Roman saying!).• Recursion is a very powerful tool (matrix block algorithms, multiscale basis

systems, adaptive mesh refinement, e.g.)—a tool many programmers however donot understand.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 35 of 40

Page 47: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Greatest Common Divisor

• Write a recursive function gcd(int a,

int b) which computes the greatestcommon divisor.

• Test it for a=50,b=54.

• Make it plot the trace.

• Idea:

• a%b = 0⇒ b is the gcd.• Otherwise

gcd(a, b)← gcd(b, a%b).

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 36 of 40

Page 48: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Types of Recursion—Linear vs. Non-linear Recursion

/ / F inonacc i implementat ion 1vo id foo ( i n t a ) {

i f ( a==0) {r e t u r n ;

}. . .foo1 ( a−1);. . .

}

/ / F inonacc i implementat ion 2vo id bar ( i n t a ) {

i f ( a==0) {r e t u r n ;

}. . .bar ( a−1);bar ( a−2);. . .

}

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 37 of 40

Page 49: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Types of Recursion—(Linear) Tail Recursion

/ / F inonacc i implementat ion 1vo id foo ( i n t a ) {

i f ( a==0) {r e t u r n ;

}. . .foo1 ( a−1);

}

vo id bar ( i n t a ) {whi le ( . . . ) {

i f ( a==0) {r e t u r n ;

}. . .

}}

Theorem: You can rewrite every tail recursion into a loop.

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 38 of 40

Page 50: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Tail Recursion in Action

i n t foo ( i n t a ) {i f ( a <= 1) {

r e t u r n 1 ;}r e t u r n a ∗ foo ( a−1);

}

• What is the semantics of foo(int)?

• Rewrite foo(int) without recursion.

• Which variant might perform faster?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 39 of 40

Page 51: 4. Functions - TUM · Functions 1Call-by-reference vs. Call-by-valueconst ModifierFunctions 2Recursion 4. Functions March 10, 2010 4. Functions Einfuhrung¨ in die Programmierung—Introduction

Functions 1 Call-by-reference vs. Call-by-value const Modifier Functions 2 Recursion

Indirekt Recursion (Kaskadische Rekursion

vo id foo ( i n t a ) {i f ( . . . ) {

foo ( a−4);}bar ( a−1);foo ( a−1);. . .

}

vo id bar ( i n t a ) {. . .foo ( a−1);. . .

}

• Is foo a recursive function?• Is foo a tail recursion?• Is foo linear?• Is bar a recursive function?• Is bar a tail recursion?• Is bar linear?

4. Functions

Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 40 of 40