User Defined Functions
User Defined Functions
DEFINED
FUNCTIONS
Function Definition
A Function Prototype is a
declaration of the function that tells
the program about the type of the
value returned by the function and
the type of argument.
FUNCTION
PROTOTYPE
DECLARING FUNCTIONS
Until now, we have defined all of the
functions before the first appearance of calls to
them in the source code. These calls were
generally in function main which we have always
left at the end of the source code. If you try to
repeat some of the examples of functions
described so far, but placing the function main
before any of the other functions that were called
from within it, you will most likely obtain
compiling errors. The reason is that to be able to
call a function it must have been declared in
some earlier point of the code, like we have done
in all our examples.
FUNCTION
PROTOTYPE
But there is an alternative way to avoid writing the
whole code of a function before it can be used in
main or in some other function. This can be
achieved by declaring just a prototype of the
function before it is used, instead of the entire
definition. This declaration is shorter than the
entire definition, but significant enough for the
compiler to determine its return type and the types
of its parameters.
FUNCTION
PROTOTYPE
Its form is:
This allows these functions to be used before they are defined, for
example, in main, which now is located where some people find it
to be a more logical place for the start of a program: the beginning
of the source code.
Anyway, the reason why this program needs at least one of the
functions to be declared before it is defined is because in odd
there is a call to even and in even there is a call to odd. If none of
the two functions had been previously declared, a compilation
error would happen, since either odd would not be visible from
even (because it has still not been declared), or even would not be
visible from odd (for the same reason).
FUNCTION
PROTOTYPE
Having the prototype of all
functions together in the same place
within the source code is found
practical by some programmers, and
this can be easily achieved by
declaring all functions prototypes at
the beginning of a program.
DEFAULT ARGUMENTS
When declaring a function we can specify
a default value for each of the last
parameters. This value will be used if the
corresponding argument is left blank
when calling to the function. To do that,
we simply have to use the assignment
operator and a value for the arguments
in the function declaration. If a value for
that parameter is not passed when the
function is called, the default value is
used, but if a value is specified this
default value is ignored and the passed
value is used instead. For example:
DEFAULT ARGUMENTS
// default values in functions
#include <iostream.h>
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
DEFAULT ARGUMENTS
As we can see in the body of the program there
are two calls to function divide. In the first one:
divide (12)
we have only specified one argument, but the
function divide allows up to two. So the function
divide has assumed that the second parameter is
2 since that is what we have specified to happen
if this parameter was not passed (notice the
function declaration, which finishes with int b=2,
not just int b). Therefore the result of this
function call is 6 (12/2).
DEFAULT ARGUMENTS
In the second call:
divide (20,4)
and the call is just like the call to any other function. You do not have to
include the inline keyword when calling the function, only in its declaration.
Date joining_date;
This declares a structure variable joining_date of type date. Thus, the complete structure
definition is as follows:
struct date
{
short day ;
short month;
short year;
};
Date joining _date ;
Now, the structure joining date will be having its elements as day month and year. The c++
compiler automatically allocates sufficient memory to accommodate all of the element variable
that make up a structure variable.
STRUCTURES
Technically ,there is no difference
between a structure and a class. In
fact, a structure is a class declared
with keyword struct and by default,
all members are public in a structure
whereas all members are private by
default in the class.
STRUCTURE