Function-in-C++ .

In this session i have discussed function in C++ .


A C++ function is collection of code that perform a particular task . There are two types of function in C++ .

1-Predefined-Function(Library Function) .

2-Userdefined-Function .

1-Predefined-Function : It is ready made function . In this function you do not need to write any logic . You simply call it and use it .

2-Userdefined-Function : It is created by the user . So You need to write the function logic .

function syntax :

    Type function-Name(Parameters-list)
    {
        Function body
    }
    

function-Name It is name of method . So you can give the name of method any valid C# identifier .

Parameters-List : It is variable that is used to give the value to function at the time of function call.

Type : It is return type of method . It may be int , float , void etc .

Function body : It is the place where you can write your program logic .


Function Aspects

There are three aspect of function in c as follows

1-Function Declaration

2-Function Call

3-Function Defintion

1-Function Declaration

First of all you have need to declare the function in global declaration section in program .

    void Display();
    

2-Function Call

Call the function in main() of the program in c or any other function of program .

    void main()
    {
         Display();
    }
    

3-Function Definition

In this part write the program logic what the function performs .

    void Display()
    {
        cout<<"Hello World";
    }
    

Example : Wrtie a function that print the hello world on the screen step by step .

    #include<iostream>
    using namespace std; 
    void Display(); // function declaration
    int main()
    {          
          Display(); // function call
          return 0;
    }
    void Display() // function definition
    {
        cout<<"Hello World";
    }
    
    OUTPUT : Hello World
    

Types of function in c

A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of function calls.

1-function without argument and without return value

2-function with argument and without return value

3-function with argument and return value

4-function without argument and return value

1-A function without argument and without return value

    #include<iostream>
    using namespace std;
    void Display(); // function declaration
    int main()
    {          
          Display(); // function call
          return 0;
    }
    void Display() // function definition
    {
        cout<<"Welcome to androidatpoint";
    }
    
    OUTPUT : Welcome to androidatpoint
    

2-A Function with argument and without return value

    #include<iostream>
    using namespace std;
    void Sum(int x,int y); // function declaration
    int main()
    {          
          Sum(20,10); // function call
          return 0;
    }
    void Sum(int x,int y) // function definition
    {
        int s=x+y;
        cout<<"Sum="<<s;
    }
    
    OUTPUT : Sum=30
    

3-A Function with argument and with return value

    #include<iostream>
    using namespace std;
    int Sum(int x,int y); // function declaration
    int main()
    {          
        int s= Sum(20,10); // function call
        cout<<"sum="<<s;  
        return 0;
    }
    int Sum(int x,int y) // function definition
    {
        return x+y;
    }
    
    OUTPUT : Sum=30
    

4-A Function without argument and with return value

    #include<iostream>
    using namespace std;
    int Sum(); // function declaration
    int main()
    {          
        int s= Sum(); // function call
        cout<<"Sum="<<s;  
        return 0;
    }
    int Sum() // function definition
    {
        return 20+10;
    }
    
    OUTPUT : Sum=30
    

Example : Write a c function that print the value from 1-10 .

    #include<iostream>
    using namespace std;
    void Print(); 
    int main()
    {          
        Print();
        return 0;
    }
    void Print() 
    {
        int i=1;
        while(i<=10)
        {
            cout<<i;
            cout<<" ";
            i++;
        }
    }
    
    OUTPUT : 1 2 3 4 5 6 7 8 9 10
    

Example : Write a c function that print the square of any given number .

    #include<iostream>
    using namespace std;
    void Square(); 
    int main()
    {          
        Print();
        return 0;
    }
    void Square() 
    {
        int x;
        cout<<"Enter the value : ";
        cin>>x;
        x=x*x;
        cout<<"Square : "<<x;
    }
    
    OUTPUT : Enter the value : 4
             Square : 16
    

Welcome to AndroidATPoint.

back image                     next image