0% found this document useful (0 votes)
12 views22 pages

Chapter Two - Functions

1. A function is a block of code that performs a specific task and can be called from different parts of a program. There are two types of functions: predefined/library functions that come with the compiler and programmer-defined functions created by the programmer. 2. Functions make programs easier to understand, write, modify, test and reuse code. Functions are defined with a return type, name, parameters, and code block. When called, the code in the function is executed. 3. Variables declared within a function are local to that function and cannot be accessed from outside the function. Global variables declared outside of functions can be accessed from any part of the program.

Uploaded by

amareadios60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views22 pages

Chapter Two - Functions

1. A function is a block of code that performs a specific task and can be called from different parts of a program. There are two types of functions: predefined/library functions that come with the compiler and programmer-defined functions created by the programmer. 2. Functions make programs easier to understand, write, modify, test and reuse code. Functions are defined with a return type, name, parameters, and code block. When called, the code in the function is executed. 3. Variables declared within a function are local to that function and cannot be accessed from outside the function. Global variables declared outside of functions can be accessed from any part of the program.

Uploaded by

amareadios60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

1.

Chapter Two:Functions
A function is a block of instructions that is executed when it is called from some other point of the
program.

There are two types of functions

1. Pre-defined functions (library functions)

2. Programmer (user) defined functions

1.1.Pre-defined functions
✓ Are those functions that come with C++ compiler. We can use them without knowing how
they are defined.

Example sqrt, abs, pow, delay, clrscr, getch, rand etc.

✓ If we use any library function in our program, we must include the header file using the include
statement.

✓ Syntax to include header file.

# include < filename>

Examples: the following list shows frequently used header files and corresponding functions

Header file functions

iostream.h cin, cout

conio.h getch, clrscr

math.h sqrt, abs, pow, ceil, floor

1.2.Programmer (User) defined functions


These are functions, which are defined, and used by the programmer

The Need for function


A natural way to solve large problems is to break them down into a series of sub-problems, which
can be solved more-or-less independently and then combined to arrive at a complete solution. In
programming, this methodology reflects itself in the use of sub-programs, and in C++ all sub-
programs are called functions (corresponding to both "functions" and "procedures" in Pascal and
some other programming languages).

1.3.Advantage of functions
• Make our program easily understandable

• Make the program easier to write, modify and test

• Facilitate task sharing (group work)

• Are re-usable.

Syntax

type name ( argument1, argument2, ...) statement

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). Each argument consists of a type of data
followed by its identifier, like in a variable declaration (for example, int x) and which acts within
the function like any other variable. They allow passing parameters to the function when it is
called. The different parameters are separated by commas.
· statement is the function's body. It can be a single instruction or a block of instructions.
Here you have the first function example:

// function example The result is 8


#include <iostream.h>

int addition (int a, int b)

int r;

r=a+b;

return (r);

int main ()

int z;

z = addition (5,3);

cout << "The result is " << z;

return 0;

In order to examine this code, first of all remember that a C++ program always begins its execution
with the main function. So we will begin there.

We can see how the main function begins by declaring the variable z of type int. Right after that
we see a call to addition function. 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:

int addition (int a, int b)

z=addition ( 5 , 3)
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.

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.

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.

The following line of code:

return (r);

finalizes function addition, and returns the control back to the function that called it (main)
following the program from the same point at which it was interrupted by the call to addition. 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.

int addition (int a, int b)

8
z=addition ( 5 , 3)

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. 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).

The following line of code in main:

cout << "The result is " << z;

produces the printing of the result on the screen.


1.4.Scope of variables
You must consider that the scope of variables declared within a function or any other block of
instructions is only their own function or their own block of instructions and cannot be used outside
of them. 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. Also, it had been
impossible to use the variable z directly within function addition, since this was a local variable to
the function main.

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. 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.
And here is another example about functions:

// function example The first result is 5


The second result is 5
#include <iostream.h> The third result is 2
The fourth result is 6
int subtraction (int a, int b)

int r;

r=a-b;

return (r);

int main ()

int x=5, y=3, z;

z = subtraction (7,2);

cout << "The first result is " << z << '\n';

cout << "The second result is " << subtraction (7,2) << '\n';

cout << "The third result is " << subtraction (x,y) << '\n';

z= 4 + subtraction (x,y);

cout << "The fourth result is " << z << '\n';

return 0;

In this case we have created the function subtraction. The only thing that this function does is to
subtract both passed parameters and to return the result.

Nevertheless, if we examine the function main we will see that we have made several calls to
function subtraction. We have used some different calling methods so that you see other ways or
moments when a function can be called.
In order to understand well these examples you must consider once again that a call to a function
could be perfectly replaced by its return value. For example the first case (that you should already
know because it is the same pattern that we have used in previous examples):

z = subtraction (7,2);
cout << "The first result is " << z;

If we replace the function call by its result (that is 5), we would have:

z=5;
cout << "The first result is " << z;

As well as

cout << "The second result is " << subtraction (7,2);

has the same result as the previous call, but in this case we made the call to subtraction directly as
a parameter for cout. Simply imagine that we had written:

cout << "The second result is " << 5;

since 5 is the result of subtraction (7,2).

In the case of

cout << "The third result is " << subtraction (x,y);

The only new thing that we introduced is that the parameters of subtraction are variables instead
of constants. That is perfectly valid. In this case the values passed to the function subtraction are
the values of x and y, that are 5 and 3 respectively, giving 2 as result.

The fourth case is more of the same. Simply note that instead of:

z = 4 + subtraction (x,y);

we could have put:

z = subtraction (x,y) + 4;

with exactly the same result. Notice that the semicolon sign (;) goes at the end of the whole
expression. It does not necessarily have to go right after the function call. The explanation might
be once again that you imagine that a function can be replaced by its result:
z = 4 + 2;
z = 2 + 4;

1.5.Functions with no types (void functions)


If you remember the syntax of a function declaration:

type name ( argument1, argument2 ...) statement

you will see that it is obligatory that this declaration begins with a type, that is the type of the data
that will be returned by the function with the return instruction. But what if we want to return no
value?

Imagine that we want to make a function just to show a message on the screen. We do not need it
to return any value; moreover, we do not need it to receive any parameters. For these cases, the
void type was devised in the C language. Take a look at:

// void function example Wel come to St. Mary’s College!


#include <iostream.h>

void dummyfunction (void)

cout << "Wel come to St. Mary’s College!";

int main ()

dummyfunction ();

return 0;

Although in C++ it is not necessary to specify void, its use is considered suitable to signify that it
is a function without parameters or arguments and not something else.
What you must always be aware of is that the format for calling a function includes specifying its
name and enclosing the arguments between parentheses. The non-existence of arguments does not
exempt us from the obligation to use parenthesis. For that reason the call to dummyfunction is

dummyfunction ();

This clearly indicates that it is a call to a function and not the name of a variable or anything else.

1.6.Arguments passed by value and by reference.


Until now, in all the functions we have seen, the parameters passed to the functions have been
passed by value. This means that when calling a function with parameters, what we have passed
to the function were values but never the specified variables themselves. For example, suppose
that we called function addition using the following code:

int x=5,y=3,z;
z = addition ( x , y );

What we did in this case was to call function addition passing the values of x and y, that means 5
and 3 respectively, not the variables themselves.

This way, when function addition is being called the value of its variables a and b become 5 and 3
respectively, but any modification of a or b within the function addition will not affect the values
of x and y outside it, because variables x and y were not passed themselves to the function, only
their values.

But there might be some cases where you need to manipulate from inside a function the value of
an external variable. For that purpose we have to use arguments passed by reference, as in the
function duplicate of the following example:
// passing parameters by reference x=2, y=6, z=14
#include <iostream.h>

void duplicate (int& a, int& b, int& c)

a*=2;

b*=2;

c*=2;

int main ()

int x=1, y=3, z=7;

duplicate (x, y, z);

cout << "x=" << x << ", y=" << y << ", z=" << z;

return 0;

The first thing that should call your attention is that in the declaration of duplicate the type of each
argument was followed by an ampersand sign (&), that serves to specify that the variable has to
be passed by reference instead of by value, as usual.

When passing a variable by reference we are passing the variable itself and any modification that
we do to that parameter within the function will have effect in the passed variable outside it.
To express it another way, we have associated a, b and c with the parameters used when calling
the function (x, y and z) and any change that we do on a within the function will affect the value
of x outside. Any change that we do on b will affect y, and the same with c and z.

That is why our program's output, that shows the values stored in x, y and z after the call to
duplicate, shows the values of the three variables of main doubled.

If when declaring the following function:

void duplicate (int& a, int& b, int& c)

we had declared it thus:

void duplicate (int a, int b, int c)

that is, without the ampersand (&) signs, we would have not passed the variables by reference, but
their values, and therefore, the output on screen for our program would have been the values of x,
y and z without having been modified.

Passing by reference is an effective way to allow a function to return more than one single value.
For example, here is a function that returns the previous and next numbers of the first parameter
passed.

// more than one returning value int main ()

#include <iostream.h> {

void prevnext (int x, int& prev, int& next) int x=100, y, z;

{ prevnext (x, y, z);

prev = x-1; cout << "Previous=" << y << ", Next=" << z;

next = x+1; return 0;

} }

Previous=99, Next=101
1.7.Default values in arguments.
When declaring a function we can specify a default value for each parameter. This value will be
used if that parameter is left blank when calling to the function. To do that we simply have to
assign a value to the arguments in the function declaration. If a value for that parameter is not
passed when the function is called, the default value is used, but if a value is specified this default
value is stepped on and the passed value is used. See the following example:

// default values in functions 6


5
#include <iostream.h>

int divide (int a, int b=2)

int r;

r=a/b;

return (r);

int main ()

cout << divide (12);

cout << endl;

cout << divide (20,4);

return 0;

As we can see in the body of the program there are two calls to the function divide. In the first one:

divide (12)
we have only specified one argument, but the function divide allows up to two. So the function
divide has assumed that the second parameter is 2 since that is what we have specified to happen
if this parameter is lacking (notice the function declaration, which finishes with int b=2). Therefore
the result of this function call is 6 (12/2).

In the second call:

divide (20,4)

there are two parameters, so the default assignation (int b=2) is stepped on by the passed parameter,
that is 4, making the result equal to 5 (20/4).

1.8.Overloaded functions.
Two different functions can have the same name if the prototype of their arguments are different,
that means that you can give the same name to more than one function if they have either a different
number of arguments or different types in their arguments. For example,

// overloaded function
2
#include <iostream.h> 2.5
int divide (int a, int b)
{
return (a/b);
}
float divide (float a, float b)
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << divide (x,y);
cout << "\n";
cout << divide (n,m);
cout << "\n";
return 0;
}
In this case we have defined two functions with the same name, but one of them accepts two
arguments of type int and the other accepts them of type float. The compiler knows which one to
call in each case by examining the types when the function is called. If it is called with two ints as
arguments it calls to the function that has two int arguments in the prototype and if it is called with
two floats it will call to the one which has two floats in its prototype.

For simplicity we have included the same code within both functions, but this is not compulsory.
You can make two functions with the same name but with completely different behaviors.

1.9.Inline functions.
When we define a function, normally the compiler creates just one set of instructions in memory.
When we call the function, execution of the program jumps to those instructions, and when the
function returns, execution jumps back to the next line in the calling function. If we call the
function 10 times, our program jumps to the same set of instructions each time. This means there
is only one copy of the function, not 10.

There is some performance overhead in jumping in and out of functions. It turns out that some
functions are very small, just a line or two of code, and some efficiency can be gained if the
program can avoid making these jumps just to execute one or two instructions. When programmers
speak of efficiency, they usually mean speed: the program runs faster if the function call can be
avoided.

If a function is declared with the keyword inline, the compiler does not create a real function: it
copies the code from the inline function directly into the calling function. No jump is made; it is
just as if we had written the statements of the function right into the calling function.

Note that inline functions can bring a heavy cost. If the function is called 10 times, the inline code
is copied into the calling functions each of those 10 times. The small improvement in speed we
might achieve is more than swamped by the increase in size of the executable program. First,
today's optimizing compilers do a terrific job on their own,

and there is almost never a big gain from declaring a function inline. More important, the increased
size brings its own performance cost.
Syntax:

inline type name (arguments ...) {instructions ... }

For example:

int Double (int target)

return 2*target;

1.10. Recursion

A function that calls itself is said to be recursive. Recursion is a general programming technique
applicable to problems that can be defined in terms of themselves.

Some problems are most easily solved by recursion, usually those in which we act on data and
then act in the same way on the result. For example, to obtain the factorial of a number (n) its
mathematical formula is:

Factorial of 0 is 1.

Factorial of n that is n! = n * (n-1) * (n-2) * (n-3) ... * 1

The second line clearly indicates that factorial is defined in terms of itself and hence can be
expressed as a recursive function:
// factorial calculator Type a number: 9
!9 = 362880
#include <iostream.h>

long factorial (long a)

if (a > 1)

return (a * factorial (a-1));

else

return (1);

int main ()

long i;

cout << "Type a number: ";

cin >> i;

cout << "!" << i << " = " << factorial (i);

return 0;

}
A recursive function must have at least one termination condition which can

