CPP

Questions on Functoins in C++

Function overloading: Defining multiple functions with same name is called function overloading. There must be a difference in the number, order or types of arguments.

for example
#include <iostream>

using namespace std;

class A
{
    public: void print() //function overloading
    {
        cout<< "Hi" << "\n";        
    }
    public: void print(char str[]) //function overloading
    {
        cout<< "Hi " << str << "\n";
    }
};

int main()
{
   A a;
   a.print();
   a.print("Ram");
   
   return 0;
}


Passing variable to a function by reference, by value and by address

In C++, there are three ways to pass a variable to a funcion

1. By Value
2. By Reference
3. By Address


Examples of passing a variable to a function are

#include <iostream>

using namespace std;

class A
{
    public: void print(int i)
    {
        cout<< "By Value" << " " << i * i << "\n";        
    }
    public: void print1(int& i)
    {
        cout<< "By Reference" << " " << i * i << "\n";        
    }
    public: void print2(int* i)
    {
       cout<< "By Address" << " " << (*i) * (*i) << "\n";        
    }
};

int main()
{
   A a;
    int i = 2;
    a.print(i);
    a.print1(i);
    a.print2(&i);
   
   return 0;
}


By passing a variable by reference or pointer, we avoid creation of a new variable in the function. When a large object is passed as a value; a new structure of the same size gets created in the function, which consumes memory. So by passing the same object by address, then same old object is referenced in the function.

One important thing to note about static variable reference is, that static variables persists across function calls i.e. they remain in memory and active when control returns from the function. Therefore we can return static variable reference from a function.

Inline functions are those functions, whose code gets inserted (instead if a jump to a function) at the place where there is a function call. By making the function inline, we requet the compiler to insert the code at the place where there is a function call. This helps in saving time in calling the function and returning from the function. We can only request for a function to be an inline function, it depends upon compiler whether to insert the code or not depending upon the size of the code and optimization preferences.

Inline functions are better than macro as well, as compiler decides whether making a function inline will result in better performance or not and than only insert the function, however in case of macro, compiler always insert the macro expansion. Inline also provide better type checking than macros.

Macros can produce unwanted side effects as well.. consider the example below

#include <iostream>

using namespace std;

#define Macro_Square(i) i * i
class A
{
    public: int Square(int i)
    {
        return i * i;
    }
};

int main()
{
    A a;
    int i = 2;
    int j = 2;
    cout<< a.Square(++i) << "\n"; //this will give 9, as ++i = 3 and 3 * 3 = 9
    cout<< Macro_Square(++j); //this will give 16, as Macro_Square(++j) => ++j * ++j => 4 * 4 = 16

    return 0;
}


Consider a scenario, where we want to pass a variable by ref and dont want to value to be modified. In this case, we can make the reference const, which will prevent it from changing.

In C++, when we overload function, then internally C++ compiler changes the name of each function and it depends upon the compiler. This process is also called name mangling.
Add Comments :

Login

Register