05 Applicative Programming
05 Applicative Programming
Header Files
Loops
Enums
Header Files
Loops
Enums
Outline
Header Files
Loops
Enums
Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { return b; } b = b%a ; }
Header Files
Loops
Enums
Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { return b; } b = b%a ; }
Header Files
Loops
Enums
Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { return b; } b = b%a ; }
What is missing in this code? What does the return b do? What happens if we replae a==0 with a=0?
Header Files
Loops
Enums
Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }
Header Files
Loops
Enums
Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }
What value does a have in each single code line? What happens with the following snippet? i n t a =10; i n t b =20; i n t r =foo ( a , b )
Header Files
Loops
Enums
Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }
What value does a have in each single code line? What happens with the following snippet? i n t a =10; i n t b =20; i n t r =foo ( a , b )
Can foo() modify the value of the variable a in our main application?
Header Files
Loops
Enums
Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }
What value does a have in each single code line? What happens with the following snippet? i n t a =10; i n t b =20; i n t r =foo ( a , b )
Can foo() modify the value of the variable a in our main application?
Header Files
Loops
Enums
19031957 Manhattan Project (Los Alamos) June 30, 1945 (but Turing et. al. published similar ideas)
Components
There is a Von-Neumann bottleneck
Header Files
Loops
Enums
One or two documents in the ofce (two registers in the ALU) aint sufcient. Introduce more registers (Itanium e.g. has 128 of them). However, number of registers still is limited.
Header Files
Loops
Enums
Running into the basement is time consuming, and The bigger the basement (memory), the slower the search becomes. The faster the processor, the more annoying the slow search in the memory is. Can we study this effect?
Header Files
Loops
Enums
Header Files
Loops
Enums
Idea of a Cache
Header Files
Loops
Enums
Cache Levels
Header Files
Loops
Enums
Caches
Computers have a hierarchy of caches and lots of registers. The time to nish one operation depends signicantly on where the data is located
right now.
It is important for many algorithms to exhibit spatial locality and temporal locality. Brain teaser 1: How would you implement a matrix-vector product? Brain teaser 2: What is the fundamental challenge if we transpose a matrix? Brain teaser 3: What is the best way to run through a Cartesian grid?
Header Files
Loops
Enums
before.
Declaration: Tell the compiler what names are available. Denition: Dene where the name is stored.
Header Files
Loops
Enums
This denition is both a declaration and a denition. A function call is replaced by a reset of the program counter (and some additional
things).
Sometimes, wed like to split up les into several les.
Header Files
Loops
Enums
We can invent some clever C/C++-concatenate operation (C/C++ actually has such a thing) and make le B compile.
Header Files
Loops
Enums
. . . However
/ / File A void foo ( i n t a ) { / / do something i n t e l l i g e n t } / / File B v o i d bar ( i n t a ) { foo ( a +2); } / / File C void t a r ( i n t a ) { foo ( a +4); }
We can invent some clever C/C++-concatenate operation (C/C++ actually has such a thing) and make le B and le C compile. However, the linking process fails as the symbol foo() is dened twice.
Header Files
Loops
Enums
Function Denitions
/ / File A void foo ( i n t a ) { / / do something i n t e l l i g e n t } / / File B void foo ( i n t a ) ; v o i d bar ( i n t a ) { foo ( a +2); } / / File C void foo ( i n t a ) ; void t a r ( i n t a ) { foo ( a +4); }
First, this code compiles ne. Second, this code links ne, if we pass the linker all three les. Third, we now know why! The compiler does not resolve the program counter
Header Files
Loops
Enums
Function Denitions
Writing the functions declaration over and over again is cumbersome and error prone. Lets put it into a le of its own. Files containing solely declarations, are called header les and typically have the extension .h or .hpp.
/ / F i l e my . h void foo ( i n t a ) ; / / F i l e my . cpp # i n c l u d e my . h void foo ( i n t a ) { / / do something i n t e l l i g e n t } / / File B # i n c l u d e my . h v o i d bar ( i n t a ) { foo ( a +2); } / / File C # i n c l u d e my . h void t a r ( i n t a ) { foo ( a +4); }
5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 41
Header Files
Loops
Enums
Header Files
/ / F i l e my . h void foo ( i n t a ) ; / / F i l e my . cpp # i n c l u d e my . h void foo ( i n t a ) { / / do something i n t e l l i g e n t }
A good header le never contains denitions. A good header les has lots of comments as other editors of les see it frequently. A header le is typically accompanied by one implementation le (best practice).
Every C/C++ and every UNIX installation comes along with tons of header les.
Header Files
Loops
Enums
Linker Usage
C comes along with lots of useful standard libraries. These libraries provide tons of useful operations.
See math.h for example or iostream which provides the terminal output function << (this is also just a function with the name <<).
We add denitions due to include statements. We add the compiled les either by
passing the object les to the linker or passing the library les (a,so) due to the -l argument with -L giving the compiler the library search path.
Header Files
Loops
Enums
/ / m u l t i p l e d e f i n i t i o n s . Has t o be removed
Sometimes, it is pain to remove multiple denitions. In particular, if a headers include other headers (transitivity of includes).
Header Files
Loops
Enums
Include Guards
/ / File a.h # i f n d e f INCLUDE A H # d e f i n e INCLUDE A H void foo ( i n t a ) ; #endif / / File b.h # i n c l u d e my . h v o i d bar ( i n t a ) ; / / F i l e b . cpp #include b . h v o i d bar ( i n t a ) {\ l d o t s } / / File c .h # i n c l u d e my . h #include b . h void t a r ( ) ; / / F i l e c . cpp #include c . h v o i d t a r ( ) {\ l d o t s }
Header Files
Loops
Enums
The C/C++ is a powerful precompiler (does something similar like cut-n-paste). Its symbol mechanism allows us to avoid multiple declarations. Embed every header le into ifndef-define-endif statements. Precompiler can do a lot of additional things we wont discuss here.
Header Files
Loops
Enums
the symbol.
page 23 of 41
Header Files
Loops
Enums
Header Files
Loops
Enums
Namespaces
/ / T h i s i s header A # i f n d e f IDENTIFIER FOR HEADER A H # d e f i n e IDENTIFIER FOR HEADER A H namespace lennardJones { / C a l c u l a t e s f o r c e between two p a r t i c l e s ( LennardJones ) / double getForce ( c o n s t double& d i s t a n c e ) ; } #endif / / T h i s i s header B # i f n d e f IDENTIFIER FOR HEADER B H # d e f i n e IDENTIFIER FOR HEADER B H namespace wca { / C a l c u l a t e s f o r c e between two p a r t i c l e s (WCA model ) / double getForce ( c o n s t double& d i s t a n c e ) ; } #endif
With namespaces, we can group variables, constants, and functions into logic subsets.
5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 25 of 41
Header Files
Loops
Enums
Using Namespaces
/ / T h i s i s t h e header # i f n d e f IDENTIFIER FOR HEADER A H # d e f i n e IDENTIFIER FOR HEADER A H namespace lennardJones { / C a l c u l a t e s f o r c e between two p a r t i c l e s ( LennardJones ) / double getForce ( c o n s t double& d i s t a n c e ) ; } #endif / / This i s the implementation # i n c l u d e lennardJones . h double lennardJones : : getForce ( c o n s t double& d i s t a n c e ) { \ldots } Namespaces are predecessors of the identier. Namespaces can be embedded into each other. Alternatively, you can use u s i n g namespace lennardJones ;
5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 41
Header Files
Loops
Enums
All the functions, constants, . . . of the standard library are embedded into the
namespace std.
std::endl is just a constant whose denition can be found in iostream or a
Header Files
Loops
Enums
5.4. Loops
Create three les: main.cpp,
F0 = 0 F1 = 1
loop, or do it recursively.
Header Files
Loops
Enums
Do-While Loop
Header Files
Loops
Enums
The For-Loop
i n t a =0; do { / / something i n t e l l i g e n t a ++; } w h i l e ( a < 20);
Often, we want to run through a sequence with xed size. While loops are error-prone as we might forget the increment. Alternatively: Use the for loop. f o r ( i n t a =0; a <20; a++) { / / something i n t e l l i g e n t }
Header Files
Loops
Enums
The for statements opens a new scope. Statement int a creates a new variable (loop counter) within this scope. Statement a<20 is the termination criterion. It is evaluated before one iterate. Statement a++ is an increment.
Header Files
Loops
Enums
Header Files
Loops
Enums
Header Files
Loops
Enums
Header Files
Loops
Enums
Header Files
Loops
Enums
Use for for xed ranges (compiler can optimise and parallelise). Do not invoke a function in the guard or increment section. Always open brackets after a for loop. Do not manipulate the counter within a for loop (error-prone; optimisation).
Header Files
Loops
Enums
5.5. Enums
In the next session, we will create more sophisticated data structures such as sequences of variables and complex data types that represent whole records (like tuples of time and measurement in an experiment). Before, we however have to talk about the last (primitive) datatypeenumerations.
enum Colour { Red , Green , Blue };
Header Files
Loops
Enums
Enums in Action
enum Colour { Red , Green , Blue }; \ldots Colour myColour = Red ; \ldots i f ( myColour==Green ) { ... }
Header Files
Loops
Enums
Enums
enum Colour { Red , Green , Blue };
Mind the semicolon terminating the enum denition. Enums are basically integers (no type safety). Enum variants belong the enclosing namespace.
Header Files
Loops
Enums
Header Files
Loops
Enums