Explain Function Prototypes With An Example. in
Explain Function Prototypes With An Example. in
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.
#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;
}
#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;
}
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.
Now, this MyFunction() can be called one of the following two ways:
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.
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.
int S::i;
int main()
{
S::init(100); //initialize static variable i before creating object
S x;
x.show();
return 0;
}
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.
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.