Lecture 18
Lecture 18
Lec-11
1
Computer Application/ 4
functions
A function is a block of instructions that is
executed when it is called from some other point
of the program. The following is its format:
type function _name ( argument1, argument2,
...)
{
statement(s);
}
Computer Application/4 2
where:
• type is the type of data returned by the
function.
• name is the name by which it will be possible to
call the function.
• arguments (as many as wanted can be
specified).
• statement is the function's body. It can be a
single instruction or a block of instructions. In
the latter case it must be delimited by curly
brackets { }.
Computer Application/4 3
Here is a plausible definition of square.
Computer Application/4 4
The first line of this definition tells us that this is a
function (that’
s what the parentheses mean), that
it is called square, that it takes an int argument
(here, called x), and that it returns an int (the type
of the result always comes first in a function
declaration); that is, we can use it like this:
Computer Application/4 5
main( )
{
int a;
a= square(2);
/ / or
cout << square(2) << '\ n'; / / print 4
cout << square(10) << '\ n'; / / print 100
}
Computer Application/4 6
int addition (int a , int b)
{ int r;
r=a+b;
return (r);
}
main ( )
{int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
Computer Application/4 7
In a C++ program always begins its execution
with the main function. So we will begin there.
Computer Application/4 8
If we pay attention we will be able to see the
similarity between the structure of the call to the
function and the declaration of the function itself
in the code lines above:
Computer Application/4 9
The parameters have a clear correspondence.
Within the main function we called to addition
passing two values: 5 and 3 that correspond to
the int a and int b parameters declared for the
function addition.
Computer Application/4 10
At the moment at which the function is called
from main, control is lost by main and passed to
function addition. The value of both parameters
passed in the call (5 and 3) are copied to the local
variables int a and int b within the function.
Computer Application/4 11
Function addition declares a new variable (int r;),
and by means of the expression r=a+b; it assigns
to r the result of a plus b. Because the passed
parameters for a and b are 5 and 3 respectively,
the result is 8.
Computer Application/4 12
The following line of code:
return (r);
Computer Application/4 13
But additionally, return was called with the
content of variable r (return (r);), which at that
moment was 8, so this value is said to be
returned by the function.
Computer Application/4 14
The value returned by a function is the value
given to the function when it is evaluated.
Therefore, z will store the value returned by
addition (5, 3), that is 8.
Computer Application/4 15
To explain it another way, you can imagine that
the call to a function (addition (5,3)) is literally
replaced by the value it returns (8).
Computer Application/4 16
The following line of code in main is:
Computer Application/4 17
Scope of variables
Computer Application/4 18
For example, in the previous example it had been
impossible to use the variables a, b or r directly in
function main since they were local variables to
function addition.
Computer Application/4 19
Also, it had been impossible to use the variable z
directly within function addition, since this was a
local variable to the function main.
Computer Application/4 20
Computer Application/4 21
Therefore, the scope of local variables is
limited to the same nesting level in which
they are declared. Nevertheless you can also
declare global variables that are visible from
any point of the code, inside and outside any
function.
Computer Application/4 22
In order to declare global variables you must
do it outside any function or block of
instructions, that means, directly in the body
of the program.
Computer Application/4 23
int add1 (int a, int b)
{ int r;
r=a+b;
return r;
}
int sub1 (int a, int b)
{int r;
r=a-b;
return r;
}
Computer Application/4 24
main ( )
{
int z , w;
z = add1(5,3);
cout << "The result is " << z <<'\ n';
w=sub1(10,5);
cout << "The result is " << w <<'\n';
return 0;
}
Computer Application/4 25
int r ;
int add1 (int a, int b)
{
r=a+b;
return r;
}
int sub1 (int a, int b)
{
r=a-b;
return r;
}
Computer Application/4 26