Monday, September 29, 2008

Function

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 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. In the latter case it must be delimited by curly brackets {}.

Here you have the first function example:

#include <iostream.h>
 
int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}
 
void main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
  getch();

}

Output of the above program is shown in below:

The result is 8


In order to examine this code, first of all remember something said at the beginning of this tutorial: 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:

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.

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 is:

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

that, as you may already suppose, produces the printing of the result on the screen.


Source : Cplusplus.com


No comments: