Function in C++ :

Function Definition :

In C++, a function must be defined before it is used anywhere in the program. The general form of a function definition is as given below :

type function-name(parameter list)
{body of the function}

where the type specified the type of value that return statement of the function returns. It may be any valid C++ data type. If no type is specified, the compiler assumed that the function returns an integer value. The parameter list is a comma-separated list of variables of a function referred to as its arguments. A function may be be without any parameters, in which case, the parameter list is empty.

Example :

  1. /* Description: add 2 numbers using function in C++ */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int sum(int,int);
  7. int a,b,c;
  8. cout<< " \n Enter two numbers :";
  9. cin>>a; cin>>b;
  10. c=sum(a,b);
  11. cout<<" sum is : " <<c;
  12. }
  13. int sum(int x,int y)
  14. {
  15. int z;
  16. z = x + y;
  17. return(z);
  18. }

Output :

C++ language add 2 numbers using function

Types of Functions :

In C++ language there are two types of functions :

  • Built-in (or library) functions.
  • User-defined functions.

Built-in Functions :

These functions are part of the compiler package. These are part of standard library made available by the compiler. For example: exit(), sqrt(), pow() etc. are library functions (or built-in functions).

User-defined Functions :

The user-defined functions are created by you (the programmer). These functions are created as per requirements of your program.

Function Prototype :

Function prototyping is one very useful feature of C++ function. A function prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values.

Thus the above given examples are function definitions and the following are declarations or shall we say, function prototypes :

int absval(int a);
int gdc(int n1, int n2);

Therefore, you can say that a function has following parts :

  • return type
  • name of the function
  • argument list

Use of void :

As you know about void data type that it specifies an empty set of values and it 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);

By declaring a functions return type void, one makes sure that the function cannot be used in an assignment statement.

A function that does not require any parameter (it has an empty argument list) can be declared as follows :

type function-name(void);

As already mentioned if you omit the type specifier of a function, it is assumed to be returning int values. For the functions returning non-integer, the type specifier must be given.

Next Update


Friend's some reason we need some long time for next update, Sorry For Long Time, see you soon.

We make a Collaboration with HackIsOn YouTube Channel So, Please Subscribe Their Channel and keep supporting