on Leave a Comment

C++ Function declaration, definition and calling

A function is a collection of statements used for performing some specific task.  Function is a way to achieve modularization. Modularization is a process of dividing a big task into small subtasks. A function can be pre-defined or user-defined.

Pre-defined function: A function whose declaration and definition is already written in header files and library files. Examples are getch(), clrscr() and so on.
User-defined function: A function whose declaration and definition is written by user in a program. A user can write a function for performing addition operation.

FUNCTION DECLARATION

A function must be declared inside or outside the main function. Function declaration is also known as function prototype. A function can be declared locally or globally.

When function is declared inside the main function, it is called local declaration. For example,

#include<iostream>
using namespace std;
int main()
{
    int add(int,int);
    int a,b,c;
    a=2;
    b=3;
    c=add(a,b);                                     //calling function
    cout<<"Sum of a and b is "<<c;
    return 0;
}
int add(int x,int y)                            //defining function
{
    return x+y;
}

In the above program, add is a user-defined function. Values of a and b are copied into x and y. Then, sum of  x and y is assigned into c.

When a function is declared outside the main function, it is called global declaration. For example,

#include<iostream>
using namespace std;
int add(int,int);
int main()
{
    int a,b,c;
    a=2;
    b=3;
    c=add(a,b);
    cout<<"Sum of a and b is "<<c;
    return 0;
}
int add(int x,int y)
{
    return x+y;
}

Formal arguments and Actual arguments

Formal arguments are the arguments used in user-defined function for storing values of arguments used for calling that user-defined function. 

Actual arguments are used in calling function.

For example,

#include<iostream>
using namespace std;
int add(int,int);
int main()
{
    int a,b,c;
    a=2;
    b=3;
    c=add(a,b);
    cout<<"Sum of a and b is "<<c;
    return 0;
}
int add(int x,int y)
{
    return x+y;
}
Here a and b are actual arguments while x and y are formal arguments.

FUNCTION DEFINITION

Function definition can be written either below or after main() function.

Syntax:

<return-type> <function-name> (<list of arguments>)
{
                //function body
}

Example:

int mul(int a,int b)
{
     return a*b;
}

FUNCTION CALLING

In c++, functions can be called be three methods.

Call by value

A function can be called by passing values of actual arguments to formal arguments. Values of actual arguments is copied into formal arguments.

Example:

#include<iostream>
#include<string.h>
using namespace std;
float mul(float,float);
int main()
{
    float a,b,c;
    cout<<"Enter two numbers : ";
    cin>>a>>b;
    c=mul(a,b);
    cout<<endl;
    cout<<"Multiplication is "<<c;
    return 0;
}
float mul(float x,float y)
{
    return x*y;
}

OUTPUT:

Enter two numbers : 1
2
Multiplication is 2

Call by address 

A function can be called by passing addresses of actual arguments to formal arguments. Pointer variables are used to access the values of actual arguments. You can change the original values of actual arguments using pointers.

Example:

#include<iostream>
#include<string.h>
using namespace std;
float mul(float*,float*);
int main()
{
    float a,b,c;
    cout<<"Enter two numbers : ";
    cin>>a>>b;
    c=mul(&a,&b);
    cout<<endl;
    cout<<"Multiplication is "<<c;
    return 0;
}
float mul(float *x,float *y)
{
    return *x**y;
}

OUTPUT:

Enter two numbers : 1
2
Multiplication is 2

Call by reference

A function can be called by passing references of actual arguments to formal arguments. Reference variables are used to access actual arguments. Any change in reference variable causes change in actual argument. 

Example:

#include<iostream>
#include<string.h>
using namespace std;
float mul(float&,float&);
int main()
{
    float a,b,c;
    cout<<"Enter two numbers : ";
    cin>>a>>b;
    c=mul(a,b);
    cout<<endl;
    cout<<"Multiplication is "<<c;
    return 0;
}
float mul(float &x,float &y)
{
    return x*y;
}

OUTPUT:

Enter two numbers : 1
2
Multiplication is 2

0 comments:

Post a Comment