Wednesday, 18 November 2015

Functions


A function is a block of codes  that has a name, return type and no. and type of parameter list. Every function is used to perform specific task and it can be executed at the different locations and in a C and C++ program as required .
A function is a self contained block of statements that perform a particular task. The name of the function must be unique.

Types of function:-

There are two types of function :-

        1 Library function (built in) function.
        2 User defined function.

Function is a type of  parameter which contains function and character(like no. , and type of   parameters.


Types of function :-


1 Built in function :-

 These function are part of the compiler package. These are parts of standard library made available by the compilers. For example; exit(), sqrt() , pow(), etc. are library functions (or built -in functions).
(read more)

2 User defined function:-  

 The user defined function are created by you ,i.e., the programmer. These function are created as per  requirements of your program.
(read more)

Function Prototype:-

A function prototype is  a declaration of the function that tells the program about the types of the L value returned by the function and the no. and the type of arguments It is one very useful factor of C++ function.
A declaration introduces a (function) name to the program whereas a definition tells the program what the function is doing and how it is doing.
The following are the declaration or we can say function prototype.
int absval(int a);
int ged (int n, int z);
A definition is automatically also a declaration.
Therefore we can say that a function prototype has following parts:-
1 Return type.
2 Name of the function.
3 Argument list.
Need for prototype:- Function prototyping enables a compiler to carefully compare each use of the function with the prototype to determine whether the function is involved properly i.e.the no. and type of argument are compared and any wrong number or type or the argument is reported. Thus correcting such problem is easy as C++ can straightly point to the error.
A function declaration can skip the argument names but a function definition cannot.
In a function prototype the name of the argument are optional:Ex:-
float volume (int a,float b, float c);
float area (float,float);      // variable names are optional.
Use of void - Void is used as the return type for functions that do not return a value.Thus a function that does not return a value is declared as follows :
void function-name (parameter list):
A function that does not require any parameter (i.e., it has an empty argument list ), can be declared as follows :-

type function name(void);

A function prototype is not needed when the function definition appears its calling function.
Possible function styles:
1 void function with no argument - This style of function simply perform an independent task. It does not send or receive any parameter and it does return any value.
2 Void function with some argument- This style of function does receive some arguments (parameters) but does not return a value.
3 void function with no arguments - This style of function takes some arguments and does not return a value.




No comments:

Post a Comment