Function in C++ Programming | C++ Programming

Default Arguments

When declaring a function, we can specify a default value for each of the last parameters which are called default arguments. This value will be used if the corresponding argument is left blank when calling the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead. 

For example:

// default values in functions

using namespace std;
int divide(int a, int b = 2)
{
    int r;
    r = a / b;
    return (r);
}
int main()
{
    cout << divide(12);
    cout << endl;
    cout << divide(20, 4);
    return 0;
}

As we can see in the body of the program there are two calls to function divide. In the first one:

divide (12)

We have only specified one argument, but the function divide allows up to two. So, the function divide has assumed that the second parameter is 2 since that is what we have specified to happen if this parameter was not passed (notice the function declaration, which finishes with int b=2, not just int b). Therefore, the result of this function call is 6 (12/2).

In the second call: divide (20,4) There are two parameters, so the default value for b (int b=2) is ignored and b takes the value passed as an argument, that is 4, making the result returned equal to 5 (20/4).

Inline Functions

A function call is a costly operation. During the function call and its execution, our system takes overheads like saving the values of registers, Saving the return address, pushing arguments in the stack, jumping to the called function, loading registers with new values, returning to the calling function, and reloading the registers with previously stored values. For large functions this overhead is negligible but for small functions taking such a large overhead is not justifiable. To solve this problem concept of an inline function is introduced in C++.

The functions which are expanded inline by the compiler each time its call is appeared instead of jumping to the called function, as usual, is called inline function. This does not change the behavior of a function itself but is used to suggest to the compiler that the code generated by the function body is inserted at each point the function is called, instead of being inserted only once and performing a regular call to it, which generally involves some additional overhead in running time.

For Example:

using namespace std;
inline int sum(int a int b)
{
    int s;
    s = a + b;
    cout << "Sum =" << s << endl;
}
int main()
{
    int x, y;
    cout << "Enter two numbers";
    cin >> x >> y;
    sum(x, y);
    return 0;
}

Here at the time of the function call instead of jumping to the called function, the function call statement is replaced by the body of the function. So, there is no function call overhead.

Overloaded functions

In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. This is called function overloading.

For example:

// overloaded function
using namespace std;
int mul(int a, int b)
{
    return (a * b);
}
float mul(float a, float b)
{
    return (a * b);
}
int main()
{
    int x = 5, y = 2;
    float n = 5.0, m = 2.0;
    cout << mul(x, y);
    cout << "\n";
    cout << mul(n, m);
    cout << "\n";
    return 0;
}

In the first call to “mul” the two arguments passed are of type int, therefore, the function with the first prototype is called; This function returns the result of multiplying both parameters. While the second call passes two arguments of type float, so the function with the second prototype is called. Thus, the behavior of a call to mul depends on the type of arguments passed because the function has been overloaded.

Notice that a function cannot be overloaded only by its return type. At least one of its parameters must have a different type.

Arguments passed by value and by reference

In the case of pass-by-value, Copies of the arguments are passed to the function, not the variables themselves. For example, suppose that we called our function addition using the following code:

For Example:

using namespace std;
int addition(int a, int b)
{
    int r;
    r = a + b;
    return (r);
}
int main()
{
    int z;
    int x = 5, y = 3;
    z = addition(x, y);
    cout << "The result is " << z;
    return 0;
}

What we did, in this case, was to call to function addition passing the values of x and y, i.e. 5 and 3 respectively, but not the variables x and y themselves.

When the function addition is called, the value of its local variables a and b become 5 and 3 respectively, but any modification to either a or b within the function addition will not have any effect on the values of x and y outside it because variables x and y were not themselves passed to the function, but only copies of their values at the moment the function was called.

In the case of pass-by-reference, the address of the variables (variable itself) not copies of the arguments are passed to the function. For example, suppose that we called our function addition using the following code:

For Example:

int addition(int &a, int &b)
{
    int r;
    r = a + b;
    return (r);
}
int main()
{
    int z;
    int x = 5, y = 3;
    z = addition(x, y);
    cout << "The result is " << z;
    return 0;
}

What we did, in this case, was to call to function addition passing the variables x and y themselves (not values 5 and 3) respectively.

When the function addition is called, the value of its local variables a and b points to the same memory location respectively, therefore any modification to either a or b within the function addition will also have an effect on the values of x and y outside it.

Return by Reference

If we return the address (reference) rather than the value from a function then it is called a return by reference.

using namespace std;
int &min(int &x, int &y)
{
    if (x < y)
        return x;
    else
        return y;
}
int main()
{
    int a, b, r;
    cout << "Enter two numbers: \n";
    cin >> a >> b;
    r = min(a, b);
    cout << "The minimum value between 2 values is " << r;
    return 0;
}

Here min function returns the reference of the variable which is smaller between a and b and the statement “min(a,b)=0” makes the value of the variable zero which is smaller.

Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C program to Find Cartesian Product of Two Sets | C programming

C Program To Check The String Is Valid Identifier Or Not | C Programming