SlideShare a Scribd company logo
1
Nico Ludwig (@ersatzteilchen)
(3) Basics of the C++ Programming Language
2
TOC
● (3) Basics of the C++ Programming Language
– Procedural Programming
– Predefined and User defined Functions
– Declaration and Definition of Functions
– Procedural and recursive Function Calling
– Namespaces and separated Function Definitions
– A Glimpse of Separated Compilation and Translation Units
● Sources:
– Bruce Eckel, Thinking in C++ Vol I
– Bjarne Stroustrup, The C++ Programming Language
3
Procedural Programming
● Imperative programming reaches its limits very soon.
– It is needed to repeat identical code parts all over in the program.
– If a repeated part contains a bug, this bug is present on all occurrences!
– We have to copy a code part to reuse it, or to share it with other developers.
– In general: Don't Repeat Yourself! DRY
– DRY in programming results in the the "Top-Down" approach.
● In engineering, complex problems are getting separated into smaller problems.
– In programming, problems are broken into procedures or functions.
– We call this the "Bottom-Up" approach, or functional decomposition.
● Procedural programming is all about the "Top-Down" and "Bottom-Up" notions.
– Top-Down: Programmers rework their code into functions to make it reusable.
– Bottom-Up: Programmers code new functions that solve subproblems.
● Procedural programming extends imperative programming with the concept of (reusable) functions.
4
Procedural Programming in C/C++
● In C/C++, procedural programming is implemented with functions.
● A function is a part of code that can be called independently.
– Formerly known as "sub program".
– In C/C++ there is no difference between procedures and functions.
– In functions, statements can be executed imperatively.
– In functions, further functions can be called as well.
●
This code style is rather verb-oriented: "do this" and "call that" etc.
● Procedural code in C/C++:
– Program execution starts in the function main(), from where other functions are being called.
– In fact coding functions is the central activity of C/C++ programmers!
● What is a procedure?
5
Using predefined Functions in own Code
● The C/C++ standard library provides a plethora of functions for us.
– The C/C++ standards team already had their "Top-Down" specs on... :-)
– These functions allow reusing foreign code to solve own problems.
– E.g: std::pow(), std::qsort(), std::bsearch(), std::transform() etc.
– Some functions open the gate to the OS, like std::system().
● How to use functions from the standard library?
– #include the respective standard h-file, where the function declaration resides.
– (Optionally add a using directive for the namespace std.)
– Call the standard function within own code.
– (Mind to pass the lib file, where the functions' definition resides, to the linker.)
● "There is a problem with the usage of
std::system()." What does this statement mean?
● Well, std::system() accepts commands that are
highly platform-dependent in most cases.
6
Declaration and Definition of Functions
int GetCount (int x, int y)
{ // A block of code (function body).
}
return 42;
Type of returned value or void. Function name Parameter list
Function bodyReturn value from function.
int GetCount (int x, int y = 0);Declaration:
Definition:
C++11 – trailing return type
auto GetCount(int x, int y) -> int {
return 42;
}
int (*)(int, int)
The signature of GetCount.
Default argument
7
Features of Functions – Returning Values
● Functions can have a return type.
– When such a function is called, a value of type "return type" will be returned as a result of that function.
– This value can be stored (stuck into a variable) or otherwise consumed.
● Functions w/o return type are declared as returning void instead of a concrete type.
– Such functions don't have a result, but a persistent side effect in most cases.
● Functions, which only return values but have no side effects, are sometimes called pure functions.
● When a return statement in a function is executed (w/ or w/o returning a value) a function will beexited immediately!
// Store the returned value in a variable... (Mind that we have to write a pair of empty
// parentheses, as GetDayOfMonth() accepts no parameters.):
int day = GetDayOfMonth();
// …or use it in an expression:
std::cout<<"The day is: "<<GetDayOfMonth()<<std::endl;
// Finally the function can be called, ignoring the result:
GetDayOfMonth();
// On calling ClearScreen(), the side effect happens, then ClearScreen() returns.
ClearScreen(); // However, ClearScreen dos not return a value we could process!
// A function, which returns an int.
int GetDayOfMonth() {
// pass
}
// Clears the screen as side effect,
// but doesn't return a value.
void ClearScreen() {
// pass
}
8
Features of Functions – Parameters
● The return type, the parameter types and the order of parameters make up the signature of a function.
● Function definition and declaration can have an optional list of parameters (params).
– A parameter is a function's variable storing the value of the passed argument.
– A set of functions can have the same name, but differ in the signature's parameter list.
● This handy feature is what we call "function overloading".
– The last items of the parameter list can be prepared with default arguments or build a variable argument list.
● On calling a function a list of arguments will be passed to satisfy the params.
– If a function requires no parameters, an empty pair of parentheses is written.
– Order and type of the arguments must match to the parameters respectively.
● By this matching, the correct (overloaded) function will be found.
● A matching (overloaded) function is resolved at compile time!
– Parameters having a default argument, need not to be "satisfied".
– Arguments are passed by value by default, so parameters contain copies of the arguments.
● It is not possible to overload return types!
● Btw. selecting function overloads at run time can
also be implemented in C++ with so called
"multiple dispatch".
● What is the difference between arguments and
parameters?
● A parameter is nothing but a local variable that
contains the value of the respective argument.
Or: an argument is the initial value of the
respective parameter.
● The passed argument expressions are evaluated
in an unpredictable order!
9
Features of Functions – Calling Functions in a Nutshell
// No return type, no parameter list.
void clearScreen() {
// pass
}
// Call clearScreen() (mind the pair of empty parentheses), clearScreen() should
// perform a side effect.
clearScreen();
// No return type, awaits an int parameter.
void checkAccount(int accountId) {
// pass
}
// Call checkAccount(), pass 42 as argument.
checkAccount(42);
// Returns int, awaits an int parameter.
int getEntry(int entryID) {
// pass
}
// Call getEntry(), pass 4 as argument, store the returned result in the variable theEntry.
int theEntry = getEntry(4);
// Returns int, awaits two int parameters.
int GetValue(int trackId, int index = 0) {
// pass
}
// Call GetValue(), pass 7 (trackId) and then 7 (trackId) and 4 (index). Result ignored.
GetValue(7); // The parameter index defaults to 0.
GetValue(7, 4); // Both parameters are being satisfied.
10
Functions are called – there is no Code Replacement
● A common misunderstanding: function call leads to "code replacement".
– This is not what's going on!
● The functions A() and B() do always exist, their code is not "merged" somehow.
● Functions are rather procedurally called in C/C++.
● (Code replacement can be achieved with macros in C/C++.)
int A() {
return 2 + 3;
}
void B() {
int result = A();
std::cout<<result<<std::endl;
}
void B() {
int result = 2 + 3; // No!
std::cout<<result<<std::endl;
}NO!B() calls A()
● There is no code replacement going on, it would
lead to code bloat, and thus bigger executables
after compilation.
11
Procedural Calling of Functions
● And again: functions can call other functions.
– When a function completes, execution returns to where the function was called from.
– "Top-level" functions call "lower-level" functions. So, there is a call hierarchy.
int main(int argc, char* argv[]) {
double result = Square(3);
}
#include <cmath>
double Square(double arg) {
return std::pow(arg, 2);
}
namespace std {
double pow(double b, double exp) {
// calculate the pow
return value;
}
}result = 9
(3)
(3, 2)
9
9
12
Recursive Calling of Functions
● Some problems can be described as smaller instances of the same problem.
– In maths such descriptions are called recursive descriptions.
– For example the factorial function (1 x 2 x 3 x 4 … x n):
● Factorial can be expressed as a recursive function in C/C++.
– In maths, recursion means that a equation is defined in terms of itself.
– In programming, recursion means that a function calls itself.
● How to write a recursive function?
– A subproblem of the whole problem must be used as argument for recursive calls.
– And it is needed to consider the base case, when the recursion terminates!
– (The progression of the recursive calls must tend to the base case.)
– If we fail to consider one of these cases, we may end with an infinite recursion!
n!=
{1; n=0
n((n−1)!); n>0
n∈N
● Where to use the "factorial" function?
● E.g. the factorial is used to calculate the count of
permutations of a set of elements.
● Infinite recursions run as long as all the stack
memory of the executing thread is exhausted.
13
Recursive Calling in Action
int main(int argc, char* argv[]) {
int result = Factorial(3);
}
int Factorial(int n) {
if (0 == n) {
return 1;
} else {
return n * Factorial(n – 1);
}
}
int Factorial(int n) {
if (0 == n) {
return 1;
} else {
return n * Factorial(n – 1);
}
}
(3)
(2)
6
2
int Factorial(int n) {
if (0 == n) {
return 1;
} else {
return n * Factorial(n – 1);
}
}
(1)
1
int Factorial(int n) {
if (0 == n) {
return 1;
} else {
return n * Factorial(n – 1);
}
}
(0)
1
0 == n
result = 6
● In the beginning, recursion is difficult to
understand for most people. - They think about all
the calls of a recursive function, in order to
understand, if it is correct or not. They rather
understand a function as a "machine" that
processes inputs and returns outputs. → A better
way to understand a function is that a function is
a process. E.g. "process all documents and
process all documents referred to in these
documents".
14
Various Implementations of the Factorial Algorithm
int FactorialRecursive(int n) {
if (0 == n) {
return 1;
} else {
return n * FactorialRecursive(n – 1);
}
}
int FactorialIterative(int n) {
int result = 1;
for (int i = n; i > 0; --i) {
result *= i;
}
return result;
}
int FactorialNiceRecursive(int n) {
return (0 == n)
? 1
: n * FactorialNiceRecursive(n - 1);
}
n!=
{1; n=0
n((n−1)!); n>0
n∈N
15
Recursion and Recursive Functions in the Wild
● Useful to operate on recursive data. -> Data trees!
– E.g. a TreeView contains trees, that contain subtrees, that contain subtrees...
– Xml data is also a cascading tree of trees.
– Directory and file hierarchies.
– Network analyses, e.g. calculation of network efficiency and network load.
● Complex problems and puzzles can be solved easily with recursion.
– Maths: permutations of a set, numeric differentiation/integration
– Games: Towers of Hanoi, Sudoku, Chess puzzles etc.
● Definition of subproblems to be solved by multiple CPUs independently.
– The idea is to use CPU resources most efficiently -> Divide and conquer!
– Filters in graphics programming.
– Data analyses in networks and databases.
16
Organizing Functions in Namespaces
● So far all user defined functions we saw, were defined as global functions.
● Global functions may clash with equally named/typed other functions.
– The functions of the standard library reside in the namespace std to prevent this.
● Also do namespaces group free functions semantically.
– It turns out, that all free functions should reside in namespaces!
– Don't define global functions (save main()), use namespaces instead!
● C++ namespaces can be understood as directories and functions as files.
– In a file system directories do contain files.
– Files with the same name can reside in different directories. -> No clash!
● Let's examine how to define and use namespaces with the function Factorial()...
● What's a free function?
● And what is its opposite?
17
// Function Factorial in the namespace Nico:
namespace Nico {
int Factorial(int n) {
return (0 == n)
? 1
: n * Factorial(n - 1);
}
}
Defining and using Namespaces in Code
// 1. Alternative: Qualify the function name
// with the namespace's name:
int main(int arc, char* argv[]) {
int result = Nico::Factorial(3);
return EXIT_SUCCESS;
}
// 2. Alternative: Make all functions
// in the namespace Nico global: with a
// using directive:
using namespace Nico;
int main(int arc, char* argv[]) {
int result = Factorial(3);
return EXIT_SUCCESS;
}
// 3. Alternative: Make only functions
// named Nico::Factorial global: with
// a using declaration:
using Nico::Factorial;
int main(int arc, char* argv[]) {
int result = Factorial(3);
return EXIT_SUCCESS;
}
● Mind that namespaces allow us to have any other
function named Factorial() residing in any other
namespace than Nico; they will not clash with
Nico::Factorial()!
18
Separated Function Definitions
● Related functions should be implemented in separate code files. - Why that?
– Each developer can manage just his functions, not interfering with others'.
– On modification, just the separately modified code files must be compiled.
– Very important: The implemented code can be hidden from its callers.
● Separate and collect function definitions. - What's to do?
– Collect related function definitions in own cpp/c-files (summarized "c-files", these are the code files).
– Create h-files with function declarations for the functions in the c-files respectively.
– These "matching" h- and c-files should have the same name.
● The file names should not contain whitespaces or special characters (like umlauts).
– As C/C++ convention, #include the h-files into the belonging cpp/c-files.
● Calling the separated functions. - What's to do?
– In our top-level code file, we've to #include the h-files declaring the functions we need.
– In the code we can call the functions, because the declarations are visible.
● So far the calling side of functions are discussed.
But what's about the function definitions? Where
do they reside? => Now we'll inspect translation
units.
19
Documentation of Function Declarations
● The separation of function declaration and definition leads to information loss.
– In some sense this was the aim (callers should not see the definitions).
– But the mere function declaration is very opaque to us.
● How to improve the situation?
– Self description: Choose meaningful function and parameter names.
– Add documentation/usage comments to function declarations.
// numeric.h
namespace Nico {
/// Calculates the factorial.
/// @param arg – the positive value to calculate the factorial from.
/// @return – the factorial result.
int Factorial(int arg);
}
20
Creation of the Translation Unit by the Preprocessor
// numeric.h
namespace Nico {
/// Calculates the factorial.
/// @param arg – the positive value
/// to calculate the factorial from.
/// @return – the factorial result.
int Factorial(int arg);
}
// Main.cpp
#include "numeric.h"
int main(int arc, char* argv[]) {
int result = Nico::Factorial(3);
return EXIT_SUCCESS;
}
// Main.cpp tu
namespace Nico {
int Factorial(int arg);
}
int main(int arc, char* argv[]) {
int result = Nico::Factorial(3);
return EXIT_SUCCESS;
}
// numeric.cpp
#include "numeric.h"
namespace Nico {
int Factorial(int n) {
return (0 == n)
? 1
: n * Factorial(n - 1);
}
}
21
Separated Compilation and Linkage
● The separated c-files with function definitions need to be compiled also!
– In simple projects, we'll only have one code file (with the definition of main()).
– But in multi-file-projects we need to tell the compiler to compile all!
– Also the linker has to link all the resulting object-files (o-files) to an executable.
● How to negotiate with the compiler and linker about multiple files?
– We can, however, compile all c-files separately, e.g. on the console.
– We can write a make file that defines the build-tasks for compiler and linker.
– => In the end it is recommended to use an IDE to define this stuff in a project.
● Ok! What does a project within an IDE do for us?
– In the project we'll just collect all the h/c-files (and other files) we require.
– The IDE project will be automatically updated, when we add/remove h/c-files.
– Compiler and linker will be automatically prepared to deal with all the project's files.
● What other kinds of files could be managed within
a IDE project?
● E.g. resource files like icons.
22
Getting our Feet wet with IDE Projects and Functions
● Example – Decompose a problem:
– Often the function call std::system("pause") is used to wait for the user on console.
– Possibly this function call should be used by other programs as well.
– That solution is Windows only! It should be expressed by platform neutral means.
– Let us extract this into our own function the "Bottom-Up" way! → Call it Wait().
– We should put this new function into its own namespace!
– Implement with std::system("pause") and test it.
– Then implement it in a platform neutral fashion and test it again.
– => Decomposition complete!
● What does to function call std::system("pause") do
(std::system() is declared in <cstdlib>)?
● Why is this solution Windows only?
23
Thank you!
Ad

More Related Content

What's hot (20)

Verilog Tasks and functions
Verilog Tasks and functionsVerilog Tasks and functions
Verilog Tasks and functions
Vinchipsytm Vlsitraining
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
Swapnil Yadav
 
ParaSail
ParaSail  ParaSail
ParaSail
AdaCore
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
Saket Pathak
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
Appili Vamsi Krishna
 
user defined function
user defined functionuser defined function
user defined function
King Kavin Patel
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
Meghaj Mallick
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
 
Overview of c language
Overview of c languageOverview of c language
Overview of c language
shalini392
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
Functions in C
Functions in CFunctions in C
Functions in C
Shobhit Upadhyay
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rutvik Pensionwar
 
Function in C++
Function in C++Function in C++
Function in C++
Prof Ansari
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Appili Vamsi Krishna
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
Lecture6
Lecture6Lecture6
Lecture6
Dr. Kavita Sharma
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
Swapnil Yadav
 
ParaSail
ParaSail  ParaSail
ParaSail
AdaCore
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
Appili Vamsi Krishna
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
 
Overview of c language
Overview of c languageOverview of c language
Overview of c language
shalini392
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rutvik Pensionwar
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Appili Vamsi Krishna
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 

Viewers also liked (20)

Health heredity powerpoint
Health heredity powerpointHealth heredity powerpoint
Health heredity powerpoint
pokorndog
 
Derechos de los profesores ocasionales r behar(1)
Derechos de los profesores  ocasionales r behar(1)Derechos de los profesores  ocasionales r behar(1)
Derechos de los profesores ocasionales r behar(1)
Martha Lucía Salamanca Solis
 
Publicaciones Digitales
Publicaciones DigitalesPublicaciones Digitales
Publicaciones Digitales
Yeny-Puma
 
Comunicación
ComunicaciónComunicación
Comunicación
adelamiguel30
 
Diagnosis and treatment of fetal cardiac disease aha 2014
Diagnosis and treatment of fetal cardiac disease aha 2014Diagnosis and treatment of fetal cardiac disease aha 2014
Diagnosis and treatment of fetal cardiac disease aha 2014
gisa_legal
 
Epoca contemporanea
Epoca contemporaneaEpoca contemporanea
Epoca contemporanea
venuz_fer
 
Historia
HistoriaHistoria
Historia
UPEL
 
Planeamiento urbano (1)
Planeamiento urbano (1)Planeamiento urbano (1)
Planeamiento urbano (1)
Roger F Ricaldi
 
Grum a
Grum aGrum a
Grum a
Izbell Btncourt
 
Tema 1 2
Tema 1 2Tema 1 2
Tema 1 2
urios
 
T 381-09
T 381-09T 381-09
T 381-09
Jesus Luna
 
A evolução das aplicações no ne
A evolução das aplicações no neA evolução das aplicações no ne
A evolução das aplicações no ne
Jamildo Melo
 
I phone
I phoneI phone
I phone
kattimiranda
 
Guia de HTML
Guia de HTMLGuia de HTML
Guia de HTML
Alvaro Gomes
 
ideas@work vol.2
ideas@work vol.2ideas@work vol.2
ideas@work vol.2
Kip Michael Kelly
 
Desastres naturais vol_i
Desastres naturais vol_iDesastres naturais vol_i
Desastres naturais vol_i
Uirapuru Florêncio
 
Johannes Paulus II: Fides et Ratio
Johannes Paulus II: Fides et RatioJohannes Paulus II: Fides et Ratio
Johannes Paulus II: Fides et Ratio
CSR
 
Portafolio
PortafolioPortafolio
Portafolio
daya1980
 
ÉTICA Y ASPECTOS LEGALES
ÉTICA Y ASPECTOS LEGALESÉTICA Y ASPECTOS LEGALES
ÉTICA Y ASPECTOS LEGALES
dinadelperu
 
Presentación1
Presentación1Presentación1
Presentación1
Ricardo Rodriguez
 
Health heredity powerpoint
Health heredity powerpointHealth heredity powerpoint
Health heredity powerpoint
pokorndog
 
Publicaciones Digitales
Publicaciones DigitalesPublicaciones Digitales
Publicaciones Digitales
Yeny-Puma
 
Diagnosis and treatment of fetal cardiac disease aha 2014
Diagnosis and treatment of fetal cardiac disease aha 2014Diagnosis and treatment of fetal cardiac disease aha 2014
Diagnosis and treatment of fetal cardiac disease aha 2014
gisa_legal
 
Epoca contemporanea
Epoca contemporaneaEpoca contemporanea
Epoca contemporanea
venuz_fer
 
Historia
HistoriaHistoria
Historia
UPEL
 
Tema 1 2
Tema 1 2Tema 1 2
Tema 1 2
urios
 
A evolução das aplicações no ne
A evolução das aplicações no neA evolução das aplicações no ne
A evolução das aplicações no ne
Jamildo Melo
 
Johannes Paulus II: Fides et Ratio
Johannes Paulus II: Fides et RatioJohannes Paulus II: Fides et Ratio
Johannes Paulus II: Fides et Ratio
CSR
 
Portafolio
PortafolioPortafolio
Portafolio
daya1980
 
ÉTICA Y ASPECTOS LEGALES
ÉTICA Y ASPECTOS LEGALESÉTICA Y ASPECTOS LEGALES
ÉTICA Y ASPECTOS LEGALES
dinadelperu
 
Ad

Similar to (3) cpp procedural programming (20)

Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
 
Introduction of function in c programming.pptx
Introduction of function in c programming.pptxIntroduction of function in c programming.pptx
Introduction of function in c programming.pptx
abhajgude
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
Programming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageProgramming Methodologies Functions - C Language
Programming Methodologies Functions - C Language
ChobodiDamsaraniPadm
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
Manzoor ALam
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
lailoesakhan
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
Vivek Singh Chandel
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
C language
C languageC language
C language
Robo India
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
Mukund Trivedi
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Functions
FunctionsFunctions
Functions
Golda Margret Sheeba J
 
Functions
FunctionsFunctions
Functions
Lakshmi Sarvani Videla
 
Unit iii
Unit iiiUnit iii
Unit iii
SHIKHA GAUTAM
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
Dhaval Jalalpara
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Ovidiu Farauanu
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
Sangharsh agarwal
 
Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
 
Introduction of function in c programming.pptx
Introduction of function in c programming.pptxIntroduction of function in c programming.pptx
Introduction of function in c programming.pptx
abhajgude
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
Programming Methodologies Functions - C Language
Programming Methodologies Functions - C LanguageProgramming Methodologies Functions - C Language
Programming Methodologies Functions - C Language
ChobodiDamsaraniPadm
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
Manzoor ALam
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
lailoesakhan
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
Vivek Singh Chandel
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
Dhaval Jalalpara
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Ovidiu Farauanu
 
Ad

More from Nico Ludwig (20)

Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_vGrundkurs fuer excel_part_v
Grundkurs fuer excel_part_v
Nico Ludwig
 
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivGrundkurs fuer excel_part_iv
Grundkurs fuer excel_part_iv
Nico Ludwig
 
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiGrundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iii
Nico Ludwig
 
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiGrundkurs fuer excel_part_ii
Grundkurs fuer excel_part_ii
Nico Ludwig
 
Grundkurs fuer excel_part_i
Grundkurs fuer excel_part_iGrundkurs fuer excel_part_i
Grundkurs fuer excel_part_i
Nico Ludwig
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
Nico Ludwig
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
Nico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
Nico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
Nico Ludwig
 
New c sharp4_features_part_vi
New c sharp4_features_part_viNew c sharp4_features_part_vi
New c sharp4_features_part_vi
Nico Ludwig
 
New c sharp4_features_part_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_v
Nico Ludwig
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
Nico Ludwig
 
New c sharp4_features_part_iii
New c sharp4_features_part_iiiNew c sharp4_features_part_iii
New c sharp4_features_part_iii
Nico Ludwig
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_ii
Nico Ludwig
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
Nico Ludwig
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_v
Nico Ludwig
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iii
Nico Ludwig
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
Nico Ludwig
 
Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_vGrundkurs fuer excel_part_v
Grundkurs fuer excel_part_v
Nico Ludwig
 
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivGrundkurs fuer excel_part_iv
Grundkurs fuer excel_part_iv
Nico Ludwig
 
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiGrundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iii
Nico Ludwig
 
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiGrundkurs fuer excel_part_ii
Grundkurs fuer excel_part_ii
Nico Ludwig
 
Grundkurs fuer excel_part_i
Grundkurs fuer excel_part_iGrundkurs fuer excel_part_i
Grundkurs fuer excel_part_i
Nico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
Nico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
Nico Ludwig
 
New c sharp4_features_part_vi
New c sharp4_features_part_viNew c sharp4_features_part_vi
New c sharp4_features_part_vi
Nico Ludwig
 
New c sharp4_features_part_v
New c sharp4_features_part_vNew c sharp4_features_part_v
New c sharp4_features_part_v
Nico Ludwig
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
Nico Ludwig
 
New c sharp4_features_part_iii
New c sharp4_features_part_iiiNew c sharp4_features_part_iii
New c sharp4_features_part_iii
Nico Ludwig
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_ii
Nico Ludwig
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
Nico Ludwig
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_v
Nico Ludwig
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iii
Nico Ludwig
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
Nico Ludwig
 

Recently uploaded (20)

TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
Play It Safe: Manage Security Risks - Google Certificate
Play It Safe: Manage Security Risks - Google CertificatePlay It Safe: Manage Security Risks - Google Certificate
Play It Safe: Manage Security Risks - Google Certificate
VICTOR MAESTRE RAMIREZ
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Make GenAI investments go further with the Dell AI Factory
Make GenAI investments go further with the Dell AI FactoryMake GenAI investments go further with the Dell AI Factory
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Vibe Coding_ Develop a web application using AI (1).pdf
Vibe Coding_ Develop a web application using AI (1).pdfVibe Coding_ Develop a web application using AI (1).pdf
Vibe Coding_ Develop a web application using AI (1).pdf
Baiju Muthukadan
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
Play It Safe: Manage Security Risks - Google Certificate
Play It Safe: Manage Security Risks - Google CertificatePlay It Safe: Manage Security Risks - Google Certificate
Play It Safe: Manage Security Risks - Google Certificate
VICTOR MAESTRE RAMIREZ
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Make GenAI investments go further with the Dell AI Factory
Make GenAI investments go further with the Dell AI FactoryMake GenAI investments go further with the Dell AI Factory
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
 
The Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdfThe Changing Compliance Landscape in 2025.pdf
The Changing Compliance Landscape in 2025.pdf
Precisely
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Vibe Coding_ Develop a web application using AI (1).pdf
Vibe Coding_ Develop a web application using AI (1).pdfVibe Coding_ Develop a web application using AI (1).pdf
Vibe Coding_ Develop a web application using AI (1).pdf
Baiju Muthukadan
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 

(3) cpp procedural programming

  • 1. 1 Nico Ludwig (@ersatzteilchen) (3) Basics of the C++ Programming Language
  • 2. 2 TOC ● (3) Basics of the C++ Programming Language – Procedural Programming – Predefined and User defined Functions – Declaration and Definition of Functions – Procedural and recursive Function Calling – Namespaces and separated Function Definitions – A Glimpse of Separated Compilation and Translation Units ● Sources: – Bruce Eckel, Thinking in C++ Vol I – Bjarne Stroustrup, The C++ Programming Language
  • 3. 3 Procedural Programming ● Imperative programming reaches its limits very soon. – It is needed to repeat identical code parts all over in the program. – If a repeated part contains a bug, this bug is present on all occurrences! – We have to copy a code part to reuse it, or to share it with other developers. – In general: Don't Repeat Yourself! DRY – DRY in programming results in the the "Top-Down" approach. ● In engineering, complex problems are getting separated into smaller problems. – In programming, problems are broken into procedures or functions. – We call this the "Bottom-Up" approach, or functional decomposition. ● Procedural programming is all about the "Top-Down" and "Bottom-Up" notions. – Top-Down: Programmers rework their code into functions to make it reusable. – Bottom-Up: Programmers code new functions that solve subproblems. ● Procedural programming extends imperative programming with the concept of (reusable) functions.
  • 4. 4 Procedural Programming in C/C++ ● In C/C++, procedural programming is implemented with functions. ● A function is a part of code that can be called independently. – Formerly known as "sub program". – In C/C++ there is no difference between procedures and functions. – In functions, statements can be executed imperatively. – In functions, further functions can be called as well. ● This code style is rather verb-oriented: "do this" and "call that" etc. ● Procedural code in C/C++: – Program execution starts in the function main(), from where other functions are being called. – In fact coding functions is the central activity of C/C++ programmers! ● What is a procedure?
  • 5. 5 Using predefined Functions in own Code ● The C/C++ standard library provides a plethora of functions for us. – The C/C++ standards team already had their "Top-Down" specs on... :-) – These functions allow reusing foreign code to solve own problems. – E.g: std::pow(), std::qsort(), std::bsearch(), std::transform() etc. – Some functions open the gate to the OS, like std::system(). ● How to use functions from the standard library? – #include the respective standard h-file, where the function declaration resides. – (Optionally add a using directive for the namespace std.) – Call the standard function within own code. – (Mind to pass the lib file, where the functions' definition resides, to the linker.) ● "There is a problem with the usage of std::system()." What does this statement mean? ● Well, std::system() accepts commands that are highly platform-dependent in most cases.
  • 6. 6 Declaration and Definition of Functions int GetCount (int x, int y) { // A block of code (function body). } return 42; Type of returned value or void. Function name Parameter list Function bodyReturn value from function. int GetCount (int x, int y = 0);Declaration: Definition: C++11 – trailing return type auto GetCount(int x, int y) -> int { return 42; } int (*)(int, int) The signature of GetCount. Default argument
  • 7. 7 Features of Functions – Returning Values ● Functions can have a return type. – When such a function is called, a value of type "return type" will be returned as a result of that function. – This value can be stored (stuck into a variable) or otherwise consumed. ● Functions w/o return type are declared as returning void instead of a concrete type. – Such functions don't have a result, but a persistent side effect in most cases. ● Functions, which only return values but have no side effects, are sometimes called pure functions. ● When a return statement in a function is executed (w/ or w/o returning a value) a function will beexited immediately! // Store the returned value in a variable... (Mind that we have to write a pair of empty // parentheses, as GetDayOfMonth() accepts no parameters.): int day = GetDayOfMonth(); // …or use it in an expression: std::cout<<"The day is: "<<GetDayOfMonth()<<std::endl; // Finally the function can be called, ignoring the result: GetDayOfMonth(); // On calling ClearScreen(), the side effect happens, then ClearScreen() returns. ClearScreen(); // However, ClearScreen dos not return a value we could process! // A function, which returns an int. int GetDayOfMonth() { // pass } // Clears the screen as side effect, // but doesn't return a value. void ClearScreen() { // pass }
  • 8. 8 Features of Functions – Parameters ● The return type, the parameter types and the order of parameters make up the signature of a function. ● Function definition and declaration can have an optional list of parameters (params). – A parameter is a function's variable storing the value of the passed argument. – A set of functions can have the same name, but differ in the signature's parameter list. ● This handy feature is what we call "function overloading". – The last items of the parameter list can be prepared with default arguments or build a variable argument list. ● On calling a function a list of arguments will be passed to satisfy the params. – If a function requires no parameters, an empty pair of parentheses is written. – Order and type of the arguments must match to the parameters respectively. ● By this matching, the correct (overloaded) function will be found. ● A matching (overloaded) function is resolved at compile time! – Parameters having a default argument, need not to be "satisfied". – Arguments are passed by value by default, so parameters contain copies of the arguments. ● It is not possible to overload return types! ● Btw. selecting function overloads at run time can also be implemented in C++ with so called "multiple dispatch". ● What is the difference between arguments and parameters? ● A parameter is nothing but a local variable that contains the value of the respective argument. Or: an argument is the initial value of the respective parameter. ● The passed argument expressions are evaluated in an unpredictable order!
  • 9. 9 Features of Functions – Calling Functions in a Nutshell // No return type, no parameter list. void clearScreen() { // pass } // Call clearScreen() (mind the pair of empty parentheses), clearScreen() should // perform a side effect. clearScreen(); // No return type, awaits an int parameter. void checkAccount(int accountId) { // pass } // Call checkAccount(), pass 42 as argument. checkAccount(42); // Returns int, awaits an int parameter. int getEntry(int entryID) { // pass } // Call getEntry(), pass 4 as argument, store the returned result in the variable theEntry. int theEntry = getEntry(4); // Returns int, awaits two int parameters. int GetValue(int trackId, int index = 0) { // pass } // Call GetValue(), pass 7 (trackId) and then 7 (trackId) and 4 (index). Result ignored. GetValue(7); // The parameter index defaults to 0. GetValue(7, 4); // Both parameters are being satisfied.
  • 10. 10 Functions are called – there is no Code Replacement ● A common misunderstanding: function call leads to "code replacement". – This is not what's going on! ● The functions A() and B() do always exist, their code is not "merged" somehow. ● Functions are rather procedurally called in C/C++. ● (Code replacement can be achieved with macros in C/C++.) int A() { return 2 + 3; } void B() { int result = A(); std::cout<<result<<std::endl; } void B() { int result = 2 + 3; // No! std::cout<<result<<std::endl; }NO!B() calls A() ● There is no code replacement going on, it would lead to code bloat, and thus bigger executables after compilation.
  • 11. 11 Procedural Calling of Functions ● And again: functions can call other functions. – When a function completes, execution returns to where the function was called from. – "Top-level" functions call "lower-level" functions. So, there is a call hierarchy. int main(int argc, char* argv[]) { double result = Square(3); } #include <cmath> double Square(double arg) { return std::pow(arg, 2); } namespace std { double pow(double b, double exp) { // calculate the pow return value; } }result = 9 (3) (3, 2) 9 9
  • 12. 12 Recursive Calling of Functions ● Some problems can be described as smaller instances of the same problem. – In maths such descriptions are called recursive descriptions. – For example the factorial function (1 x 2 x 3 x 4 … x n): ● Factorial can be expressed as a recursive function in C/C++. – In maths, recursion means that a equation is defined in terms of itself. – In programming, recursion means that a function calls itself. ● How to write a recursive function? – A subproblem of the whole problem must be used as argument for recursive calls. – And it is needed to consider the base case, when the recursion terminates! – (The progression of the recursive calls must tend to the base case.) – If we fail to consider one of these cases, we may end with an infinite recursion! n!= {1; n=0 n((n−1)!); n>0 n∈N ● Where to use the "factorial" function? ● E.g. the factorial is used to calculate the count of permutations of a set of elements. ● Infinite recursions run as long as all the stack memory of the executing thread is exhausted.
  • 13. 13 Recursive Calling in Action int main(int argc, char* argv[]) { int result = Factorial(3); } int Factorial(int n) { if (0 == n) { return 1; } else { return n * Factorial(n – 1); } } int Factorial(int n) { if (0 == n) { return 1; } else { return n * Factorial(n – 1); } } (3) (2) 6 2 int Factorial(int n) { if (0 == n) { return 1; } else { return n * Factorial(n – 1); } } (1) 1 int Factorial(int n) { if (0 == n) { return 1; } else { return n * Factorial(n – 1); } } (0) 1 0 == n result = 6 ● In the beginning, recursion is difficult to understand for most people. - They think about all the calls of a recursive function, in order to understand, if it is correct or not. They rather understand a function as a "machine" that processes inputs and returns outputs. → A better way to understand a function is that a function is a process. E.g. "process all documents and process all documents referred to in these documents".
  • 14. 14 Various Implementations of the Factorial Algorithm int FactorialRecursive(int n) { if (0 == n) { return 1; } else { return n * FactorialRecursive(n – 1); } } int FactorialIterative(int n) { int result = 1; for (int i = n; i > 0; --i) { result *= i; } return result; } int FactorialNiceRecursive(int n) { return (0 == n) ? 1 : n * FactorialNiceRecursive(n - 1); } n!= {1; n=0 n((n−1)!); n>0 n∈N
  • 15. 15 Recursion and Recursive Functions in the Wild ● Useful to operate on recursive data. -> Data trees! – E.g. a TreeView contains trees, that contain subtrees, that contain subtrees... – Xml data is also a cascading tree of trees. – Directory and file hierarchies. – Network analyses, e.g. calculation of network efficiency and network load. ● Complex problems and puzzles can be solved easily with recursion. – Maths: permutations of a set, numeric differentiation/integration – Games: Towers of Hanoi, Sudoku, Chess puzzles etc. ● Definition of subproblems to be solved by multiple CPUs independently. – The idea is to use CPU resources most efficiently -> Divide and conquer! – Filters in graphics programming. – Data analyses in networks and databases.
  • 16. 16 Organizing Functions in Namespaces ● So far all user defined functions we saw, were defined as global functions. ● Global functions may clash with equally named/typed other functions. – The functions of the standard library reside in the namespace std to prevent this. ● Also do namespaces group free functions semantically. – It turns out, that all free functions should reside in namespaces! – Don't define global functions (save main()), use namespaces instead! ● C++ namespaces can be understood as directories and functions as files. – In a file system directories do contain files. – Files with the same name can reside in different directories. -> No clash! ● Let's examine how to define and use namespaces with the function Factorial()... ● What's a free function? ● And what is its opposite?
  • 17. 17 // Function Factorial in the namespace Nico: namespace Nico { int Factorial(int n) { return (0 == n) ? 1 : n * Factorial(n - 1); } } Defining and using Namespaces in Code // 1. Alternative: Qualify the function name // with the namespace's name: int main(int arc, char* argv[]) { int result = Nico::Factorial(3); return EXIT_SUCCESS; } // 2. Alternative: Make all functions // in the namespace Nico global: with a // using directive: using namespace Nico; int main(int arc, char* argv[]) { int result = Factorial(3); return EXIT_SUCCESS; } // 3. Alternative: Make only functions // named Nico::Factorial global: with // a using declaration: using Nico::Factorial; int main(int arc, char* argv[]) { int result = Factorial(3); return EXIT_SUCCESS; } ● Mind that namespaces allow us to have any other function named Factorial() residing in any other namespace than Nico; they will not clash with Nico::Factorial()!
  • 18. 18 Separated Function Definitions ● Related functions should be implemented in separate code files. - Why that? – Each developer can manage just his functions, not interfering with others'. – On modification, just the separately modified code files must be compiled. – Very important: The implemented code can be hidden from its callers. ● Separate and collect function definitions. - What's to do? – Collect related function definitions in own cpp/c-files (summarized "c-files", these are the code files). – Create h-files with function declarations for the functions in the c-files respectively. – These "matching" h- and c-files should have the same name. ● The file names should not contain whitespaces or special characters (like umlauts). – As C/C++ convention, #include the h-files into the belonging cpp/c-files. ● Calling the separated functions. - What's to do? – In our top-level code file, we've to #include the h-files declaring the functions we need. – In the code we can call the functions, because the declarations are visible. ● So far the calling side of functions are discussed. But what's about the function definitions? Where do they reside? => Now we'll inspect translation units.
  • 19. 19 Documentation of Function Declarations ● The separation of function declaration and definition leads to information loss. – In some sense this was the aim (callers should not see the definitions). – But the mere function declaration is very opaque to us. ● How to improve the situation? – Self description: Choose meaningful function and parameter names. – Add documentation/usage comments to function declarations. // numeric.h namespace Nico { /// Calculates the factorial. /// @param arg – the positive value to calculate the factorial from. /// @return – the factorial result. int Factorial(int arg); }
  • 20. 20 Creation of the Translation Unit by the Preprocessor // numeric.h namespace Nico { /// Calculates the factorial. /// @param arg – the positive value /// to calculate the factorial from. /// @return – the factorial result. int Factorial(int arg); } // Main.cpp #include "numeric.h" int main(int arc, char* argv[]) { int result = Nico::Factorial(3); return EXIT_SUCCESS; } // Main.cpp tu namespace Nico { int Factorial(int arg); } int main(int arc, char* argv[]) { int result = Nico::Factorial(3); return EXIT_SUCCESS; } // numeric.cpp #include "numeric.h" namespace Nico { int Factorial(int n) { return (0 == n) ? 1 : n * Factorial(n - 1); } }
  • 21. 21 Separated Compilation and Linkage ● The separated c-files with function definitions need to be compiled also! – In simple projects, we'll only have one code file (with the definition of main()). – But in multi-file-projects we need to tell the compiler to compile all! – Also the linker has to link all the resulting object-files (o-files) to an executable. ● How to negotiate with the compiler and linker about multiple files? – We can, however, compile all c-files separately, e.g. on the console. – We can write a make file that defines the build-tasks for compiler and linker. – => In the end it is recommended to use an IDE to define this stuff in a project. ● Ok! What does a project within an IDE do for us? – In the project we'll just collect all the h/c-files (and other files) we require. – The IDE project will be automatically updated, when we add/remove h/c-files. – Compiler and linker will be automatically prepared to deal with all the project's files. ● What other kinds of files could be managed within a IDE project? ● E.g. resource files like icons.
  • 22. 22 Getting our Feet wet with IDE Projects and Functions ● Example – Decompose a problem: – Often the function call std::system("pause") is used to wait for the user on console. – Possibly this function call should be used by other programs as well. – That solution is Windows only! It should be expressed by platform neutral means. – Let us extract this into our own function the "Bottom-Up" way! → Call it Wait(). – We should put this new function into its own namespace! – Implement with std::system("pause") and test it. – Then implement it in a platform neutral fashion and test it again. – => Decomposition complete! ● What does to function call std::system("pause") do (std::system() is declared in <cstdlib>)? ● Why is this solution Windows only?

Editor's Notes

  • #5: What is a procedure?
  • #6: &amp;quot;There is a problem with the usage of std::system().&amp;quot; What does this statement mean? Well, std::system() accepts commands that are highly platform-dependent in most cases.
  • #9: It is not possible to overload return types! Btw. selecting function overloads at run time can also be implemented in C++ with so called &amp;quot;multiple dispatch&amp;quot;. What is the difference between arguments and parameters? A parameter is nothing but a local variable that contains the value of the respective argument. Or: an argument is the initial value of the respective parameter. The passed argument expressions are evaluated in an unpredictable order!
  • #11: There is no code replacement going on, it would lead to code bloat, and thus bigger executables after compilation.
  • #13: Where to use the &amp;quot;factorial&amp;quot; function? E.g. the factorial is used to calculate the count of permutations of a set of elements. Infinite recursions run as long as all the stack memory of the executing thread is exhausted.
  • #14: In the beginning, recursion is difficult to understand for most people. - They think about all the calls of a recursive function, in order to understand, if it is correct or not. They rather understand a function as a &amp;quot;machine&amp;quot; that processes inputs and returns outputs. → A better way to understand a function is that a function is a process. E.g. &amp;quot;process all documents and process all documents referred to in these documents&amp;quot;.
  • #17: What&amp;apos;s a free function? And what is its opposite?
  • #18: Mind that namespaces allow us to have any other function named Factorial() residing in any other namespace than Nico; they will not clash with Nico::Factorial()!
  • #19: So far the calling side of functions are discussed. But what&amp;apos;s about the function definitions? Where do they reside? =&amp;gt; Now we&amp;apos;ll inspect translation units.
  • #22: What other kinds of files could be managed within a IDE project? E.g. resource files like icons.
  • #23: What does to function call std::system(&amp;quot;pause&amp;quot;) do (std::system() is declared in &amp;lt;cstdlib&amp;gt;)? Why is this solution Windows only?