0% found this document useful (0 votes)
44 views64 pages

Introduction To Programming: 4 Functions

The document is an introduction to functions in programming. It discusses defining and calling functions, function parameters, return types, local and global variables, and function declarations. Functions perform specific tasks, break complex tasks into smaller functions, and parameters are passed by value so changes inside a function do not affect the original variables.

Uploaded by

Orxan Qayibov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views64 pages

Introduction To Programming: 4 Functions

The document is an introduction to functions in programming. It discusses defining and calling functions, function parameters, return types, local and global variables, and function declarations. Functions perform specific tasks, break complex tasks into smaller functions, and parameters are passed by value so changes inside a function do not affect the original variables.

Uploaded by

Orxan Qayibov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Introduction to Programming

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;
}

➥ The program branches to a called function and returns to the


statement right after the call
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 77
4.1 Functions and Their Parameters ...

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

➥ Either you write your program “backwards” (main() comes last),


or you declare your functions, and define (implement) them later
in your program.
➥ If you have a call cycle (a() calls b(), and b() calls a()), then you
must use declarations.
➥ Function declarations (function prototypes) typically go into
header files (→ #include "myfunc.h")

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 79
4.1 Functions and Their Parameters ...

Declaring and defining functions


➥ Declaration:
return-type name ( parameter-list );
➥ Example:
int maximum ( int a, int b );
➥ Definition (implementation):
return-type name ( parameter-list ) { statement-list }
➥ Example:
int maximum ( int a, int b ) {
if ( a > b ) { return a; }
else { return b; }
}

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

➥ A function may have multiple return statements


➥ when return is executed, the function immediately returns to
its caller
➥ in a return statement, any expression (matching the return
type) is allowed as return value
➥ beware: too many return statements can make a function
difficult to follow!

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 81
4.1 Functions and Their Parameters ...

Formal and actual parameters


➥ Formal parameters
➥ are the parameters listed in the declaration / definition

➥ 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

➥ The words parameter and argument are used interchangably

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

➥ Global variables are declared outside any function


➥ they are accessible from all functions in the program
➥ Some examples ...

➥ Avoid the use of global variables!


➥ they are very hard to track . . .
➥ however, global constants are rather common
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 83
4.1 Functions and Their Parameters ...

Formal parameters are local variables


➥ In C++, parameters are passed by value
➥ this means, a function always receives copies of the actual
parameters
➥ when the function is called, the values of the actual
parameters are assigned to the formal parameters
➥ With call-by-value, variables given as actual parameters are never
changed
➥ The same variable can be simultaneously passed to multiple
parameters:
y = maximum(x, x)

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

int f(int b, int a)


{
a = 2 * b + a * a;
return a + 1;
}
Activation record
...
x 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

int f(int b, int a)


{
a = 2 * b + a * a;
return a + 1;
}

...
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

int f(int b, int a)


{
a = 2 * b + a * a;
return a + 1;
}

...
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

int f(int b, int a)


{ b 12 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

int f(int b, int a)


{ b 12 a 11
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 11
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 11
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 145
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 145
a = 2 * b + a * a;
return a + 1; Result 146
}

...
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

int f(int b, int a)


{
a = 2 * b + a * a;
return a + 1;
}

...
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

int f(int b, int a)


{ b a
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 15
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 15
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 15
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 249
a = 2 * b + a * a;
return a + 1; Result
}

...
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

int f(int b, int a)


{ b 12 a 249
a = 2 * b + a * a;
return a + 1; Result 250
}

...
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

int f(int b, int a)


{
a = 2 * b + a * a;
return a + 1;
}

...
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

int f(int b, int a)


{
a = 2 * b + a * a;
return a + 1;
}

...
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 ...

Another example: swapping two variables?


void swap(int x, int y) {
int temp;
cout << "swap: before, x=" << x << " y=" << y << endl;
temp = x; x = y; y = temp;
cout << "swap: after, x=" << x << " y=" << y << endl;
}
int main() {
int x = 5, y = 10;
cout << "main: before, x=" << x << " y=" << y << endl;
swap(x, y);
cout << "main: after, x=" << x << " y=" << y << endl;
return 0;
}

➥ Try it! (“References” will later fix this problem; ☞ 7.7)


Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 86
4.2 Excursion: Debuggers

What can a debugger do?


➥ It allows to watch the state of a running program, e.g.,
➥ stop the execution at a given code line (breakpoint)
➥ examine the procedure call stack
➥ print (or modify) the contents of variables
➥ print type definitions
➥ execute the program line-by-line

➥ 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

The factorial function


➥ Problem: given n (distinguishable) items, in how many different
linear sequences can we arrange them?

➥ For n = 1, it’s simple: only one way


➥ For n > 1, we solve the problem like this:
➥ we have n choices for the first item in the sequence
➥ once we have chosen the first item, we just need to know how
many different sequences exist for the remaining ones
➥ the result is n times this number
!
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) 88
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 ...

The C++ function fac()


➥ Mathematicians allow zero items in a row and thus define
!
1, if n = 0
fac(n) =
n ∗ fac(n − 1) if n > 0

➥ C++ implementation of this function:


unsigned int fac(unsigned int n) {
if (n == 0) {
return 1;
}
else {
return n * fac(n-1);
}
}
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 90
4.3 Recursive Functions ...

Local variables (dynamically)


➥ Each invocation of a function gets its own set of local variables!
➥ Otherwise, variables would no longer be local in 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 fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}

