Hochiminh University of Technology
Computer Science and Engineering - [CO1011]
Fundamentals of
C++ Programming
Function and
Pointer
Lecturer: Duc Dung Nguyen
Credits: 4
Outcomes
❖
Solving problems with functions
❖
Understand recursive algorithms
❖
Declare and implement recursive functions
❖
Declare and using pointers
2
Outline
❖
Function: definition, declaration, parameters, returned value
❖
Scope of variables
❖
Storage
❖
Pointer
❖
Recursion
3
Function
Function
❖
You should never write monolithic code
❖
Difficult to write correctly.
❖
Difficult to debug.
❖
Difficult to extend.
❖
Hard to maintenance
❖
Non-reusable
❖
Nonsense!
5
Function
❖
Math vs. Computer Software
❖
A function can return no value
❖
A function can take many different types of parameters
❖
A function can set as many output as it needs
Input
Function
6
Output
Function
❖
Definition: a group of statements that is given a name, and which can be
called from some point of the program.
❖
Syntax:
❖
<type> <name>(
);
❖
<type> <name>() { <statements> }
❖
<type>: the value returned by the function
❖
<name>: name of function
❖
: each parameter consists of a type followed by an identifier
7
Function
❖
<type>
❖
❖
The function can return any type
❖
At some point, it must return a value
❖
return x;
The function can return nothing (sometimes it is called procedure)
❖
No need for the return statement.
❖
return statement can be used to end the function.
8
Function
❖
Example
#include <iostream>
#include <math.h>
int generateArrayValue(int range) {
return rand() % range;
}
int main() {
int img[12][16];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 16; j++) {
img[i][j] = generateArrayValue(256);
}
}
return 0;
}
9
Function
❖
Example
#include <iostream>
#include <math.h>
float add(float, float);
int main() {
float img[12][16];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 16; j++) {
img[i][j] = add(rand() % 256, (rand() % 100) * 0.01);
}
}
return 0;
}
float add(float a, float b) {
return a + b;
}
10
Function
❖
Example
#include <iostream>
#include <math.h>
void printArray(float*, int);
void printArray(float* pA, int N) {
for (int i = 0; i < N; i++) {
cout << pA[i] << “ ”;
}
}
float add(float a, float b) {
return a + b;
}
int main() {
float img[12][16];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 16; j++) {
img[i][j] = add(rand() % 256, (rand() % 100) * 0.01);
}
}
printArray(img[2], 16);// print the third line of two dimensions array
return 0;
}
11
Function
❖
Name
❖
Many functions can have the same name: overloaded functions.
❖
Functions with the same name must not share the same prototype
❖
Function signature: name + parameter list
❖
Provide convenience for programmer
12
Function
❖
Example
#include <iostream>
#include <math.h>
float add(float a, float b) {
return a + b;
}
int add(int a, int b) {
return a + b;
}
double add(int a, double b) {
return (double)a + b;
}
int main() {
double k = 3.14159;
cout << add(3.0, 5.2) << endl;
cout << add(3, -8) << endl;
cout << add(1, k) << endl;
return 0;
}
13
Function
❖
Parameters: there are two ways to pass parameters to a function
❖
Value: the value will be copied to local variable (parameter) of the function
❖
Reference (only in C++): the parameter is associated with passed variable
❖
User can only pass variables through a reference parameter
❖
Any change in the parameter affects the variable
14
Function
❖
Example
#include <iostream>
#include <math.h>
float add(float a, float b) {
b += 1;
return a + b - 1;
}
float foo(int a, float &b) {
b *= a;
return b;
}
int main() {
float x = 2.5, y = 3.14159;
cout << add(3.1, 6.08) << endl;
x = foo(2, y);
cout << x << endl;
cout << y << endl;
return 0;
}
15
Function
❖
main:
❖
Default return value of main: 0 - the program executed successfully
❖
stdlib.h/cstdlib:
❖
EXIT_SUCCESS: same as default return value
❖
EXIT_FAILURE: the program failed
16
Function
❖
Parameter passing:
❖
C++ allows user pass parameters by value or by reference
❖
❖
Unlike C++, everything in Java is pass-by-value.
❖
❖
If user pass a parameter using reference, it will be translated to pointer
Think about what happens in the background.
C++ allows user pass default values to parameters
17
Function
❖
Example
#include <iostream>
#include <math.h>
float add(float a, float b = 1.0f) {
return a + b;
}
int main() {
float x = 2.5;
cout << “add: x + 6.08 = ” << add(x, 6.08) << endl;
cout << “increase x: x + 1 = ” << add(x) << endl;
return 0;
}
18
Function
❖
Reuse functions
❖
Define prototype in header file (.h): <type> <name>();
❖
Must export the function if it was build in a library.
❖
❖
Use export/import instructions: depend on platform and language
Static linked libraries vs. Dynamic linked libraries
19
Function
❖
Why do you need function prototype?
❖
To reuse a function written in another module
❖
To solve tricky situations
20
Function
❖
inline functions
❖
Similar to function, except that the compiled code will be inserted where
we call inline functions.
❖
Purpose: improve performance
❖
inline <return type> <function name>() {
<function body>
}
21
Scope of Variables
Scope of Variables
❖
In C/C++, the variable is effective in the scope of declaration statement
❖
In C: all variables must be declared at the beginning of the function. No
initialization in declaration.
❖
In C++: variables can be declared anywhere and take effect in the declared
scope
❖
void test() {
for (int i = 0; i < 5;) { i += 2; }
i = 10;// error
}
23
Scope of Variables
❖
Global vs. Local variables
❖
Global variables can be accessed everywhere in the function without
declaration
❖
Local variables can only be accessed inside the scope where it is declared
24
Scope of Variables
❖
Global vs. Local variables
#include <iostream>
#include <math.h>
float defaultFactor;
float mul(float a, float b, bool useGlobal = false) {
return useGlobal? a * defaultFactor: a * b;
}
int main() {
defaultFactor = 2.0f;
cout << “Use default factor: ” << mul(3.14159, 0, true) << endl;
cout << “Multiply pi by 5: ” << mul(3.14159, 5.0f) << endl;
return 0;
}
25