You are on page 1of 4

Explain function prototypes with an example.

In C++ all functions must be declared before they are used. This is accomplished using
function prototype. Prototypes enable complier to provide stronger type checking. When
prototype is used, the compiler can find and report any illegal type conversions between
the type of arguments used to call a function and the type definition of its parameters. It
can also find the difference between the no of arguments used to call a function and the
number of parameters in the function. Thus function prototypes help us trap bugs before
they occur. In addition, they help verify that your program is working correctly by not
allowing functions to be called with mismatched arguments.

A general function prototype looks like following:


return_type func_name(type param_name1, type param_name2, …,type param_nameN);
The type indicates data type. Parameter names are optional in prototype.

Following program illustrates the value of function parameters:

void sqr_it(int *i); //prototype of function sqr_it


int main()
{
int num;
num = 10;
sqr_it(num); //type mismatch
return 0;
}

void sqr_it(int *i)


{
*i = *i * *i;
}
Since sqr_it() has pointer to integer as its parameter, the program throws an error when
we pass an integer to it.

What is function overloading? Explain with an example.


Function overloading is the process of using the same name for two or more functions.
The secret to overloading is that each redefinition of the function must use either different
types of parameters or a different number of parameters. It is only through these
differences that the compiler knows which function to call in any situation.

Consider following program which overloads MyFunction() by using different types of


parameters:

#include <iostream>
Using namespace std;
int MyFunction(int i);
double MyFunction(double d);
int main()
{
cout <<MyFunction(10)<<”\n”; //calls MyFunction(int i);
cout <<MyFunction(5.4)<<”\n”; //calls MyFunction(double d);
return 0;
}
int MyFunction(int i)
{
return i*i;
}

double MyFunction(double d)
{
return d*d;
}

Now the following program overloads MyFunction using different no of parameters

#include <iostream>
Using namespace std;
int MyFunction(int i);
int MyFunction(int i, int j);
int main()
{
cout <<MyFunction(10)<<”\n”; //calls MyFunction(int i);
cout <<MyFunction(1, 2)<<”\n”; //calls MyFunction(int i, int j);
return 0;
}

int MyFunction(int i)
{
return i;
}

double MyFunction(int i, int j)


{
return i*j;
}

Please note, the key point about function overloading is that the functions must differ in
regard to the types and/or number of parameters. Two functions differing only in their
return types can not be overloaded.

Describe default arguments in functions.

C++ allows a function to assign a parameter a default value when no argument


corresponding to that parameter is specified in a call to that function. The default value is
specified in a manner syntactically similar to a variable initialization. For example, this
declares MyFunction as taking one int argument with a default value of 0:

Void MyFunction(int i =0)


{
//…
//…
}

Now, this MyFunction() can be called one of the following two ways:

MyFunction(10); //passing explicit value


MyFunction(); //not passing any value; function will give default value

One of the reasons why default arguments are included in C++ is because they provide
another method for the programmer to manager greater complexity. To handle the widest
variety of situations, quite frequently a function contains more parameters than are
required for its most common usage. Thus, when the default arguments apply, you need
specify only the arguments that are meaningful to the exact situation, not all those needed
by the most general case.

What is static function? Explain with an example.


Static member functions are used to maintain a single copy of a class member function
across various objects of the class. Static member functions can be called either by itself,
independent of any object, by using class name and :: (scope resolution operator) or in
connection with an object.

Restrictions on static member functions are:

1. They can directly refer to other static members of the class.


2. Static member functions do not have this pointer.
3. Static member function can not be virtual.

Though there are several restrictions on static member functions, one good use of them is
to initialize private static data members of a class before any object is created.

Consider following example:


#include <iostream>
using namespace std;
class S
{
static int i;
public:
static void init(int x)
{
i = x;
}
void show()
{
cout <<i;
}
};

int S::i;
int main()
{
S::init(100); //initialize static variable i before creating object
S x;
x.show();
return 0;
}

What does extern mean in a function declaration?


An extern function or a member can be accessed outside the scope of the .cpp file in
which it was defined.

A variable, function or declaration that is defined with extern, allows making the usage of
variable, function by the remaining part of the current source file. The declaration does
not replace the definition. The declaration of function or a variable is just to describe their
use external to the current file.

In case one identifier is declared with file scope, and an identifier that is declared external
with the same name within a block refers the same object. In case no other identifier
exists at file scope then the identifier will be linked to the external file.

Define precondition and post-condition to a member function.

Precondition: A condition that should return true when a member function is invoked. In
order to use a function correctly a precondition should return true. If a precondition fails
to hold, an operation will not take responsibility to perform any action of sensibility. For
example, the interface invariants of stack class respond nothing about pushing even
though the stack is already full. In this scenario, sinful () is a precondition for push
operation.

Post-Condition: A condition that should return true before returning from an invoked
function. In order to use a function correctly a post condition should return true. Taking a
stack as an example, is empty () must necessarily be true after pushing the element into
the stack when an element is pushed. The function is empty () is a post condition.

You might also like