be satisfied. Otherwise, the function will call itself indefinitely until the runtime stack overflows.
The Factorial function, for example, has the termination condition n ==0 which, when satisfied,
causes the recursive calls to fold back. (Note that for a negative n this condition will never be
satisfied and Factorial will fail).

As a general rule, all recursive functions can be rewritten using iteration. In

situations where the number of stack frames involved may be quite large, the

iterative version is preferred. In other cases, the elegance and simplicity of the

recursive version may give it the edge.

For factorial, for example, a very large argument will lead to as many stack

frames. An iterative version is therefore preferred in this case:

long Factorial (long int n)

long result = 1;

while (n > 0) result *= n--;

return result;

1.11. Prototyping functions.

Until now, we have defined all of the functions before the first appearance of calls to them that
generally was in main leaving the function main for the end. If we try to repeat some of the
examples of functions described so far, but placing the function main before any other function
that is called from within it, we will most likely obtain an error. The reason is that to be able to
call a function it must have been declared previously (it must be known), like we have done in all
our examples.
But there is an alternative way to avoid writing all the code of all functions before they can be used
in main or in another function. It is by prototyping functions. This consists in making a previous
shorter, but quite significant, declaration of the complete definition so that the compiler can know
the arguments and the return type needed.

Syntax:

type name ( argument_type1, argument_type2, ...);

It is identical to the header of a function definition, except:

• It does not include a statement for the function. That means that it does not include the
body with all the instructions that are usually enclose within curly brackets { }.

• It ends with a semicolon sign (;).

• In the argument enumeration it is enough to put the type of each argument. The inclusion
of a name for each argument as in the definition of a standard function is optional, although
recommended.
For example:
Type a number (0 to exit): 9
// prototyping
Number is odd.
#include <iostream.h>

void odd (int a); Type a number (0 to exit): 6

void even (int a); Number is even.

int main () Type a number (0 to exit): 1030

{ Number is even.

int i; Type a number (0 to exit): 0

do { Number is even.

cout << "Type a number: (0 to exit)";

cin >> i;

odd (i);

} while (i!=0);
return 0;

void odd (int a)

if ((a%2)!=0) cout << "Number is odd.\n";

else even (a);

void even (int a)

if ((a%2)==0) cout << "Number is even.\n";

else odd (a);

}
This example illustrates how prototyping works. Moreover, in this concrete case the prototyping
of -at least- one of the two functions is necessary.

The first things that we see are the prototypes of functions odd and even:

void odd (int a);


void even (int a);

that allows these functions to be used before they are completely defined, for example, in main,
which now is located in a more logical place: the beginning of the program's code.

Nevertheless, the specific reason why this program needs at least one of the functions prototyped
is because in odd there is a call to even and in even there is a call to odd. If none of the two
functions had been previously declared, an error would have happened, since either odd would not
be visible from even (because it has not still been declared), or even would not be visible from
odd.

Many programmers recommend that all functions be prototyped. Having the prototype of all the
functions in the same place can spare us some time when determining how to call it or even ease
the creation of a header file.

1.12. Scope and lifetime of identifiers


Variable scope, visibility & duration define where and when a given variable can be accessed and
what it will contain.

Scope- refers to the section of a program where a give variable can be accessed.

Four rules can be used to describe this range

a) Starts where the variable is declared

b) Ends where the next unmatched black closing bracket (})

c) Function arguments are accessed within the function.

d) Global variables are accessible in any succeeding function in the file.

Visibility: - is similar to scope except when a global variable has the same name as a local variable
(and it will be hidden.)

The global variable can still be refereed to by preceding the name with the scope resolution (: :)
Example: -

Int a = 0;

V0id main () Output:

{ -1

int a = -1; 0

cout << a <<endl;

cout < <::a;

Duration: - is the time period over which a variable is active in memory

a) Global and main variables last the entire time the program is running .

b) Function arguments and variables declared within block are only active where
the function or block is running.

c) Local variables can be made state, so that they last the entire program, by
preceding the declaration with static key accord.
Exercise

1. Write a function that accepts date of birth and calculates the age.

2. Write a function that takes 3 numbers and returns the min & the max of the numbers.

3. Write a function that swaps the value of arguments.

4. Write a function that takes 3 numbers and from the calling function and displays the sum
& average.

5. Write a function that takes 3 numbers and Return the sum and the average to the calling
function through its parameters.

6. Write a function that Reads 3 numbers from the keyboard and returns the inputs to the
calling functions.

7. Write functions that take parameters length, width, perimeter area, length 7 width are to
be parsed by the calling function and perimeter and area are to be retuned by the function.

You might also like