. . .
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 fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1); n 2 Result
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1); n 2 Result
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
n 1 Result
return 1;
else
return n * fac(n−1); n 2 Result
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
n 1 Result
return 1;
else
return n * fac(n−1); n 2 Result
}
n 3 Result

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

unsigned int fac(unsigned int n) n 0 Result


{
if (n == 0)
n 1 Result
return 1;
else
return n * fac(n−1); n 2 Result
}
n 3 Result

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

unsigned int fac(unsigned int n) n 0 Result 1


{
if (n == 0)
n 1 Result
return 1;
else
return n * fac(n−1); n 2 Result
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
n 1 Result 1 1
return 1;
else
return n * fac(n−1); n 2 Result
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1); n 2 Result 2 1
}
n 3 Result

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}
n 3 Result 6 2

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

unsigned int fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}

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 fac(unsigned int n)


{
if (n == 0)
return 1;
else
return n * fac(n−1);
}

. . .
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);

➥ If one parameter has a default value, then all the following


parameters must have a default value, too
➥ otherwise it is unclear what is left out in the call

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);

➥ In C++, multiple functions with the same name are allowed, if


➥ the number of parameters are different
➥ or at least one parameter has a different type

➥ This is called overloading the function name

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 94
4.4 Functions: More Details ...

Overloading functions ...

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).

Why use modules?


➥ Structure the program code
➥ Make modules re-usable by others
➥ Save compilation time

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 96
4.5 Separate Compilation (Modules) ...

An example: a calculator module

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.cpp in calcMain.cpp:


#include "calculator.h" // search in user and system dirs

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) ...

Compiling the program:

bslab01:~> g++ -c calcMain.cpp


bslab01:~> g++ -c calculator.cpp
bslab01:~> g++ -o calcMain calcMain.o calculator.o

➥ 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

➥ Compilation of our calculator program:


➥ compile calcMain.cpp: g++ -c calcMain.cpp
➥ compile calculator.cpp: g++ -c calculator.cpp
➥ link the object files into an executable program:
g++ -o calcMain calcMain.o calculator.o
➥ After a change, we want to recompile only the affected files
➥ Dependencies: calcMain.cpp calculator.h calculator.cpp

calcMain.o calculator.o

calcMain

➥ The make program automates this process

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

int maximum(int a, int b);


int main() {
int x = 5, y = 10;
cout << "Maximum of " << x << " and " << y << " is ";
cout << maximum(x, y) << endl;
return 0;
}
int maximum(int a, int b) {
if ( a > b ) { return a; }
else { return b; }
}

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 5/13) 104

You might also like