1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| //#include <stdafx.h> // Visual Studio users need to uncomment this line #include <iostream> // Declaration of function DoPrint() void DoPrint() { using namespace std; // we need this in each function that uses cout and endl cout << "In DoPrint()" << endl; } // Declaration of main() int main() { using namespace std; // we need this in each function that uses cout and endl cout << "Starting main()" << endl; DoPrint(); // This is a function call to DoPrint() cout << "Ending main()" << endl; return 0; } |
Starting main()
. The second line in main is a function call to DoPrint. At this point, execution of statements in main() is suspended, and the CPU jumps to DoPrint(). The first (and only) line in DoPrint prints In DoPrint()
. When DoPrint() terminates, the caller (main()) resumes execution where it left off. Consequently, the next statment executed in main prints Ending main()
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| //#include <stdafx.h> // Visual Studio users need to uncomment this line #include <iostream> // Declaration of function DoPrint() void DoPrint() { using namespace std; cout << "In DoPrint()" << endl; } // Declaration of main() int main() { using namespace std; cout << "Starting main()" << endl; DoPrint(); // This is a function call to DoPrint() DoPrint(); // This is a function call to DoPrint() DoPrint(); // This is a function call to DoPrint() cout << "Ending main()" << endl; return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| //#include <stdafx.h> // Visual Studio users need to uncomment this line #include <iostream> void DoPrint2() { using namespace std; cout << "In DoPrint2()" << endl; } // Declaration of function DoPrint() void DoPrint() { using namespace std; cout << "Starting DoPrint()" << endl; DoPrint2(); // This is a function call to DoPrint2() DoPrint2(); // This is a function call to DoPrint2() cout << "Ending DoPrint()" << endl; } // Declaration of main() int main() { using namespace std; cout << "Starting main()" << endl; DoPrint(); // This is a function call to DoPrint() cout << "Ending main()" << endl; return 0; } |
1
2
3
4
5
6
7
8
9
10
11
| // void means the function does not return a value to the caller void ReturnNothing() { // This function does not return a value } // int means the function returns an integer value to the caller int Return5() { return 5; } |
1
2
3
| cout << Return5(); // prints 5 cout << Return5() + 2; // prints 7 cout << ReturnNothing(); // This will not compile |
int main()
.void main()
. Technically this is illegal. When these compilers see void main()
, they interpret it as:
1
2
3
4
5
| int main() { // your code here return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| //#include <stdafx.h> // Visual Studio users need to uncomment this line #include <iostream> // add takes two integers as parameters, and returns the result of their sum // add does not care what the exact values of x and y are int add( int x, int y) { return x + y; } int main() { using namespace std; // It is the caller of add() that decides the exact values of x and y cout << add(4, 5) << endl; // x=4 and y=5 are the parameters return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| //#include <stdafx.h> // Visual Studio users need to uncomment this line #include <iostream> int add( int x, int y) { return x + y; } int multiply( int z, int w) { return z * w; } int main() { using namespace std; cout << add(4, 5) << endl; // evalutes 4 + 5 cout << add(3, 6) << endl; // evalues 3 + 6 cout << add(1, 8) << endl; // evalues 1 + 8 int a = 3; int b = 5; cout << add(a, b) << endl; // evaluates 3 + 5 cout << add(1, multiply(2, 3)) << endl; // evalues 1 + (2 * 3) cout << add(1, add(2, 3)) << endl; // evalues 1 + (2 + 3) return 0; } |
1
2
3
| int a = 3; int b = 5; cout << add(a, b) << endl; // evaluates 3 + 5 |
1
| cout << add(1, multiply(2, 3)) << endl; // evalues 1 + (2 * 3) |
add(1, multiply(2, 3)) => add(1, 6) => 7
1
| cout << add(1, add(2, 3)) << endl; // evalues 1 + (2 + 3) |
add(1, add(2, 3)) => add(1, 5) => 6
- Reading inputs from the user
- Calculating a value from the inputs
- Printing the calculated value
1
2
3
4
5
6
7
8
9
10
| void multiply( int x, int y) { return x * y; } int main() { cout << multiply(4, 5) << endl; return 0; } |
1
2
3
4
5
6
7
8
9
10
| int multiply( int x, int y) { int product = x * y; } int main() { cout << multiply(4, 5) << endl; return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| int add( int x, int y, int z) { return x + y + z; } int multiply( int x, int y) { return x * y; } int main() { cout << multiply(add(1, 2, 3), 4) << endl; return 0; } |
0 comments: