Functions in C++

Functions in C++ are different from C functions in many ways.
  1. They can have reference parameters- along with pointer or value parameters
  2. They can return a value by reference
  3. They can be overloaded
  4. They can have default values for parameters.
Let us consider each of these aspects one by one. 

Reference parameters

If a function has reference parameter, it accesses the actual argument instead of a copy. This is similar to call by pointer, but there is no extra pointer involved here. Let us look at an example.

Now in this example, b is  a reference parameter. So b++ will change our actual argument n.

Output will be
                     22
         m and n are 10 13


The parameter can even be const & which indicates that the reference parameter can not be modified by the function.

It is faster to send a large object as a reference parameter.

Reference Return value
A function may return a reference value. But returning a reference to a local variable is error just like returning a pointer to local variable.



By using a reference return value, a function call can appear on LHS of an expression. This is very useful for cascading of functions like insertion and extraction operators.

Overloaded functions


 Many a times a function can have varying number of parameters. e.g. if you want to find average of numbers, you may want to send 2 numbers as parameters or 3 or 3. When you write these functions, you would prefer to call all these functions with the same name, average.
C++ lets you do that. In C++ you can have multiple functions with same name, but different number of parameters or different types of parameters. Let us look at some examples.

In this program sum function is having 4 overloaded versions, with 2 int parameters, 3  int parameters, 2 float parameters and 2float and one int parameters. When you call sum function in the program, the compiler selects the appropriate version, based on the type and number of parameters.
But the line x=sum(12.3,11.3), is not clear. Here both the parameters are double - neither int nor float. Remember that in C/C++ 12.4 is double 12.4f is float. Hence you get compilation error

In such situations, you can explicitly cast the parameters as shown here.
             x = sum((float)12.4,(float)11.3);

Note

  • Overloading can be done by changing number/type of parameter but not just by changing return type
    • In the above example if we write a third function as                                                     double sum(float a,float b)

                         we get compilation error

Now you try your hands on overloading functions.
Write two overloaded functions to find larger of two ints and larger of two doubles.

Default values for parameters:


In the previous example, first and second functions are quite similar. In fact first function is like second function where c is 0. That is exactly what default value for a parameter does. If a function expects 3 parameters, and user calls the function with only 2, the third will get a default value which is defined at the declaration of the function.

Let us look at a small example

If you want to provide default values for multiple parameters, then you should give values from right to left, without leaving any parameter in the middle

int sum(int a, int b=10,int c=12);//correct
int smallest(int a=10,int b,int c=100);//wrong, can not define a without b


Comments

Popular Posts