type name(parameters) {
code
}
The initial type is the function return value type.
If the function has no return value, the return type is void.
This is followed by the name and
finally, within parentheses, the parameters of the function.
The return command exits the function.
If the function has a return value,
it is given after this command.
The function must be defined before it is called in another function.
line function prints a line,
with n stars:
void line(int n) {
for (int i = 1; i <= n; i++) {
cout << "*";
}
cout << "\n";
}
The following function smaller
returns the smaller of its parameters:
int smaller(int a, int b) {
if (a < b) return a;
else return b;
}
Here's how to use the functions in the main program:
int main() {
line(5); // *****
line(8); // ********
cout << smaller(5,3) << "\n"; // 3
}
void test(int x) {
x++;
cout << x << "\n"; // 2
}
int main() {
int a = 1;
test(a);
cout << a << "\n"; // 1
}
However, if a parameter is preceded in the function definition by an
& character, it is passed as reference so that the
parameter is not copied and any changes made in the function are reflected in the caller:
void test(int &x) {
x++;
cout << x << "\n"; // 2
}
int main() {
int a = 1;
test(a);
cout << a << "\n"; // 2
}
In the following example, the function line(n,c)
prints a line containing the character c repeated n times,
and the line(n) function prints a line of n stars.
The latter function is implemented as a special case of the former.
void line(int n, char c) {
for (int i = 1; i <= n; i++) {
cout << c;
}
cout << "\n";
}
void line(int n) {
line(n,'*');
}
int main() {
line(5); // *****
line(5,'@'); // @@@@@
}
A variable becomes global when you define it at the main program level (at the same level as the functions). A global variable can be used in all functions starting from the definition point. In addition, the global variable automatically gets an initial value 0 if it is not given a value when it is defined.
For example, the following code contains the global variable x,
which is used in two functions:
int x; // initial value 0
void test() {
x++;
}
int main() {
test();
cout << x << "n"; // 1
}
A variable defined in a function is local,
and can be used from the definition point
inside the block where it is defined
(that is, in the area between the { and } characters).
For example, in the following code snippet, the variable x
is defined inside an if block and is therefore
no longer available after the end of the block.
if (...) {
int x = 5;
cout << x << "\n"; // 5
}
cout << x << "\n"; // error!
Multiple variables can have the same name,
as long as they have a different scope.
When this is the case, the most local scope possible of the specified variable is taken in each situation.
For example, in the following code, the variable x
is defined on three different levels.
The block inside the function
not related to a condition or a loop -
its only purpose is to create a scope.
int x = 1;
void test() {
int x = 2;
{
int x = 3;
cout << x << "\n"; // 3
}
cout << x << "\n"; // 2
}
int main() {
test();
cout << x << "\n"; // 1
}