Function in C ++
All the inbuilt functions of the C language can be used in C ++ and the user can also create the function according to himself, which are called user-defined functions. The program of C ++ is under Structured Programming, that is, it consists of dividing the program into various functions. Function contributes significantly to the program. Many functions can be easily done by the function.
The function is a type of program that performs a particular function. In addition to shortening the function program, the execution speed of the program also increases.
Function Prototype
The function prototype provides information to the compiler regarding the interface of the function, ie, the number of Arguments given in the function related to the data type and the Return value type of the Argument.
The function prototype is determined at the time of function declaration. When calling a function, the compiler compares the function to the prototype made at the time of declaration, that is, to ensure that the correct argument is given at the function calling time and whether the return type is correct.
A Function Prototype is a declaration statement with Syntax as follows.
Syntax:-
return_type function_name (argument_list);
here
- return_type: - is the type of data returned by the function.
- function_name : - is the name given to a function.
- argument_list: - is the number of arguments given to the function and their type.
Example:-
- int area (int, float);
- Float add (float a, float b);
- void input (void);
- void output ( );
- void input ( );
And
int sum (int a, int b, int c);
You can write it as follows -
int sum (int, int, int)
At the time of function declaration, the Compiler Argument determines the type, not their names, so when we have to declare the function only. So it can also be declared by giving it the Argument type alone.
Creating Function
The function can be made in C ++ in various ways. There are mainly four types of functions depending on the function prototype -
- With argument with return
- With argument but no return
- No argument but return
- No Argument but no return
Out of all these functions, we understand the first one, you can also do the rest of the functions based on the example given below. Generally, only the first and second functions are used more.
Function with an argument with return
When we give an argument in a function and the function returns the result by processing those arguments. So such a function is called With Argument With Return Function. If we have to make a program to add two numbers, then we will create a function for this as follows.
#include<iostream.h>
#include<conio.h>
int add (int, int) ; //Function declaration
void main ()
{
clrscr ();
int x, y;
cout<<“\n enter any two value”;
cin>>x>>y;
int r;
r = add (x, y); //फंक्शन (Function) calling
cout <<“total =”<<r;
getch ();
}
int add (int a, int b); // Function definition
{
int c;
c = a+b;
return(c);
}
Or you can create the following types -
int add (int a, int b); // Function declaration
{
int c;
c = a+b;
return c;
}
void main()
{
clrscr();
int x = 13;
int y = 14;
cout<<add (x,y);
}