Sei sulla pagina 1di 6

LECTURE NUMBER 11

Functions
Objectives :
To develop and implement functions creating well structured C++ programs.
To appreciate modular approach in programming using functions.
Discussion :
Using functions we can structure our programs in a more modular way, accessing all the potential
that structured programming can offer to us in C++.
A function is a group of statements that is executed when it is called from some point of the
program. The following is its format:
type name ( parameter1, parameter2, ...) { statement }
where:
o type - is the data type specifier of the data returned y the function !optional".
o name - is the identifier y which it will e possile to call the function.
o parameters !as many as needed": #ach parameter consists of a data type specifier
followed y an identifier, li$e any regular variale declaration !for example: int x" and
which acts within the function as a regular local variale. They allow to pass arguments
to the function when it is called. The different parameters are separated y commas.
o statements is the function%s ody. &t is a loc$ of statements surrounded y races ' (.
)ere you have the first function example:
** function example
+include ,iostream-
using namespace std.
int addition !int a, int "
'
int r.
r/a+.
return !r".
(
int main !"
'
int 0.
0 / addition !1,2".
cout ,, 3The result is 3 ,, 0.
return 4.
(
The result is 5
&n order to examine this code, first of all rememer something said at the eginning of this
semester : a C++ program always begins its execution by the main function. 6o we will egin
there.
7e can see how the main function egins y declaring the variale 0 of type int. 8ight after that,
we see a call to a function called addition. 9aying attention we will e ale to see the similarity
etween the structure of the call to the function and the declaration of the function itself some
code lines aove:
The parameters and arguments have a clear correspondence. 7ithin the main function we called
to addition passing two values: 1 and 2, that correspond to the int a and int parameters declared
for function addition.
At the point at which the function is called from within main, the control is lost y main and
passed to function addition. The value of oth arguments passed in the call !1 and 2" are copied to
the local variales int a and int within the function.
:unction addition declares another local variale !int r", and y means of the expression r/a+, it
assigns to r the result of a plus . ;ecause the actual parameters passed for a and are 1 and 2
respectively, the result is 5.
The following line of code:
finali0es function addition, and returns the control ac$ to the function that called it in the first
place !in this case, main". At this moment the program follows it regular course from the same
point at which it was interrupted y the call to addition. ;ut additionally, ecause the return
statement in function addition specified a value: the content of variale r !return !r".", which at
that moment had a value of 5. This value ecomes the value of evaluating the function call.
6o eing the value returned y a function the value given to the function call itself when it is
evaluated, the variable z will be set to the value returned by addition (! "#! that is $% To explain it
another way, you can imagine that the call to a function !addition !1,2"" is literally replaced y the
value it returns !5".
The following line of code in main is:
return !r".
cout ,, 3The result is 3 ,, 0.
That, as you may already expect, produces the printing of the result on the screen.
Scope of Variables
The scope of variales declared within a function or any other inner loc$ is only their own
function or their own loc$ and cannot e used outside of them. :or example, in the previous
example it would have een impossile to use the variales a, or r directly in function main
since they were variales local to function addition. Also, it would have een impossile to use
the variale 0 directly within function addition, since this was a variale local to the function
main.
Therefore, the scope of local variales is limited to the same loc$ level in which they are
declared. <evertheless, we also have the possiility to declare gloal variales. These are visile
from any point of the code, inside and outside all functions. &n order to declare gloal variales
you simply have to declare the variale outside any function or loc$. that means, directly in the
ody of the program.
And here is another example aout functions:
&n this case we have created a function called sutraction. The only thing that this function does is
to sutract oth passed parameters and to return the result.
<evertheless, if we examine function main we will see that we have made several calls to
function sutraction. 7e have used some different calling methods so that you see other ways or
moments when a function can e called.
&n order to understand well these examples you must consider once again that a call to a function
could e replaced y the value that the function call itself is going to return. :or example, the first
case !that you should already $now ecause it is the same pattern that we have used in previous
examples":
&f we replace the function call y the it returns !i.e., 1", we would have:
** function example
+include ,iostream-
using namespace std.
int sutraction !int a, int "
'
int r.
r/a-.
return !r".
(
int main !"
'
int x/1, y/2, 0.
0 / sutraction !=,>".
cout ,, 3The first result is 3 ,, 0 ,, %?n%.
cout ,, 3The second result is 3 ,, sutraction !=,>" ,, %?n%.
cout ,, 3The third result is 3 ,, sutraction !x,y" ,, %?n%.
0/ @ + sutraction !x,y".
cout ,, 3The fourth result is 3 ,, 0 ,, %?n%.
return 4.
(
The first result is 1
The second result is 1
The third result is >
The fourth result is A
0 / sutraction !=,>".
cout ,, 3The first result is 3 ,, 0.
0 / 1.
cout ,, 3The first result is 3 ,, 0.
As well as
has the same result as the previous call, ut in this case we made the call to sutraction directly as
an insertion parameter for cout. 6imply consider that the result is the same as if we had written:
since 1 is the value returned y sutraction !=,>".
&n the case of:
The only new thing that we introduced is that the parameters of sutraction are variales instead
of constants. That is perfectly valid. &n this case the values passed to function sutraction are the
values of x and y, that are 1 and 2 respectively, giving > as result.
The fourth case is more of the same. 6imply note that instead of:
we could have written:
with exactly the same result. & have switched places so you can see that the semicolon sign !."
goes at the end of the whole statement. &t does not necessarily have to go right after the function
call. The explanation might e once again that you imagine that a function can e replaced y its
returned value:
Functions with No ype. he use of !oi".
&f you rememer the syntax of a function declaration:
type name ( argument1, argument2 ...) statement
you will see that the declaration egins with a type, that is the type of the function itself !i.e., the
type of the datum that will e returned y the function with the return statement". ;ut what if we
want to return no valueB
cout ,, 3The second result is 3 ,, sutraction !=,>".
cout ,, 3The second result is 3 ,, 1.
cout ,, 3The third result is 3 ,, sutraction !x,y".
0 / @ + sutraction !x,y".
0 / sutraction !x,y" + @.
0 / @ + >.
0 / > + @.
&magine that we want to ma$e a function Cust to show a message on the screen. 7e do not need it
to return any value. &n this case we should use the void type specifier for the function. This is a
special specifier that indicates asence of type.
void can also e used in the function%s parameter list to explicitly specify that we want the
function to ta$e no actual parameters when it is called. :or example, function printmessage could
have een declared as:
Although it is optional to specify void in the parameter list. &n C++, a parameter list can simply
e left lan$ if we want a function with no parameters.
7hat you must always rememer is that the format for calling a function includes specifying its
name and enclosing its parameters etween parentheses. The non-existence of parameters does
not exempt us from the oligation to write the parentheses. :or that reason the call to
printmessage is:
The parentheses clearly indicate that this is a call to a function and not the name of a variale or
some other C++ statement. The following call would have een incorrect:
** void function example
+include ,iostream-
using namespace std.
void printmessage !"
'
cout ,, 3&%m a functionD3.
(
int main !"
'
printmessage !".
return 4.
(
&%m a functionD
void printmessage !void"
'
cout ,, 3&%m a functionD3.
(

printmessage !".


printmessage.