07 Functions
07 Functions
Tian-Li Yu
[email protected]
➢ Understand functions
⚫ create
⚫ invoke
⚫ pass parameters to
➢ Pass-by-value, pass-by-reference
➢ Function overloading
➢ Function prototypes & header files
➢ Scope of local and global variables
➢ Inline functions
2
Introducing Functions
3
Introducing Functions (contd.)
4
Example: max
#include <iostream>
using namespace std;
/** Return the max between two
numbers */ int main() {
int max(int num1, int num2) { int i = 5;
int result; int j = 2;
if (num1 > num2) int k = max(i, j);
result = num1; cout << "The maximum is " << k;
else return 0;
result = num2; }
return result;
}
5
Struct
6
Calling Functions
7
Calling Functions (contd.)
i is now 5
8
Calling Functions (contd.)
j is now 2
9
Calling Functions (contd.)
Invoke max(i, j)
10
Calling Functions (contd.)
invoke max(i, j)
Pass the value of i to num1 (num1=5)
Pass the value of j to num2 (num2=2)
11
Calling Functions (contd.)
return result, which is 5
12
Calling Functions (contd.)
Assign the return value to k
int k = 5;
13
Call Stacks
14
Trace Call Stack
15
Trace Call Stack (contd.)
16
Trace Call Stack (contd.)
17
Trace Call Stack (contd.)
18
Trace Call Stack (contd.)
19
Trace Call Stack (contd.)
20
Trace Call Stack (contd.)
21
Trace Call Stack (contd.)
22
Trace Call Stack (contd.)
23
Pass By Value
TestPassByValue
24
Pass By Value (contd.)
25
Reference Variables
26
Pass By Reference
++ref_Count;
cout << "count is " << count << endl;
cout << "ref_Count is " << ref_Count << endl;
TestPassByReference
27
Note
28
Overloading Functions
TestFunctionOverloading
29
Ambiguous Invocation
30
Ambiguous Invocation (contd.)
int main() {
cout << maxNumber(1, 2) << endl;
return 0;
}
31
Function Prototypes
33
Function Prototypes (contd.)
int f3(int num);
int f2(int num);
int f1(int num);
// Return (base^exponent)
double pow(double base, double exponent);
DefaultArgumentDemo
36
Example: Root Finding By Bisection
BisectionF
37
Recursive Call
➢ Golden rule
⚫ Base case first
⚫ Then recursive call
38
Example: Factorial
39
Tracing Factorial
40
Do Not Abuse Recursion
➢ Fibonacci
⚫ a0 = 0, a1 = 1.
⚫ an = an-1 + an-2
int fibonacci(int x) {
if (x==0) return 0;
if (x==1) return 1;
return fibonacci(x-2)+ fibonacci(x-1);
}
42
Do Not Abuse Recursion (contd.)
43
Practices
GCD
Double^Int
44
Header Files
45
Header File Examples
#include <iostream>
➢ UseMyLib.cpp #include "MyLib.h"
➢ MyLib.cpp and
UseMyLib.cpp in one project.
47
Keyword: #define
➢ #define N 4
int b = a * N; int b = a * 4;
#endif
51
Scope of Local Variables (contd.)
52
Scope of Local Variables (contd.)
53
Scope of Local Variables (contd.)
54
Global Variables
➢ Notes:
⚫ Do not rely on “defaulted to zero.” (Always initialize)
⚫ Using global variables is usually not recommended.
VariableScopeDemo
55
Unary Scope Resolution
StaticVariableDemo
57
Function Abstraction
58
Benefits of Functions
59
Math Functions
60
Divide and Conquer
➢ Problem decomposition.
PrintCalendar
61
Design Diagram
printMonthName
62
Implementation: Top-Down
63
Implementation: Bottom-Up
65
Inline Function Example
#include <iostream>
using namespace std;
inline void f(int month, int year) {
cout << "month is " << month << endl;
cout << "year is " << year << endl;
}
int main() {
int month = 10, year = 2008;
f(month, year);
return 0;
}
66
Final Notes About Inline
67
Summary
➢ Pass-by-value, pass-by-reference.
➢ Inline functions.
➢ Divide-and-conquer methodology.
68