C++ function prototyping

C++ FUNCTION PROTOTYPING

Function prototyping is one of the major improvements added in C++ functions. The prototype describes the function interface to the compiler by giving details such as type of arguments, No. of arguments and last the type of return values. With function prototyping, a template is always used when declaration and defining a function. When a function is called, the compiler uses the template is always used when declaring and defining a function. when a function is called, the compiler uses the template to ensure that proper arguments are passed, Any violation in matching the arguments or the return types will be caught by the compiler at the time of compilation itself.These checks and controls did’nt exist in the conventional C functions.

Remember, C also uses prototyping but it was introduce first in c++ language by stroustrap and the success of this feature inspired the ANSI C committee to adopt it. however there is a major difference in prototyping between C and C++ language. while c++ makes the prototyping assential, ANSI C makes it optional, perhaps, to preserve the compatibility with classic C

Function prototype is a declaration statement in the calling program and in the following form

type function_name(arguments list);

The arguments list contains the types and names of arguments that must be passed to the function

Example
float valume(int x, float y, float z);

Note the each argument variable must be declared independently inside the parentheses. This is a combined declaration like

float volume(int x, float y,z); // it is illegal

in a function declaration, the names of the arguments are dummy variable and therefore, they are optional.

float volume(int,float,float);

is acceptable at the place of declaration. At this stage, the compiler only checks for the type of the arguments when the function is called In general, we can either exclude or include the variable name in the argument list of prototypes. The variable names in the prototype just act as place holders and therefore, if names are used they don’t have to match the names used in the function cal or function definition.

in the definition of function, names are required because arguments must be referenced inside the function. Example

float valume(int a, float b, float c)
{
float v=a*b*c;
……….
……….
}

the volume() function can be invoked as follows :
float cube1=volume(b1,w1,h1); //function calling
here The variable b1,w1, h1 are known as the actual parameters(arguments) which specify the dimensions of cube1. Their types(which have been declared earlier) should match with the types declared in the function prototype. Remember, the calling statement should not include names in the following example

void display();

In c++ this means that the function does not pass any parameters. it is identical to the statement

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise