Chapter II. Modular Programming in C++
Chapter II. Modular Programming in C++
Hà Nội, 2020 1
Chapter II. Modular programming in C++
2
Chapter II. Modular programming in C++
3
Chapter II. Modular programming in C++
4
The basics of functions
5
The basics of functions
• In C++, a function is a unit (a subprogram) that has a name and that can
be called from the other parts of a program as many times as it is
needed.
• In order to use a function efficiently, some of its inner variables
(parameters) are assigned a value when the function is called.
• When a function is called (activated), the values (arguments) to be
assigned to each parameter have to be passed in a similar way.
• The called function passes control back to the place where it was called
by a return statement.
• The value of the expression in the return statement is the return
value returned back by the function, which is the result of the function
call expression.
6
The basics of functions
7
The basics of functions
Function definition
8
The basics of functions
9
The basics of functions
10
The basics of functions
Function definition
11
The basics of functions
13
The basics of functions
14
The basics of functions
15
The basics of functions
16
The basics of functions
❖ Parametrizing functions
• A parameter can be scalar (bool, char, wchar_t, short, int, long, long long,
float, double, enumeration, reference and pointer) or structure, union, class
or array.
The general form of polynomials:
Horner's scheme
17
The basics of functions
❖ Parametrizing functions
▪ Parameter passing methods
• Passing parameters by value
18
The basics of functions
❖ Parametrizing functions
▪ Parameter passing methods
• Passing parameters by value
19
The basics of functions
❖ Parametrizing functions
▪ Parameter passing methods
• Passing parameters by reference
20
The basics of functions
❖ Parametrizing functions
▪ Parameter passing methods
• Passing parameters by reference
21
The basics of functions
❖ Parametrizing functions
▪ Parameter passing methods
• Passing parameters by reference
22
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Arithmetic type parameters
23
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Arithmetic type parameters
24
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• User-defined type parameters
25
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• User-defined type parameters
26
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Passing arrays to functions
27
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Passing arrays to functions
28
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Passing arrays to functions
29
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Passing arrays to functions
30
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Passing arrays to functions
31
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• String arguments
32
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• String arguments
33
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• String arguments
34
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• String arguments
35
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Functions as arguments
36
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Functions as arguments
37
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Default arguments
Parameters
Call
n d a0
SeqSum() 10 1 1
SeqSum(12) 12 1 1
SeqSum(12,3) 12 3 1
SeqSum(12, 3, 7) 12 3 7
38
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Variable length argument list
39
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Variable length argument list
40
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Parameters and return value of the main() function
41
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Parameters and return value of the main() function
Providing
command
line
arguments
42
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Parameters and return value of the main() function
43
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Parameters and return value of the main() function
44
The basics of functions
❖ Parametrizing functions
▪ Using parameters of different types
• Parameters and return value of the main() function
45
The basics of functions
46
The basics of functions
47
The basics of functions
48
The basics of functions
49
The basics of functions
50
The basics of functions
51
The basics of functions
52
The basics of functions
53
The basics of functions
more efficient
54
The basics of functions
more efficient
55
The basics of functions
56
The basics of functions
57
The basics of functions
58
The basics of functions
59
Chapter II. Modular programming in C++
60
How to use functions on a more professional level?
❖ Inline functions
❖ Overloading (redefining) function names
❖ Function templates
61
How to use functions on a more professional level?
❖ Inline functions
• C++ compilers decrease the time spent on calling the functions
marked with the keyword inline.
• This solution is recommended to be used for small-sized and
frequently called functions.
62
How to use functions on a more professional level?
❖ Inline functions
63
How to use functions on a more professional level?
❖ Inline functions
❖ Overloading (redefining) function names
❖ Function templates
64
How to use functions on a more professional level?
65
How to use functions on a more professional level?
66
How to use functions on a more professional level?
❖ Inline functions
❖ Overloading (redefining) function names
❖ Function templates
67
How to use functions on a more professional level?
❖ Function templates
▪ Creating and using function templates
• A template declaration starts with the keyword template,
followed by the parameters of the template enclosed within the
signs < and >.
68
How to use functions on a more professional level?
❖ Function templates
▪ Creating and using function templates
69
How to use functions on a more professional level?
❖ Function templates
▪ Creating and using function templates
70
How to use functions on a more professional level?
❖ Function templates
▪ Function template instantiation
• A function template can be instantiated in an explicit way as well
if concrete types are provided in the template line containing the
header of the function:
71
How to use functions on a more professional level?
❖ Function templates
▪ Function template specialization
72
How to use functions on a more professional level?
❖ Function templates
▪ Some further function template examples
73
How to use functions on a more professional level?
❖ Function templates
▪ Some further function template examples
74
How to use functions on a more professional level?
❖ Function templates
▪ Some further function template examples
75
Chapter II. Modular programming in C++
76
Namespaces and storage classes
77
Namespaces and storage classes
78
Namespaces and storage classes
79
Namespaces and storage classes
A file level variable is only visible in the module containing its declaration.
Identifiers having file level scope are those that are declared outside the functions
file level
of the module and that are declared with internal linkage using the static storage
class.
A program level variable is accessible from the functions of all the modules (all
program compilation units) of a program.
level Global variables that are declared outside the functions of a module (that is
with external linkage) have program level scope. 80
Namespaces and storage classes
Variable scopes
81
Namespaces and storage classes
automatic Within blocks, variables defined without the static storage class and
lifetime the parameters of functions have automatic (local) lifetime.
83
Namespaces and storage classes
84
Namespaces and storage classes
85
Namespaces and storage classes
86
Namespaces and storage classes
87
Namespaces and storage classes
88
Namespaces and storage classes
89
Namespaces and storage classes
90
Namespaces and storage classes
91
Namespaces and storage classes
92
Namespaces and storage classes
93
Namespaces and storage classes
94
Namespaces and storage classes
95
Namespaces and storage classes
❖ Namespaces
▪ The default namespaces of C++ and the scope operator
• C++ enclose Standard library elements in the namespace
called std.
• In order to be able to refer the elements of a namespace, we
have to know how to use the scope operator (::).
• With the help of :: operator, we can refer names having file and
program level scope (that is identifiers of the global namespace)
from any block of a program.
96
Namespaces and storage classes
❖ Namespaces
▪ The default namespaces of C++ and the scope operator
97
Namespaces and storage classes
❖ Namespaces
▪ Creating and using user-defined namespaces
• Creating namespaces
98
Namespaces and storage classes
❖ Namespaces
▪ Creating and using user-defined namespaces
• Accessing the identifiers of a namespace
Directly by using the scope operator: Or by using the directive using namespace:
99
Namespaces and storage classes
❖ Namespaces
▪ Creating and using user-defined namespaces
• Accessing the identifiers of a namespace
Or by providing using -declarations:
100
Namespaces and storage classes
❖ Namespaces
▪ Creating and using user-defined namespaces
• Nested namespaces, namespace aliases:
101
Namespaces and storage classes
❖ Namespaces
▪ Creating and using user-defined namespaces
• Nested namespaces, namespace aliases:
102
Namespaces and storage classes
❖ Namespaces
▪ Creating and using user-defined namespaces
• Nested namespaces, namespace aliases:
103
Namespaces and storage classes
• Anonymous namespaces:
104
Chapter II. Modular programming in C++
105
Preprocessor directives of C++
❖ Including files
❖ Conditional compilation
❖ Using macros
107
Preprocessor directives of C++
❖ Including files
The following table sums up the keywords and language elements that
can be used in header files:
C++ elements Example
Comments // comment
Conditional directives #ifdef MOTOROLA
Macro definitions #define INTEL
#include directives #include <string>
Enumerations enum response {no, yes, maybe};
Constant definitions const double pi=3.1415265; 108
Preprocessor directives of C++
❖ Including files
❖ Including files
The following elements should never be placed in include files:
110
Preprocessor directives of C++
❖ Including files
➢ One part of the elements that can be placed in a header file have
to be included only once in a code.
➢ That is why, all header files have to have a special preprocessing
structure based on conditional directives:
111
Preprocessor directives of C++
❖ Including files
❖ Conditional compilation
❖ Using macros
112
Preprocessor directives of C++
❖ Conditional compilation
• The code parts to be compiled under conditions can be selected
in many ways, by using the following preprocessor directives:
#if, #ifdef, #ifndef, #elif, #else and #endif.
113
Preprocessor directives of C++
❖ Conditional compilation
• Instead of the structure above, it is better to use a solution that
examines the definition of the symbol TEST.
114
Preprocessor directives of C++
❖ Conditional compilation
• Each pair of the following checkings return the same results:
115
Preprocessor directives of C++
❖ Conditional compilation
• If the following structure is used, we can choose between two
code parts:
116
Preprocessor directives of C++
❖ Conditional compilation
• The following example creates the two-dimensional array named
array depending on the value of the symbol SIZE:
117
Preprocessor directives of C++
❖ Conditional compilation
• For more complex structures, it is recommended to use multi-
way branches.
118
Preprocessor directives of C++
❖ Conditional compilation
• The following example integrates the declaration file on the basis
of the manufacturer:
119
Preprocessor directives of C++
❖ Including files
❖ Conditional compilation
❖ Using macros
120
Preprocessor directives of C++
❖ Using macros
▪ Symbolic constants
• Symbolic constants can be created by using the simple form of
the #define directive:
121
Preprocessor directives of C++
❖ Using macros
▪ Symbolic constants
122
Preprocessor directives of C++
❖ Using macros
▪ Parameterized macros
• The macros can be efficiently used in much more cases if they
are parameterized. The general form of a function-like
parameterized macro:
123
Preprocessor directives of C++
❖ Using macros
▪ Parameterized macros
124
Preprocessor directives of C++
❖ Using macros
▪ Parameterized macros
• All advantages of macros (type-independence, faster code) can
be achieved by using inline functions/function templates
(instead of macros) while keeping the C++ code legible:
125
Preprocessor directives of C++
❖ Using macros
▪ Undefining a macro
• A macro can be undefined anytime and can be redefined again,
even with a different content. It can be undefined by using
the #undef directive.
Original source code Substituted code
int main() {
#define MACRO(x) (x) + 7
int main() {
int a = MACRO(12);
int a = (12) + 7;
#undef MACRO
a = MACRO(12);
a = MACRO(12);
a = 123
#define MACRO 123
}
a = MACRO
}
126
Preprocessor directives of C++
❖ Using macros
▪ Macro operators
• If the # character is placed before the parameter in a macro, the
value of the parameter is replaced as enclosed within quotation
marks (i.e. as a string).
127
Preprocessor directives of C++
❖ Using macros
▪ Macro operators
• By using the ## operator, two syntactic units (tokens) can be
concatenated.
128
Preprocessor directives of C++
❖ Using macros
▪ Predefined macros
Macro Description Example
__DATE__ String constant containing the date of the compilation. "Oct 02 2013"
❖ Using macros
▪ #line, #error and #pragma directives
• The #line directive forces C++ compilers not to signal the error
code in the C++ source text but in the original source file written
in another special language.
130
Preprocessor directives of C++
❖ Using macros
▪ #line, #error and #pragma directives
• An #error directive can be used to print out a compilation error
message which contains the text provided in the statement:
#error error_message
131
Preprocessor directives of C++
❖ Using macros
▪ #line, #error and #pragma directives
• #pragma directives are used to control the compilation process
in an implementation-dependent way.
• The empty directive (#) can also be used, but it does not affect
preprocessing.
132