C++ Functions:Defining, Declaring, Calling, Arguments

A function can be referred to be a section which is defined by a name within which multiple task takes place. The best example of the function is the main() function which we declare before we start writing any C++ program. To make a code more simplified and well defined and quick executable it is divided into sub parts of several functions to perform a specific task.
    Declaring a function consists of several parts better classified as the return type, name, parameters. Return type specifies the value which is to be returned by the function after executing all its codes its types are: void, int, float,double, long, char, string, class;etc. Name as stated is the name of the function defined. Parameters to be explained can be stated as the values passed through the function so that they can be used in execution without any further definition. Then finally comes the body of the function. Body as the word itself defines is the set of code executed within the scope of function.

  For example if we define any user defined function as max:
     
// function returning the max between two numbers
 
int max(int num1, int num2)  {
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
 
Function declaration just doesn't mean defining it in the main() function. Defining a function means defining its prototype its parametric form and also calling it in the main function. But a very important point is to be noted, the parameters of a function are not that important. A function can be without parameter it is not an error but a procedure. It is advised to do both function prototype as well as definition because a function is normally declared to perform at different platforms and to be in the safer side it should be declared at the top only.
   Calling a function means invoking a set of arguments or giving the whole access of program in different hand. The parameters declared in the main function are passed through the function storing its returned value(if available) in a third variable and displaying the result. The parameters are used to invoke the function at particular location.

#include 
using namespace std;
 
// function declaration
int max(int num1, int num2);
 
int main () {
   // local variable declaration:
   int a = 100;
   int b = 200;
   int ret;
 
   // calling a function to get max value.
   ret = max(a, b);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
// function returning the max between two numbers
int max(int num1, int num2)  {
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
A function can be called in three different ways call by value, pointer, reference:
Call By Value: This method copies the original value provided to the function. In case any changes to be made to the parameters the original values are also effected.
 Call By Pointer: This deals with the address of the parameters provided. Any change applied to the values are affected to the parameters only not to the originals variables.
Call By Reference: This procedure copies the value of parameters into a temporary variable and any change made while execution is affected to the temp variable and the original is remained unchanged.
       By default C++ uses call by value to declare any function. To change our preference we need to declare the parameter in different forms as per requirements.Else while execution the original variables will come up with changed value and the original value will be lost.

Share this

Related Posts

Latest