07. Function in C++ | IndianTechnoEra - IndianTechnoEra
Latest update Android YouTube

07. Function in C++ | IndianTechnoEra

what is functoins in c++?
Admin
07. Function in C++ | it2Edu

Function

A.Functions allow us to group commonly used code into a compact unit that can be used repeatedly.

We have already encountered one function named main. It is a special function called at the beginning of the program.

Use of function

Example: Suppose we want to write a program to compute the area of three triangles. We could write out the formula three times or we could create a function to do the work.

Syntax:

// defining function
function_type function_name(parameters)
{
    // statement(s)
}

// calling function

function_name(arguments);

  • function_type is the data type of the data that function will return and if the written type is void then no data will be returned.
  • function_name follows the same rules as variable name. This is not a coincidence, because both function and variable names are Identifier.
  • parameters is a variable in a function definition. Parameters are optional.
  • When a function is called, the arguments are the data passed into the function's parameters.

Example with parameters

#include<iostream>
using namespace std;
float triangle(float width,float height)
{
    float area;
    // area of triangle
    area = width * height / 2.0;
    return area;
}
int main()
{
    cout<<"Triangle #1 : "<< triangle(1.3,4.7) <<endl;
    cout<<"Triangle #2 : "<< triangle(4.1,2.1) <<endl;
    cout<<"Triangle #3 : "<< triangle(6.7,4.3) <<endl;
    return 0;
}

Output
Triangle #1 : 3.055
Triangle #2 : 4.305
Triangle #3 : 14.405


Example without parameter

#include <iostream>
using namespace std;
void area()
{
    float area_circle;
    float rad;
    cout<<"\nEnter the radius : ";
    cin>>rad;
    area_circle = 3.14 * rad * rad ;
    cout<<"\nArea of Circle = "<<area_circle;
}
int main()
{
    area();
    area();
    return 0;
}

Output
Enter the radius :
Area of Circle = 78.5
Enter the radius :
Area of Circle = 113.04



Function argument 

i.) Call by value

Formal arguments - while declaring a function, the arguments list of parameters we specify are known as formal parameters.

Actual arguments: 

The parameter’s value (or arguments) we provide while calling a function is known as actual arguments.

Example

#include<iostream>
using namespace std;
int sum(int x, int y)
{
    int add = x + y;
    return add;
}
int main()
{
    int a =10;
    int b = 20;
    int add = sum(a, b);
    cout<<add;
    return 0;
}

In the above example,

  • Variable x and y are the formal parameters or (formal arguments).
  • Variable a and b are the actual arguments (or actual parameters).
  • In the call by value method, the actual arguments are copied to the formal arguments, hence any operation performed by function on arguments doesn’t affect actual parameters.
  • This is little confusing. Let go through example :

Example

#include<iostream>

using namespace std;

int increment(int var)

{

    var = var+1;

    return var;

}

int main()

{

    int num1 = 20;

    int num2 = increment(num1);

    cout<<"num1 value is: "<<num1<<endl;

    cout<<"num2 value is:"<<num2<<endl;

    return 0;

}

Output

num1 value is: 20

num2 value is:21

Value of variable num1 is 20 even after doing increment operation because function is called by value, which means num1 value gets copied into var and the variable var got incremented (not variable num1), which later stored in num2, via call.

ii.) Call by reference

First go through the pointer to understand the Call by Reference.

In Call by Reference method the addresses of actual arguments (or parameters) are passed to the formal parameters, which means any operation performed on formal parameters affects the value of actual parameters.

Example

#include<iostream>
using namespace std;
int increment(int  *var)
{
    *var = *var+1;
    return *var;
}
int main()
{
    int num1=20;
    int num2 = increment(&num1);
    cout<<"num1 value is : "<<num1<<endl;
    cout<<"num2 value is : "<<num2<<endl;
    return 0;
}

Output

num1 value is : 21
num2 value is : 21

Unlike “call by value”, in this method, the value of num1 got changed because the address of num1 is passed as an argument so the increment operation is performed on the value stored at the address.


Recursive function

The function which calls itself is known as a recursive function.​

void area()
{
    // function calls itself
    area();
}
int main()
{
   area();
}

A recursive function may seem like a never-ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our function will never finish. This might be true is some cases, but in practice, we can check to see if a certain condition is true and in that case exit (return from) our function. The case in which we end our recursion is called a base case.

Example :

#include<iostream>
using namespace std;
int factorial(int i)
{
    if(i <= 1)
    {
        // This is base case
        return 1;
    }
    return i * factorial(i - 1);
}

int  main()
{
    int i = 15;
    cout<<"Factorial of "<<i<<" is "<<factorial(i)<<endl;
    return 0;
}


Output

Factorial of 15 is 2004310016



Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.