Overview

DEFAULT ARGUMENTS

The text here isn't that wrong. It's just lacking. You cannot use a default in the middle of a list - I'll demonstate within the code

In C++ we can give parameters default value when they are declared at the start of the function. If no alternative value is passed to the function, the default value is used.

Suppose that I wanted just param2 to be used. I can't just put in the calling argument

MyFunction(param2);

This would just pass param2 as the first value and the second default (the one I wanted to change) would still be the default. We can't use

MyFunction(,param2);

That's just plain wrong!. There is nothing wrong with passing a zero in the param1 slot, as long as that won't screw things up in MyFunction

#include <iostream.h>

void MyFunction(int =3, int =4);

void main()
{
  int param1 = 5;
  int param2 = 6;

  MyFunction();
  MyFunction(param1);
  MyFunction(param1,param2);
}

void MyFunction(int a, int b)
{
  cout << "Parameters are " <<  a << 
  " "<< b << "\n";
}

The function MyFunction has been called with 0, 1 or 2 arguments. The result from this program is shown below :

Parameters are 3 4

Parameters are 5 4

Parameters are 5 6


The default values are defined in the function prototype, not the declaration. (unless the declaration is also the prototype):

#include <iostream.h>

void MyFunc(int a = 3, int b = 4)
{
   cout << "Parameters are " << a << " "<< b << "\n";
}

int main()
{
  int param1 = 5;
  int param2 = 6;
  MyFunc();
  MyFunc(param1);
  MyFunc(param1,param2);
  return (0);
}

Once a default value has been defined, all the rest of the parameters to the right of the first default value must also have default values:

void MyFunc(int, int = 4); // OK

void MyFunc(int = 3, int); // invalid

EXERCISE 10

Write a program which has a function with the values a,b and c passed to it. Set the default values to all be zero. In this function, display the message 'Value passed was zero' if a zero argument is passed to the function. Otherwise display the value of the argument. Test the function by passing 0, 1, 2 and 3 arguments to it.


INLINE FUNCTIONS

An inline function is also upto the compiler if it decides that it should be inline irrespective of if the programmer has said that a function has to be inline.

So far functions are 'jumped' to and 'returned from' when the program runs. An inline function is inserted into the main program when it is compiled. This will remove the time overhead associated with a normal function call. If the inline function is called many times, the code will be bigger than if the function was called normally. It is generally used with functions that are few lines long.

go to should never be used anyway. if conditions should also be avoided. Basically, unless it's a small piece of code, using inline is not that useful

Inline function restrictions : No loops, no switch statements, no goto statements.

#include <iostream.h>

inline void MyFunc(int x)
{
   cout << "\nThe value of " 
   << x << " squared is " << x*x;
}

void main()
{
   int x = 5;
   MyFunc(x);
}

In which case, it won't be inline!

Note : the keyword inline is not compulsory and may be left out :

void MyFunc(int x)
{
   cout << "\nThe value of " << x << " squared is " << x*x;
}

Also, it is only a request to the compiler to insert the function "inline". If the function is too long, the compiler may not do it.