If you like my posts please leave a comment.

Sunday, September 23, 2012

Function Prototype, Definition and Calling


Function Prototype:


Prototype actually means to a ‘model’. In C++ a function prototype models the actual function completely. As per ANSI C standard all functions in a program must be prototyped means, it must be declared before it is used. Functions’ prototypes can appear in separate header files or it may be the part of the program itself. Usually we put the function prototypes just after including all the header files in our program.


            This is included at the start of the program code to notify the compiler of the type and number of arguments that a function will use. It helps us to avoid the errors which may caused by the mismatch of data types between the values passed to the function and the type of the value that the function is expecting.
Thus a general form of function prototype: <Data-type of the returning value> <function name> (parameter list);
Example:
void volume(int a, float b, float c);
or
void volume(int, float, float );

Using of void data type:

As it is already been said that, void data type specifies an empty set of values and it is used as the return type for a function that is not returning a value. Thus a function that doesn’t return a value is declared as void. Means this function cannot be used in the assignment statements. If a function doesn’t require any parameter or argument, the argument list can be kept empty by declaring the function prototype as shown below:
<Data-type> <function-name>(void);
Example:
int avg(void);
     Note** function prototype becomes optional when the function definition appears before its calling function. However to develop a good programming habit it is always advisable to write the function prototypes for the functions to be used in your program.
            Thus on the basis of above discussion we can now write some valid forms of function prototypes for the same function. Either one or all of these valid forms of the prototype with their associated definitions can be used in the same program. However how and when to use different prototypes for the same function in a single program will be discussed later.
            Now let’s see the different forms prototypes for the same function say avg( ).
  1. float avg(float, float, float);/*function is having a return type other than empty or  “nothing” and an argument list. */
  2. void avg(float, float, float);/*function is having no return type or return type as empty of “nothing”*/
  3. void avg(void);/*Here avg() function is having return type as empty or “nothing” and argument list is also empty.*/

Function Definition:

Function definition consists of function header and function body. The function header is very similar to that of the function prototype except that it does not include semicolon (;). Moreover in function prototype mentioning the name of the variables in the argument list is optional. Whereas in case of function header mentioning of variables along with their associated data type is must, in the argument list. Variables declared in the argument list of header of the function are known as formal arguments.
Note** The function prototype and the function definition must agree EXACTLY on the return type, function name and argument or parameter list. While specifying arguments in the header and the prototype must specify data type for each and every argument separated by comma (.)
Function body; starts with an opening curly brace ({ ) immediately after the function header and ends with a closing curly brace ( }). Thus function definition contains a set of program instructions enclosed with a pair of curly braces.

Accessing Function / Function Calling / Invoking a Function:

A function is called or invoked or executed by providing its name, followed by parameters being sent enclosed with in the pair parenthesis (if the called function was not declared and defined with argument list as ‘void’). The syntax of calling of function is very similar to that of the declaration, except that it does not specify the return type of the function. If a function is declared and defined with a return type other than the ‘void’ then the return value of the function can be used with an assignment statement or directly can be sent to ‘cout’. The program given below is showing you the transfer of program control incase of invoking a function. follow the dotted lined to trace the flow of control.

The function, that does not expect any argument, is invoked with empty parenthesis. And the functions with ‘void’ as return type cannot be used in any assignment statement. Example: sum(); All the functions (except main( )), are usually called from the main() function, but they can also called from any other UDF (User Defined Function(s)), if called function’s definition is available globally means, if it is declared and defined just after  including header files or in some header file.
Example1:
#include <iostream.h>
#include <conio.h>
int sum(int, int);
void avg(float, float);
//--------------function prototypes ends---------------
int sum(int x, int y)
{
avg(x,y);//avg() function is being called by the definition of function sum()
return (x+y);
}
void avg(float a, float b)
{
cout<<"average is= "<<(a+b)/2<<endl;
}
//------------function definitions ends----------
void main()
{
clrscr();
int ans;
ans=sum(5,6);
cout<<"sum was = "<<ans<<endl;
getch();
}
Output:
average is = 5.5
Sum was = 11
So, on wrapping up all of these we can say
  1. All library functions and UDFs are usually called from main( ) function.
  2. One library function cannot call any other library function of your choice, if it is not pre-defined to do so.
  3. UDFs can call all the library functions provided by associated header file(s) must be included in your program.
  4. One UDF can call any other UDF provided by its declaration and definition should available globally. Means it must be declared and defined just after including header files or it can present in some header file.
  5. If an UDF’s (say X( )’s) declaration appears within another function (say Y( )), then the function X( ) is locally available to Y( ). Such function’s prototypes are local prototypes and their scope to the function that contains their definitions.
     More on this topic will be discussed later under the topic ‘Scope Rules’. Please keep following my blog.



2 comments:

  1. it was really helpful to me..as I have to submit an assignment related to this,
    thank you Dear

    ReplyDelete