Introduction To Programming: 4 Functions
Introduction To Programming: 4 Functions
Introduction To Programming: 4 Functions
Summer Term
4 Functions
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 75
4 Functions ...
Contents
➥ Functions and their parameters
➥ Excursion: Debuggers
➥ Recursion
➥ Functions: More details
➥ Modules (separate compilation)
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 76
4.1 Functions and Their Parameters
Calling functions
func1(){
main(){ 1 ...
... ...
func1(); return; func3(){
3 ...
... } ...
func2(); func2(){ ...
2
... func3(); return;
func4(); 4
... }
return 0; return;
} func4(){ }
...
...
return;
}
Designing functions
➥ A well-designed function performs a specific task
➥ A function does one thing, does it well, and returns
➥ Complicated tasks should be broken into multiple functions
➥ This helps giving your program structure
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 78
4.1 Functions and Their Parameters ...
Declaring functions
➥ Before you can use (call) a function, you have to declare it
➥ “before” means in the program code
➥ note: This is the same as with variables
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 79
4.1 Functions and Their Parameters ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 80
4.1 Functions and Their Parameters ...
Return types
➥ A function must have a return type
➥ Types are either int, float, bool, . . .
or void, meaning: no return value
➥ a function without a return value is also called a procedure
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 81
4.1 Functions and Their Parameters ...
➥ Actual parameters
➥ are the parameters given to an actual call to the function
➥ Example:
int maximum(int a, int b); // a, b: formal parameters
...
x = maximum(42, 44); // 42, 44: actual parameters
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 82
4.1 Functions and Their Parameters ...
Local variables
➥ Variables declared inside a function’s body (i.e., inside { . . . })
only exist locally within that function
➥ when the function returns, the local variables do not exist any
more
➥ note: in C++, this rule applies to any compound statement
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 84
4.1 Functions and Their Parameters ...
An example
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 10 a
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 10 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
Activation record of f
int f(int b, int a)
{ b a
a = 2 * b + a * a;
return a + 1; Result
}
...
x 10 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 10 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a 12
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3); 146 250
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
An example
...
x 11 a −104
int x = 10;
int a = 12;
a = f(a, ++x) − f(a, a+3);
...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 85
4.1 Functions and Their Parameters ...
➥ It helps you to find out what the program actually does (the bugs
you have to find / remove yourself ;-)
➥ In the lab:
➥ g++ -g myprog.cpp -o myprog
➥ ddd myprog
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 87
4.3 Recursive Functions
Recursive functions
➥ A function is recursive, if the function is used (called) in its own
definition (implementation)
!
1, if n = 1
fac(n) =
n ∗ fac(n − 1) if n > 1
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 89
4.3 Recursive Functions ...
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 91
4.3 Recursive Functions ...
Execution of fac
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
n 4 Result 24 6
. . .
unsigned int result = fac(4); result
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.3 Recursive Functions ...
Execution of fac
. . .
unsigned int result = fac(4); result 24
. . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 92
4.4 Functions: More Details
Default parameters
➥ When a function is called, for each formal parameter, an actual
parameter must be given ...
➥ ... unless the function prototype provides a default value
➥ Example: void myFunction ( int x = 50 );
➥ Calls: myFunction(); myFunction(10);
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 93
4.4 Functions: More Details ...
Overloading functions
➥ Sometimes, the same functionality is needed on different types:
➥ int maximum (int a, int b);
➥ double maximum (double a, double b);
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 94
4.4 Functions: More Details ...
Caution:
➥ Only use this feature for multiple functions of the same
functionality!
➥ the “intellectual distance” between two functions with the same
name is zero
➥ You may have a hard time finding out what will be called, if
differences are subtle like signed/unsigned, float/double, . . .
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 95
4.5 Separate Compilation (Modules)
What is a module?
➥ A module is a part of a program that can be compiled separately
➥ In C++, a module always should consist of two files:
➥ a header file (*.h), which contains declarations, e.g. function
prototypes, constants, ...
➥ an implementation file (*.cpp).
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 96
4.5 Separate Compilation (Modules) ...
calculator.h:
#ifndef CALCULATOR_H
#define CALCULATOR_H
namespace calculator {
// The following functions live in their own name space
double calculate(double operand1, char op,
double operand2);
double readOperand(int no);
char readOperator();
void printResult(double val);
}
#endif
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 97
4.5 Separate Compilation (Modules) ...
calculator.cpp:
#include <iostream> // search in system directories
#include "calculator.h" // search in user and system dirs
namespace calculator {
using std::cout;
using std::cin;
using std::endl;
double calculate(double operand1, char op,
double operand2) {
if (op == ’*’)
return operand1 * operand2;
if (op == ’/’)
return operand1 / operand2;
return 0; // Invalid operator
}
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 98
4.5 Separate Compilation (Modules) ...
double readOperand(int no) {
double val;
cout << "Please enter the "
<< ((no == 1) ? "first" : "second") << " operand: ";
cin >> val;
return val;
}
char readOperator() {
char val;
cout << "Please enter the operator: ";
cin >> val;
return val;
}
void printResult(double val) {
cout << "The result is " << val << endl;
}
}
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 99
4.5 Separate Compilation (Modules) ...
using calculator::readOperand;
using calculator::readOperator;
using calculator::printResult;
using calculator::calculate;
int main() {
char op; double a, b;
a = readOperand(1);
op = readOperator();
b = readOperand(2);
printResult(calculate(a, op, b));
return 0;
}
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 100
4.5 Separate Compilation (Modules) ...
➥ The option -c tells the compiler to skip the linker step and to
produce an object file
➥ The final invocation of g++ links the two object files together
➥ If you later change, e.g., the main program, you have to repeat
only the compilation of the changed code and the linker step:
bslab01:~> g++ -c calcMain.cpp
bslab01:~> g++ -o calcMain calcMain.o calculator.o
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 101
4.6 Excursion: The make utility
calcMain.o calculator.o
calcMain
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 102
4.6 Excursion: The make utility ...
The makefile
➥ make is controlled by a file called makefile:
# How to make the executable ’calcMain’ when
# ’calcMain.o’ and ’calculator.o’ already exist.
calcMain: calcMain.o calculator.o
g++ -o calcMain calcMain.o calculator.o
# dependency
calculator.o: calculator.cpp calculator.h
# dependency
calcMain.o: calcMain.cpp calculator.h
calcMain.cpp calculator.h calculator.cpp
➥ make has a default rule to
compile *.cpp to *.o calcMain.o calculator.o
calcMain
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 103
Summary
Functions
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 104