Functions in C++

Types of Functions in C++ are :

  1. Library functions
  2. User Defined Functions

The main( ) Function

ANSI does not specify any return type for the main ( ) function which is the starting point for the execution of a program . The definition of main( ) is

main()
{
//main program statements
} 

This is property valid because the main () in ANSI C does not return any value. In C++, the main () returns a value of type int to the operating system. The functions that have a return value should use the return statement for terminating. The main () function in C++ is therefore defined as follows.

int main( )
{
--------------
--------------
return(0)
}

main() is special in C++ in that user code is not allowed to call it; in particular, it cannot be directly or indirectly recursive. This is one of the many small ways in which C++ differs from C.

The main function returns an integer value. In certain systems, this value is interpreted as a success/failure code. The return value of zero signifies a successful completion of the program. Any nonzero value is considered a failure. Unlike other functions, if control reaches the end of main(), an implicit return 0; for success is automatically added.

User Defined Function in C++

Use can define their own function as per requirement to easy the complexity of program. There are four basic types to define user define function.

  1. No Return – No Parameter
  2. No Return – With Parameter
  3. With Parameter – No Return
  4. With Parameter – With Return

User can choose any one of the syntax to create udf. A function prototype gives information to the compiler that the function may later be used in the program.

Syntax:

returnType functionName(type1 argument1, type2 argument2, ...);

Related posts