A function is a sequence of statements designed to do a particular job. You already know that every program must have a function named main(). However, most programs have many functions, and they all work analogously to main.
Often, your program needs to interrupt what it is doing to temporarily do something else. You do this in real life all the time. For example, you might be reading a book when you remember you need to make a phone call. You put a bookmark in your book, make the phone call, and when you are done with the phone call, you return to your book where you left off.
C++ programs work the same way. A program will be executing statements sequentially inside one function when it encounters a function call. A function call is an expression that tells the CPU to interrupt the current function and execute another function. The CPU “puts a bookmark” at the current point of execution, and then calls (executes) the function named in the function call. When the called function terminates, the CPU goes back to the point it bookmarked, and resumes execution.
Here is a sample program that shows how new functions are declared and called:
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;
}
|
This program produces the following output:
This program begins execution at the top of main(), and the first line to be executed prints 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()
.
Functions can be called multiple times:
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;
}
|
This program produces the following output:
In this case, main() is interrupted 3 times, once for each call to DoPrint().
Main isn’t the only function that can call other functions. In the following example, DoPrint() calls a second function, DoPrint2().
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;
}
|
This program produces the following output: