Functions in C/C++ Programming

Functions groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind. e.g

Simple Functions

Our first example demonstrates a simple function those purpose is to print a line of 45 asterisks.The example program generates a table , and lines of asterisks are used to make the table more readable. Here”s the listing for Table:

 // table .cpp
   // demonstrates simpole function
   # include
   using namespace std;
   void starline( );
   into main ()
   {
	   starline ( );
	   cout << "Data type Range" << endl;
	   starline ( );
	   cout << "char -128 to 127 " << endl
	   << "short -32 ,768 to 32,767"<< endl
	   << "int system independent: << endl
	   << " long q-2,147,483,648 to 2,147,483,647" << endl;
	   starline ( );
		return 0;
	}
//""""""""""""""""""""""""..
//starline ( )
// function defintion
void starline ( )
{
	for(into j=0;j<45; j++)
	cout << "*" ;
	cout << endl;
}

The output from the program looks like this
*************************************
Data type Range
*************************************
Char -128 to 127
Short -32,768 to 32,767
Into system dependent
Double -2,147,483,648 to 2,147,483,647
*************************************

Why we use Functions”

The most important reason to use functions is to aid in the conceptual organization of a program.

Another reason to use functions is to reduce program size. Any sequence of instructions that appears in program more than once is a candidate for being made into a function. The function”s code is stored in only one place in memory, even though the function is executed many times in the course of the program.

Two reasons

  1. Writing functions avoids rewriting the same code over and over. Suppose that there is a section of code in a program that calculates area of a triangle. If, later in the program we want to calculate the area of a different triangle we wont like to write the same instructions all over again. Instead we would prefer to jump to a “section of code” that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function.
  2. Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided in to separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code in to modular functions also makes the pro-gram easier to design and understand.

Function Declaration

Just as you can”t use a variable without first telling the compler what it is, you also can”t use a funciotns without teling the compiler about it, There are two ways to do this . The approach we show here ist o declare the funtion before it is called. The other approach is to define it before it”s called. ; we”ll examine that next.) in the Table program, the functions starline() is declared in the line.

 Void starline ( );

The declaration tells the compiler that at some later point we plan to present a function called starline. The keyword void specifies that the function has no return value, and the empty parentheses indicate that it takes no arguments.

Notice that the funtois declarations is terminated with a semicolon It is a complete statement in itself.

Function declarations are also called prototypes, since they provide a model or blueprint for the function. They tell the compiler,” a function that looks like this is coming up later in the program, so it”s all right if you see references to it before you see the function itself.”

Calling the Function

The function is called (or invoked) three times from main (). Each of the three calls look like this:

 Starline():

This is all we need to call a function name,followed by parentheses. The syntax of the call is very similar to that of declaration, except that the return type is not used. A semicolon terminates the call. Executing the call statement causes the function to execute; that is, control is transferred to the function, the statement in the function definition are executed, and then control returns
to the statement following the function call.

Function Definition

Finally, we come to the functions its self, which is referred to as the functions definition, The definition contains the actual code for the function. Here’s the definition for starline( ) .

 void starline ( )
   {
   for ( intj=0, j < 45; j++ )
   cout << "*" ;
   cout << endl;
   }

The definition consists of a line called the decelerator, followed by the function body , The function body is composed of the statements that make up the function, delimited by braces.

The decelerator must agree with the declaration: It must use the same function name, have the same argument types in the same order( if there are arguments), and have the same return type.

Notice that a semicolon does not terminate the declarator. Figure shows the syntax of the function declaration, function call, and function definition.

When the function is called, control is transferred to the first statement in the functions body. The other statements in the function body are then executed, and when the closing brace is encountered, control returns to the calling program.