NIOS Computer Science: Chapter 12 – Function Part 4

Download PDF of This Page (Size: 299K)

Diagram shows types of functions

Diagram Shows Types of Functions

Diagram shows types of functions

User defined C++ function

Image of user defined C++ function

Image of User Defined C++ Function

Image of user defined C++ function

A C++ function is a grouping of program statements in a single unit.

The main ( ) is an essential function in all C ++ programs. If there is a single function in a program, it should be named main ( ).

The main ( ) function is the starting point for the execution of a program. The definition of main ( ) would look like as follows:

main ( )

{

// main program statements

}

In C ++, the main ( ) returns a value of type int to the operating system.

The function that has a return value should use the return statement for termination. The main ( ) function, therefore, is defined as follows:

int main ( )

{

____

____

____

return (0);

}

Since the return type of function is int by default, the keyword int in main ( ) header is optional.

Consider the program given below:

#include<iostream.h>

void main ( )

{

cout < < “Computer Science \n”;

}

It prints computer science on the screen. We can also write the above program in the following way:

# include < iostream.h>

void message ( );

void main ( )

{

message ( );

}

void message ( )

{

cout << “Computer Science\n”;

}

The above program also prints computer science but it uses a function message for printing. In addition to functions main ( ) and message ( ), the program has an extra line after # include preprocessor directive. The line is void message ( ). This is known as function prototype.