Cute FTP Pro

A closer look at Functions


The following topics are covered in this section:


Types of Functions

There are two types of functions namely:

1. Library Functions

2. User Defined Functions

There are many functions which are provided by the compiler. These functions that come with the compiler are called as library functions. The prototype for the library functions will be present in some header file. To use a library function, you only need to include the header file (where the function prototype exists) and should know the name of the function.

It is common for programmers to write coding for functions and then use them later in some other program when needed. In fact in C programming, this was quite common. You can do the same in C++ as well. You could define a set of general-purpose functions in a header file and include the header file in the programs where you want to use those defined functions (by specifying a function call statement). But using functions in this way can lead to some problems, which will be discussed later (the main problem is when there is a clash of function names). In C++ we make use of ‘classes’ and reuse classes, rather than functions directly.

Using library functions: rand( ), srand ( ), time( )

There may be instances wherein you will want to generate numbers randomly. For example if you are simulating a game played with dice then you should be able to produce numbers between 1 to 6 randomly (corresponding to the 6 faces of a dice). Or if you want to simulate the tossing of a coin you should be able to retain the randomness of the event (i.e. these are events which you can’t predict. It could be a head or a tail).

We can make use of the rand ( )library function to perform such tasks. The rand ( ) function is defined in the stdlib.h library. Let us write a program to toss a coin and ask the user for his/her choice. If the user’s choice and the result of the toss are the same then the user wins the game.

The rand ( ) function will generate integers upto a maximum of 32767. But in the case of tossing a coin we have only two possibilities (a head or a tail). To scale down 32767 to two cases we divide the rand ( ) value by 2 and use the remainder (the remainder will either be 1 or 0). Thus we assume that 1 is head and 0 means tail in the above program. The output for the above program will be:

Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.
Do you want to play again? n

If you run the program again a second time the result will be:

Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.

As you might have noticed the sequence is the same (i.e. each time the program is run the same set of random numbers are produced). This is because the random number is generated in a sequence and unless you ask the computer to start the sequence from a different position it will always keep starting at the same place. We have another function called srand( ) than can be used to alter the start of the sequence. To do this just add the satement:

srand( time(0) );

before the ‘do’ statement. The function time ( ) is defined in ‘time.h’ header file. The output will now be:

If you run the program there is a good chance of getting a different result for the same inputs. This is because we are setting the random generator differently each time the program is run. The function time (0) will give the present system time in seconds. Each time you run this the time (0) value will be different.

Suppose you want to simulate the throw of a dice then you should scale down the outcomes of the rand( ) function to 6. For this (instead of rand( )%2, you could use the statement:

( ( rand( ) % 6 ) + 1 )

We have to add 1 because rand ( )%6 will produce values between 0 and 5 but we need values from 1 to 6.


Function Overloading

Can two functions have the same name?

More than one function can have the same name but they should have different number of parameters or the types of the parameters should be different. This is known as function overloading. The name of a function along with its parameter data types forms the function signature. The signature helps differentiate between two functions. In function overloading the signatures of the functions with the same name will be different.

In general, overloading is the process of assigning several meanings to a function (or an operator as in operator overloading, which we will discuss in a later chapter). Consider the example given below:

// A PROGRAM TO ILLUSTRATE FUNCTION OVERLOADING

In the above program, at the starting itself, the compiler will know that there are three functions with the same name of ‘display’. But the compiler is also clever enough to see that the parameters of each of the three functions are different. Once it notices that they are different, the compiler will consider the three functions to be different.

Whenever the ‘display’ function is called, the correct function will be called depending on the arguments that you have used.

Remember: The return value of a function is not part of the function signature. Do not try to overload based on return values (this will produce a compile error). You may wonder why this is the case? The return value of a function need not be stored (i.e. even if a function returns a value you need not use that value in your program). If you used 2 functions called int add( ) and double add( ) and if you never assigned the return value to any variable the program will have no way of determining which function to call.


Apply what you’ve learnt

Let’s write a C++ program to create a little mathematical program. The program should have a menu display providing the user with three options. There should be an option to generate the Fibonacci series, an option to find the factorial of a number and an option to exit the program. All of this has to be done using functions. (The entire program could be written within the main ( ) function without using functions but by using functions you will be able to create a clean and structured program. For performing each of the tasks we will write individual functions and you will find that it is much easier to follow the logic of the program by writing using functions).

By the way, the Fibonacci series goes as follows:

1,1,2,3,5,8,13,21,34,55,89 and so on.

(just add two numbers and you will get the next number in the series).

The factorial of a number, say 5 is:

5*4*3*2*1

Factorial of 6 would be:

6*5*4*3*2*1

//A program using functions

You can add more options and develop it into a useful mathematical program. This program was just meant to illustrate how the use of functions can aid in the understanding of the program logic.


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.