
please wait
type name ( parameter1, parameter2, ...) { statements }
type
is the type of the value returned by the function.name
is the identifier by which the function can be called.parameters
(as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: int x
), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.statements
is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.
|
|
The result is 8 |
addition
and main
. Remember that no matter the order in which they are defined, a C++ program always starts by calling main
. In fact, main
is the only function called automatically, and the code in any other function is only executed if its function is called from main
(directly or indirectly).main
begins by declaring the variable z
of type int
, and right after that, it performs the first function call: it calls addition
. The call to a function follows a structure very similar to its declaration. In the example above, the call to addition
can be compared to its definition just a few lines earlier:5
and 3
, to the function; these correspond to the parameters a
and b
, declared for function addition
.addition
: here, execution of main
is stopped, and will only resume once the addition
function ends. At the moment of the function call, the value of both arguments (5
and 3
) are copied to the local variables int a
and int b
within the function.addition
, another local variable is declared (int r
), and by means of the expression r=a+b
, the result of a
plus b
is assigned to r
; which, for this case, where a
is 5 and b
is 3, means that 8 is assigned to r
.
|
|
addition
, and returns the control back to the point where the function was called; in this case: to function main
. At this precise moment, the program resumes its course on main
returning exactly at the same point at which it was interrupted by the call to addition
. But additionally, because addition
has a return type, the call is evaluated as having a value, and this value is the value specified in the return statement that ended addition
: in this particular case, the value of the local variable r
, which at the moment of the return
statement had a value of 8.addition
is an expression with the value returned by the function, and in this case, that value, 8, is assigned to z
. It is as if the entire function call (addition(5,3)
) was replaced by the value it returns (i.e., 8).
|
|
|
|
The first result is 5 The second result is 5 The third result is 2 The fourth result is 6 |
addition
function in the previous example, this example defines a subtract
function, that simply returns the difference between its two parameters. This time, main
calls this function several times, demonstrating more possible ways in which a function can be called.
|
|
|
|
|
|
|
|
subtraction (7,2)
.
|
|
x
and y
have at the moment of the call: 5 and 3 respectively, returning 2 as result.
|
|
|
|
|
|
type name ( argument1, argument2 ...) { statements }
void
, which is a special type to represent the absence of value. For example, a function that simply prints a message may not need to return any value:
|
|
I'm a function! |
void
can also be used in the function's parameter list to explicitly specify that the function takes no actual parameters when called. For example, printmessage
could have been declared as:
|
|
void
with same meaning, but the use of void
in the argument list was popularized by the C language, where this is a requirement.printmessage
was called in an earlier example:
|
|
|
|
main
is int
, but most examples in this and earlier chapters did not actually return any value from main
.main
ends normally without encountering a return
statement the compiler assumes the function ends with an implicit return statement:
|
|
main
for historical reasons. All other functions with a return type shall end with a proper return
statement that includes a return value, even if this is never used.main
returns zero (either implicitly or explicitly), it is interpreted by the environment as that the program ended successfully. Other values may be returned by main
, and some environments give access to that value to the caller in some way, although this behavior is not required nor necessarily portable between platforms. The values for main
that are guaranteed to be interpreted in the same way on all platforms are:value | description |
---|---|
0 | The program was successful |
EXIT_SUCCESS | The program was successful (same as above). This value is defined in header <cstdlib> . |
EXIT_FAILURE | The program failed. This value is defined in header <cstdlib> . |
return 0;
statement for main
is a tricky exception, some authors consider it good practice to explicitly write the statement.
|
|
x
and y
, respectively. These values (5 and 3) are used to initialize the variables set as parameters in the function's definition, but any modification of these variables within the function has no effect on the values of the variables x and y outside it, because x and y were themselves not passed to the function on the call, but only copies of their values at that moment.duplicate
in this code duplicates the value of its three arguments, causing the variables used as arguments to actually be modified by the call:
|
|
x=2, y=6, z=14 |
&
) following the parameter type, as in the parameters taken by duplicate
in the example above.a
, b
, and c
become aliases of the arguments passed on the function call (x
, y
, and z
) and any change on a
within the function is actually modifying variable x
outside the function. Any change on b
modifies y
, and any change on c
modifies z
. That is why when, in the example, function duplicate
modifies the values of variables a
, b
, and c
, the values of x
, y
, and z
are affected.
|
|
|
|
x
, y
, and z
without being modified (i.e., 1, 3, and 7).int
, but if the parameter is of a large compound type, it may result on certain overhead. For example, consider the following function:
|
|
a
and b
to be copies of the arguments passed to the function when it is called. And if these are long strings, it may mean copying large quantities of data just for the function call.
|
|
concatenate
taking references is more efficient than the version taking values, since it does not need to copy expensive-to-copy strings.
|
|
const
, the function is forbidden to modify the values of neither a
nor b
, but can actually access their values as references (aliases of the arguments), without having to make actual copies of the strings.const
references provide functionality similar to passing arguments by value, but with an increased efficiency for parameters of large types. That is why they are extremely popular in C++ for arguments of compound types. Note though, that for most fundamental types, there is no noticeable difference in efficiency, and in some cases, const references may even be less efficient!inline
specifier informs the compiler that inline expansion is preferred over the usual function call mechanism for a specific function. This does not change at all the behavior of a function, but is merely used to suggest the compiler that the code generated by the function body shall be inserted at each point the function is called, instead of being invoked with a regular function call.
|
|
concatenate
is called, the program prefers the function to be expanded inline, instead of performing a regular call. inline
is only specified in the function declaration, not when it is called.inline
specifier. Therefore, this specifier merely indicates the compiler that inline is preferred for this function, although the compiler is free to not inline it, and optimize otherwise. In C++, optimization is a task delegated to the compiler, which is free to generate any code for as long as the resulting behavior is the one specified by the code.
|
|
6 5 |
divide
. In the first one:
|
|
int b=2
). Therefore, the result is 6.
|
|
b
(int b=2
) is ignored, and b
takes the value passed as argument, that is 4, yielding a result of 5.x
cannot be used before being declared with a statement, such as:
|
|
main
function, which is the function from where the other functions were called. If main
were defined before the other functions, this would break the rule that functions shall be declared before being used, and thus would not compile.protofunction
with two int parameters can be declared with either of these statements:
|
|
|
|
Please, enter number (0 to exit): 9 It is odd. Please, enter number (0 to exit): 6 It is even. Please, enter number (0 to exit): 1030 It is even. Please, enter number (0 to exit): 0 It is even. |
|
|
void
in this case). With these prototype declarations in place, they can be called before they are entirely defined, allowing for example, to place the function from where they are called (main
) before the actual definition of these functions.odd
and even
are mutually called; there is a call to even
in odd
and a call to odd
in even
. And, therefore, there is no way to structure the code so that odd
is defined before even
, and even
before odd
.n!
) the mathematical formula would be:n! = n * (n-1) * (n-2) * (n-3) ... * 1
5!
(factorial of 5) would be:5! = 5 * 4 * 3 * 2 * 1 = 120
|
|
9! = 362880 |
![]() Statements and flow control | ![]() Index | ![]() Overloads and templates |