• Now Online : 60
  • admin@codemyne.net

Introduction

Functions:

A function is a single comprehensive unit-that performs a specified task. This specified task is repeated each time the function is called. Functions break large computing tasks into smaller ones. They work together to accomplish the goal of the whole program. Moreover, function increase the modularity of the program and reduce code redundancy.

Every program must contain one function named main where the program always begins execution. The main function may call other functions, which in turn, may again call other functions. When a function is called the control is transferred to the first statement in the called function.

The statement following the function call, or expression being evaluated with the function is put into a stack. After the function is executed, control returns to the calling function, and execution continues with the evolution of the expression in which the call was made. A value can be returned when the function completes the specified task and that value can be used as and operand in expression.

Functions can communicate data between themselves in two ways, one through global variables and the other through an argument or parameter list.

Functions in C++ can be classified into two categories - library functions and user functions. Library functions are set of pre-defined functions. printf(), fflush(), strcat() etc., belong to the category of library functions. The main distinction between these two categories is that library functions are not required to be written by the user; whereas a user defined function has to be developed by the users at the time of writing a program. However a user-defined function can later become a part of a C++ library.

functions in cpp

Function Prototype:

In C++ all functions must be declared before they used. This is normally accomplished by using a function prototypes are used. The compiler finds and reports any illegal type conversions between the types of arguments used to call a function and the type definition of its parameters. The general form of a function prototype is

        type func_name (type param1, type param2,… type paramN);

Function definition:

A function definition introduces a new user defined function to the program by declaring the type of value it returns and its parameters, and specifying the statement that are executed where the function is called. Function definition can also serve as its prototype if the definition occurs prior to the function’s first use in the program.

Example 1:

/* this program illustrates the definition and usage of a function*/
/* this program defines a function to take two numbers as input add them  and return the result*/
#include<iostream.h>       
   
int main() // every program start with the main()function, this is where execution starts 
{ 
    int x,y,z ;
    int add (int x,int y);
    cout<<" \n Enter a number";
    cin>>x;
    cout<<" \n Enter another number";
    cin>>y;
    z = add (x,y) ;
    /* the value returned by the function is assigned to the variable z*/
    cout<<"\n the result is "<<z;
}
int add (int x, int y)
{
    return x+y;
    /* the function add and returns the sum of the two variables x and y */
}

Function Example:

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

Function Example:

#include<iostream.h>
int subtraction(int a ,int b)
{
    int r;
        r = a-b;
    return (r); 
}
int main()
{
    int x =5 ,y = 3,z;
    z = subtraction(7,2);
    cout<<"the first result is"<<z <<"\n";
    cout<<"the second result is"<<sub(7,2)<<"\n";
    cout<<"the third result is"<<sub(x,y)<<"\n ";
    z =4 +subtraction(x,y);
    cout<<"the fourth result is"<<z <<"\n"
    return 0;        
}

The function in which the function call is contained is known as the calling function (which in this case is main) and the function named in the call is said to be the called function (here it is add). The function add() returns an integer value, which is assigned to a variable in main, z. when function is required to return a value, the return type has to be specified explicitly. The general form of a C++ function is given below.

return_type function_name(argument list)
argument declaration;
{
    variable declarations;
    function statements;
    return value; /* return value must always match the data type specified at the time of function definition */
} 

Passing values to functions:

Arguments to a function are usually passed in two ways. The first is termed as call by value. In example, parameters were passed by this method. In call by value, a copy of the variable is made and passed to the function as argument. With this method, changes made to the parameters of the function have no effect on the variables in the calling function, because the changes are made only to the copies. On the other hand, call by reference is a method in which the address of each argument is passed to the function. By this method, the changes made to the parameters of the function will affect the variables in the calling functions. An example to demonstrate call by value is given below. Both versions are listed below.

//call by value
#include<iostream.h>
void main()
{
    int a=1, b=2;
    void swap (int, int);
    /* functions must be declared before they can be used in a program. */
    swap(a,b);
    cout<<”\n a=”<<a<<”b=”<<b;
}
void swap(int a, int b)
{
    int temp;
    temp a;
    a=b;
    b=temp; 
}

The output will be
a=1 and b=2 (The values of the variables are not exchanged)

//call by reference
#include<iostream.h>
void main()
{
    Int a=1, b=2;
        void swap(int *, int *);
    swap(&a, &b);
    cout<<”\n a*”<<a<<” b*”<<b;
}
void swap(int *a, int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp; 
} 

The output of the program will be
a=2 and b=1 (the values are exchanged)

//passing parameters by reference 
#include<iostream.h>
void duplicate(int &a, int &b, int &c)
{
    a*=2;
    b*=2;
    c*=2;
}
int main()
{
    int x=1, y=3, z=7;
    duplicate(x,y,z);
    cout<<”x=”<<x<<”, y=”<<y<<”, z=”<<z;
    return 0;
}

//more than one recruiting value
#inlcude<iostream.h>
void prevnext(int x, int& prev, int& next)
{
    prev=x-1;
    next=x+1;
} 
int main()
{
    int x=100, y, z;
    prevnext(x, y, z);
    cout<<”Previous=”<<y  <<”, Next=”<<z;
    return 0;
}

//default values in functions
#include<iostream.h>
int divide(int a, int b=2)
{
    int r;
    r=a/b;
    return(r);
}
int main()
{
    cout<<divide(12);
    cout<<end1;
    cout<<divide(20,4);
    return 0;
}

//overloaded function
#include<iostream.h>
int operate (int a, int b)
{
    return(a*b);
}
float operate (float a, float b)
{
    return (a/b);
}
int main()
{
    int x=5, y=2;
    float n=5.0, m=2.0;
    cout<<operate(x, y);
    cout<<”\n”;
    cout<<operate(n, m);
    cout<<”\n ”;
    return 0;
}

//pointer to functions
#include<iostream.h>
int addition(int a, int b)
{
    return(a+b);
}
int subtraction(int a, int b)
{
    return(a-b);
} 
int operation (int x, int y, int (*functocall) (int, int))
{
int g;
g=(*functocall) (x,y);
return(g);
} 
int main()
{
int m,n;
int (*minus) (int, int)=subtraction;
m=operation (7, 5, addition);
n=operation (20, m, minus);
cout<<n;
return 0;
}

In the first program, swap is done only on the copies of the variables a and b. But in the call by reference version, swap is done on the original variables themselves because the address of the variables is passed down to the function.

Recursive function:

C++ supports a feature called recursion. A recursive function is a function that makes a call to itself. Recursion is the process of defining something in terms of itself, and is sometimes called circular definition. Recursive functions can be effectively used in applications in which the solution to a problem can be expressed in terms of successively applying the same solution to subsets of the problem A useful example of recursion is the evaluation of the factorial of a given number.

//function to display the factorial of numbers till 10
#include<iostream.h>
void main()
{
    int j;
    int factorial(int)     //function declaration
    for(j=0; j<10; j++)
{
cout<<”\n”<<factorial(j);
}
factorial(int n)
{
    int fact;
    if(n==0)
    return 1;
}
else
{
fact=n*factorial(n-1);
return fact;
}
}

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment