More
    CodingC++Functions, Prototypes & Recursion in C++

    Functions, Prototypes & Recursion in C++

    Function:

    In a simple language we can say that functions are basically the segments of a code. Different functions are written in a code to perform a specific task (for example, a function which is framed to add two numbers or a function that returns factorial of a number etc.). And as per requirement we use the working of one function in another function (calling a function).

    CALLING FUNCTION: which calls the other function.

    CALLED FUNCTION: which is being called by the calling function.

    Example 1.1: Addition of two numbers using function:

    #include<iostream>
    #include<bits/stdc++.h>
    using namespace std;
    
    int sum(int a,int b){
        return a+b;
    }
    
    int main(){
        cout<<sum(5,7);
        return 0;
    }

    In the above example main() is the calling function and sum() is the called function. And a and b are the argument of the sum() function of integer type, whenever the sum() function will be called, the calling function will assign the value to this argument.

    Prototypes:

    In example 1.1, if the sum function will be framed below the main function than the compiler would fail to execute output sum(5, 7) and will give error that “sum was not  declared in the scope” . So to get ride of such problem a single line is to be written above the main function (for example, a prototype for the sum() function in example 1.1 will be simply:

    int sum(int a, int b);

    After writing this prototype you can frame the sum() function wherever you want in your code either above ,below or anywhere else; compiler will be able to find the called function.

    Remember one thing, if the called function (sum() function as per ex. 1.1) is above the calling function (main() function as per ex. 1.1) you can skip writing the prototype. But if it is below the calling function then you must write the prototype. Well, it is recommended  to always write the prototype.

    Call by Value and Call by Reference:

    A function can call the another function in two ways:

    1) call by value: if the calling function just pass the value to the arguments of called function.

    2) call by reference: when the arguments of called function is declared as a pointer and the address is passed to it.

    It will be more clear with this example:

    Example 1.2: Multiplication of two numbers using function: (call by value)

    #include<iostream>
    #include<bits/stdc++.h>
    using namespace std;
    
    int product(int a,int b);
    
    int main(){
        cout<<product(5,7);
        return 0;
    }
    int product(int a,int b){
        return a*b;
    }

    Example 1.3:

    Swaping two numbers using function: (this code can only be successfully executed by passing address to the argument of ‘called function’ because if we just pass the value, then only the variables of ‘called function’ will be swaped and it will not be reflected in the variables of calling function (since both are independent of each other).

    #include<iostream>
    #include<bits/stdc++.h>
    using namespace std;
    void swap(int* a,int* b);
    
    int main(){
        int l=1,m=2;
        cout<<"the value of l and m before swaping is : "<<l<<" and "<<m<<endl;
        swap(&l,&m);
        cout<<"the value of l and m after swaping is : "<<l<<" and "<<m<<endl;
       return 0;
    }
    void swap(int* a, int* b){
        int t=*a;
        *a=*b;
        *b=t;
    }
    

    Recursion:

    In a simple language, when the called and the calling function are same then such situation is known as recursion and that function is called as recursive function.

    (RECURSION: WHEN THE FUNCTION CALLS ITSELF)

    Example 1.4:

    Finding factorial of a number using function:

    #include<iostream>
    #include<bits/stdc++.h>
    using namespace std;
    int fact(int a);
    int main(){
        int n;
        cin>>n;
        cout<<n<<"! = "<<fact(n)<<endl;
        return 0;
    }
    
    int fact(int a){
        if(a==0)
        return 1;
        else if(a==1)
        return 1;
        else 
        return a*fact(a-1);
    }

    Here fact() function is called by fact() function itself. So, we can say that here fact() function is a recursive function.

    Read More Applications of Recursion:

    Linear Diophantine Equation Using Extended Euclidean Algorithm

    Sponsored

    3 COMMENTS

    1. Hello Sadhna

      I just arrived at this article. I enjoyed it a lot. Carry on writing such useful stuff .

      Thank you and bye for now.

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Subscribe Today

    GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

    Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

    Exclusive content

    Latest article

    More article