Functions in C++
Functions in C++
Function is a block of code that perform a specific task. Functions are used to avoid repetition of code.
We can make a function for a specific task and can use it repeatedly in the program.
Return Type: it is the value that return the function from the body. It could be int, float, char and bool.
If no return type it should be void.
Statement(s): statements are code that specify the task of the function
Built in function: There are many built in or predefined function available in C++. For example:
pow(x,y), sqrt(x), cos(x), log(y), gcd(x,y) prime(n) etc
User defined function: We can define our own function known as user defined function.
2. Declaration of a function
If a Function is defined after the main() its prototype must be declared before the main()
otherwise a compiler error occur.
Terminator is required when a functions is declared.
3. Calling a function
Functions are called in the main() function or in other functions.
Syntax:
void FunctionName (arguments)
{
Statement(s);
}
Here return type of function is void and arguments will be some data of type (int, float, char, bool,
double, long double).
Examples-1 Examples-2
#include<iostream> /Function that print sum of two numbers
using namespace std; #include<iostream>
void greaterno(int x, int y); using namespace std;
main() void sum(int, int);
{ main()
greaterno(20, 30); {
} sum(5, 7);
void greaterno(int x, int y) //definition }
{ void sum(int a, int b)
if(x>y) {
{
cout<<x<<endl; cout<<”sum=”<<a+b;
} }
else if(y>x)
{
cout<<y<<endl;
}
}
3. Returnable Functions without arguments
In such a type of functions input data is given from the body and output is retuned from the body.
return command followed by data, variable or expression is used in the body of a function to
return data. The data is stored back to function and can be used for any other process.
The return statement stops execution and returns to the calling function. When a return statement
is executed, the function is terminated immediately at that point, regardless of whether it's in the
middle of a loop, etc.
Syntax:
<return type> FunctionName ( )
{
statement(s);
return (data/variable)
}
Here return type of function will be some data type (int, float, char, bool, double, long double).
Examples-1 Examples-2
//Function that return greater of two numbers Define a function that can get two numbers and
#include<iostream> return sum of these numbers.
using namespace std; #include<iostream>
int greaterno(); //Declaration of a function using namespace std;
main() main()
{
{
cout<<greaterno(); //calling a function
} cout<<sum();
int greaterno() //definition }
{ int sum()
int x,y; {
cout<<"Enter the value of x and y:"; int x,y;
cin>>x>>y;
cout<<"Enter x,y:";
if(x>y)
{ cin>>x>>y;
return x; return x+y;
}
}
else if(y>x)
{
return y;
}
}
Important Note: All mathematical, relational and logical operators can be used on
returnable function.
Syntax:
<return type> FunctionName (arguments ) //prototype of function
{
statement(s); //body of function
return (data/variabale);
}
Here as usual return type of function will be some data type (int, float, char, bool, double, long
double) and argument will also be some data type (int, float, char, bool, double, long double).
//Function that print greater of two numbers Define a function that can get two numbers as
#include<iostream> argument and return sum of these numbers.
using namespace std; #include<iostream>
int greaterno(int, int); //Declaration
using namespace std;
main()
{ int sum(int x,int y);
cout<<greaterno(10, 30); //calling
} main()
int greaterno(int x, int y) //definition {
{ Int a=2, b=5;
if(x>y) cout<<sum(10,20<<endl);
{
cout<<sum(a, b)<<endl;
return x;
} }
else if(y>x) int sum(int x, int y)
{ {
return y; return x+y;
}
}
}
The value of a is given to x and value of b is given
to y on calling the function