Pages

Wednesday, November 2, 2011

Functions

Modular programming
            Uses routines, procedures, methods or functions that divides program into subprograms (specific task) that is easier to manage, maintain and troubleshoot.

Functions (User Defined)
            In C++, functions are the very essence of the program. Function works by grouping meaningful program codes together give them a simple name that later will called somewhere in the program. The very example of a function is actually we’ve been doing even from the start when we created our first program in c++. This function is called the main function.

            The main function, if you notice, will be crowded when we put all our codes there. By introducing a sub function (function other than the main function), we will be able to divert some of the codes in our main function to avoid having troubles in finding the fault in the program. In order to use a function, calling it is necessary.

A function may call another function. A function that calls a function is known as the calling function and a function that is called is known as the called function. This is usually happens in the main function when it calls another function or from one sub function to another sub function.

Syntax:
            DataType functionname( )
            {
            //body of the function
            }

If you notice, it is exactly similar to our main function ( int main( ) ).

Two types of declaration:

1.      Declaring and defining at the same time
DataType functionname( )
{
//body of the function
}

2.      Declaring and defining separately
DataType functionname( );

DataType functionname( )
{
//body of the function
}

The first one is the simplest form where it is declared and defined at the same time. The problem with this is you need to be aware of the hierarchy of your function. If a calling function calls a function that is declared (using the first form) after it will give you an error message. This is due to the fact that the function being called is the function that is not yet declared when it is called.
The second form will answer the problem mentioned above where functions are declared first at the top before defining them.
Still, it will be your preference that will be followed. Just don’t forget the syntax to avoid confusion in your program.

Sample Program for form 1:

#include<iostream>
using namespace std;
void firstfunction( ) //declaring a function
{
            cout<”This is from the function using form 1”<<endl;
}

int main( )
{
            firstfunction( );
            return 0;
}

Sample Program for form 2:

#include<iostream>
using namespace std;
void firstfunction( ); //declaring a function
void firstfunction( ) //defining a funciton
{
            cout<”This is from the function using form 2”<<endl;
}

int main( )
{
            firstfunction( ); //calling a function
            return 0;
}

Note: void refers to non returning value. This refers to the ability of a function that can return a value or not.

No comments:

Post a Comment