L 01 Procedural
L 01 Procedural
Mikhail Nesterenko
Procedural Programming
without classes: using standalone functions,
primitive types and simple control structures
Before We Go
• a computer program is governed by rules
• what are
– syntax rules?
– semantic rules?
– stylistic rules?
3
Types, Variables
• primitive (basic/built-in) types – string, int, bool, char, double
– exist but won’t need
• int may be long or short, signed or unsigned
• char may be signed or unsigned
• floating point types may be float, double or long double
– auto – type to be determined by compiler (C++11)
auto i =7; // nice for type of loop variables
could also be auto & and const auto
– decltype(expr) – type is same as expr (C++11)
decltype(i) j =8;
4
Operators, Arity
operators – built-in functions
• fixed number of operands (parameters)
5
Expressions
6
Conditionals and Loops
• conditionals
– if/else
– switch
– ternary expression
• loops
– while
– do-while
– for
• for(init_statement; expression; post_statement)
action
• range-based-for (C++11)
int a[] = {10,20,30,40};
for(auto e: a)
cout << e;
can be auto &e or const auto &e
how do I write a range-based-for to add 5 to each element of the array?
7
Functions
• what is function invocation/call/definition/declaration?
• what is the difference between argument and parameter?
• in procedural programming functions are standalone
• function definition – includes head and body (implementation)
• function overloading – multiple different functions within the same scope
provided that they have different signatures
double max(double, double);
int max(int, int);
• resolution (of function call) – compile time process of associating function
invocation with function declaration
int i = max(10, 20); // resolved to second declaration
• client/caller – function that invokes another function
– when dealing with programming patterns, function that uses the pattern
8
Function Parameters, Default Values
• parameters may be passed by value, by reference, what’s the difference?
• default values
– default parameter value may be specified at function declaration
void move(int from, int to=0, int by =1);
or definition, but not both
– convention – specify at declaration.
– client has an option of specifying parameter or using default value
move (2, 3, 4);
move (2, 3);
move (2);
10
References (cont.)
reference can be used to return values
• in which case function can be used on the left-hand side of
assignment
int& getElement(int x[], int i) {
return x[i];
}
...
int a[] = {10, 20, 30};
11
Pointers and Functions
function may also return a pointer
int* getElement(int x[], int i) {
return &x[i];
}
...
int a[] = {10, 20, 30};
cout << *getElement(a, 2);
*getElement(a, 5) = 55;
12
nullptr
• nullptr – null pointer constant (C++11 addition)
– is of type pointer unlike NULL and 0, which are integer type
• what’s wrong with NULL and zero?
void myFunc(int); // overloaded function
void myFunc(char *);
13
Strings
14
Static Variables
• static variables are initialized only once and retain their value across function
invocations
void login() {
static int number1 = 0;
int number2 = 0;
++number1; ++number2;
cout << number1 << number2;
}
int main(){
login();
login();
login();
}
• prints 112131
15