Skip to main content

function in cpp

Functions​

A function in C++ refers to a group of statements that takes input, processes it, and returns an output.

Features of function in C++​

  • Re-usability
  • write once use multiple times

Syntax :

<Returntype> functionName(Parameter)
{ //start fun body

return <value of return type>

}//end fun body

void = nothing/empty

There are Two type of Function

  1. Inbuild

  2. User defined

User defined function​

Example 1 :

#include <iostream>
using namespace std;

// Function Defination
void sayHello()
{
cout<<"Hello"<<endl;
}

int main(){

// Function Call
sayHello();
return 0;
}

Output :

Hello

Explanation :

In a Line no 4 we pass comment // Function Defination.
In a Line no 5 create user defined function that name has sayHello() .
print message Hello using cout.
In a Line no 12 we pass comment // Function Call.
In main function we call sayHello() function.

Example 2 :

#include <iostream>
using namespace std;

// Function Defination
void sayHello()
{
cout<<"Hello"<<endl;
}

int main(){

// Function Call
sayHello();
sayHello();
sayHello();
return 0;
}

Output :

Hello
Hello
Hello

Explanation :

In a Line no 4 we pass comment // Function Defination.
In a Line no 5 create user defined function that name has sayHello() .
print message Hello using cout.
In a Line no 12 we pass comment // Function Call.
In main function we call 3 times sayHello() function.

Wap to print Suare of number using user-defined function.

Example 3 :

#include <iostream>
using namespace std;

// Function Defination
void calsquare(int num)
{
int square = num * num;
cout<<"Square :"<<square;
}

int main(){

// Function Call
calsquare(10);

return 0;
}

Output :

Square : 100

Explanation :

In a Line no 4 we pass comment // Function Defination.
In a Line no 5 create user defined function that name has calsquare() and pass one parameter (int num) .
Declare int variable that name has square and store num * num.
print Square : and square variable.
In a Line no 12 we pass comment // Function Call.
In main function we call calsquare(10); function and pass 10 value.

Example 4 :

#include <iostream>
using namespace std;

// Function Defination
void calsquare(int num)
{
int square = num * num;
cout<<"Square :"<<square;
}

int main(){

// Function Call
calsquare(10);
calsquare(5);
calsquare(8);

return 0;
}

Output :

Square : 100
Square : 25
Square : 64

Explanation :

In a Line no 4 we pass comment // Function Defination.
In a Line no 5 create user defined function that name has calsquare() and pass one parameter (int num) .
Declare int variable that name has square and store num * num.
print Square : and square variable.
In a Line no 12 we pass comment // Function Call.
In main function we call 3 times calsquare() function and pass 3 different value.

Wap to print addition of two number using user-defined function.

Example 6 :

#include <iostream>
using namespace std;

// Function Defination
void addTwoNum(int a, int b)
{
int sum = a + b;
cout<<"Sum : "<<sum<<endl;
}

int main(){

// Function Call
addTwoNum(10, 20);

return 0;
}

Output :

Sum : 30

Explanation :

In a Line no 4 we pass comment // Function Defination.
In a Line no 5 create user defined function that name has addTwoNum() and pass two parameter (int a, int b) .
Declare int variable that name has sum and store a + b.
print Sum : and sum variable.
In a Line no 12 we pass comment // Function Call.
In main function we call addTwoNum(10, 20); function and pass 10 and 20 value.

Example 7 :

#include <iostream>
using namespace std;

// Function Defination
void addTwoNum(int a, int b)
{
int sum = a + b;
cout<<"Sum : "<<sum<<endl;
}

int main(){

// Function Call
addTwoNum(10, 20);
addTwoNum(100, 5);

return 0;
}

Output :

Sum : 30
Sum : 105

Explanation :

In a Line no 4 we pass comment // Function Defination.
In a Line no 5 create user defined function that name has addTwoNum() and pass two parameter (int a, int b) .
Declare int variable that name has sum and store a + b.
print Sum : and sum variable.
In a Line no 12 we pass comment // Function Call.
In main function we call addTwoNum(10, 20) function and pass 2 different value(10, 20).

Example 1 :

#include <iostream>
using namespace std;

void cal_perimerter(int l, int b)
{
int perimeter = 2 * (l + b);
cout<<"Perimeter :"<<perimeter<<endl;
}
int main() {

cal_perimerter(5, 8);

return 0;
}

Output

Perimeter : 26

Explanation :

In the above example, we can create a function. The name of that function is cal_perimeter.In the 2nd line we can create a variable perimeter. and store formula of perimeter 2 * (l * b) In next line we print perimeter and value endl for new line.
In int main() we call function cal_perimeter and pass two value (5, 8).

Example 2 :

#include <iostream>
using namespace std;

void cal_perimerter(int l, int b)
{
int perimeter = 2 * (l + b);
cout<<"Perimeter :"<<perimeter<<endl;
}
int main() {
int l1 =5 , b1 = 8;

cal_perimerter(l1, b1);

return 0;
}

Output

Perimeter : 26

Explanation :

In the above example, we can create a function. The name of that function is cal_perimeter.In the 2nd line we can create a variable perimeter. and store formula of perimeter 2 * (l * b) In next line we print perimeter and value endl for new line.
create l1 and b1 variable l1 store 5 value b1 store 8 value, and datatype is int.
In int main() we call function cal_perimeter and pass two value (5, 8).

Example 3 :

#include <iostream>
using namespace std;

void cal_perimerter(int l, int b)
{
int perimeter = 2 * (l + b);
cout<<"Perimeter :"<<perimeter<<endl;
}
int main() {
int l1 =5 , b1 = 8;
cal_perimerter(l1, b1);

int l2 =7 , b2 = 3;
cal_perimerter(l2, b2);

return 0;
}

.

Explanation :

In the above example, we can create a function. The name of that function is cal_perimeter.In the 2nd line we can create a variable perimeter. and store formula of perimeter 2 * (l * b) In next line we print perimeter and value endl for new line.
create l1 and b1 variable l1 store 5 value b1 store 8 value, and datatype is int.
In int main() we call function cal_perimeter and pass two value (5, 8).
create l2 and b2 variable l2 store 7 value b2 store 3 value, and datatype is int.
In int main() we call function cal_perimeter and pass two value (7, 3).

Output

Perimeter : 26 perimeter : 20

4 Types of function

  • Function with argument and with return value

  • Function with argument and without return value

  • Function without argument and with return value

  • Function without argument and without return value

    1 Function with argument and with return value

//  Function with argument and with return value
#include<iostream>
using namespace std;

int cal_square(int num)
{
int sq = num * num;
return sq;
}
int main() {

int ans = cal_square(10);
cout<<"Ans :"<<ans;

return 0;
}

output

100

Explanation :

In the above example, we can create a function. The name of that function is cal_square. pass argument int num.
initialize sq variable num * num and return sq variable.
initialize ans variable and call cal_square(10) and pass parameter 10
print Ans and variable.

2 Function with argument and without return value


// Function with argument and without return value

#include<iostream>
using namespace std;

int cal_square(int num)
{
int sq = num * num;
cout<<sq;
}
int main() {

cal_square(5);

return 0;
}

output

25

Explanation :

In the above example, we can create a function. The name of that function is cal_square. pass argument int num.
initialize sq variable num * num and print sq variable.
call cal_square(5) and pass parameter 5

3 Function without argument and with return value


//Function without argument and with return value

#include<iostream>
using namespace std;

int cal_square()
{
int num = 20;
int sq = num * num;
return sq;
}
int main() {
int ans = cal_square();
cout<<"Ans : "<<ans;

return 0;
}

output

400

Explanation :

In the above example, we can create a function. The name of that function is cal_square.
initialize num variable and store 20 value.
initialize sq variable num * num and return sq variable.
initialize ans variable and call cal_square(10) and pass parameter 10
print Ans and variable.

4 Function without argument and without return value

// Function without argument and without return value
#include<iostream>
using namespace std;

void cal_square()
{
int num = 10;
int sq = num * num;
cout<<sq;
}
int main() {

cal_square();

return 0;
}

Output

100

Explanation :

In the above example, we can create a function. The name of that function is cal_square.
initialize num variable and store 10 value.
initialize sq variable num * num and return sq variable.
call cal_square(10)

In-build function​

  1. min();
  2. min();
  3. sqrt();
  4. pow();
  5. floor();
  6. ceil();
  7. abs();

1. min();

//   min function
#include<iostream>
using namespace std;

int main()
{
int ans = min(5, 10);
cout<<"Result :"<<ans;

return 0;
}

Output

5

Explanation :

In this example initialize ans variable and call min() function and pass 2 parameter (5, 10)
min(); function is a in-build function and give minimum value.
print Result : and ans variable.

2. max();

//   max function
#include<iostream>
using namespace std;

int main()
{
int ans = min(50, 20);
cout<<"Result :"<<ans;

return 0;
}

Output

20

Explanation :

In this example initialize ans variable and call max() function and pass 2 parameter (50, 20)
max(); function is a in-build function and give maximum value.
print Result : and ans variable.

//  max function
#include<iostream>
using namespace std;

int main()
{
int ans = max(10, 30);
cout<<"Result :"<<ans;

return 0;
}

Output

50

Explanation :

In this example initialize ans variable and call max() function and pass 2 parameter (10, 30)
max(); function is a in-build function and give maximum value.
print Result : and ans variable.

3. sqrt();

//  sqrt function
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int result = sqrt(25);
cout<<"Result :"<<result;

return 0;
}

output

5

Explanation :

  • #include<cmath> used this library in this function.

In this example initialize result variable and call sqrt() function and pass one parameter (25)
sqrt(); function is a in-build function and give square.
print Result : and result variable.

//  sqrt function
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int result = sqrt(78);
cout<<"Result :"<<result;

return 0;
}

output

8.63176

Explanation :

In this example initialize result variable and call sqrt() function and pass one parameter (78)
sqrt(); function is a in-build function and give square.
print Result : and result variable.

4. pow();

//  pow function
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int result = pow(2, 3);
cout<<"Result :"<<result;

return 0;
}

output

8

Explanation :

In this example initialize result variable and call pow() function and pass 2 parameter (2, 3)
pow(); function is a in-build function and give power.
print Result : and result variable.

5. floor();

//  floor function
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int result = floor(3.4);
cout<<"Result :"<<result;

return 0;
}

output

3

Explanation :

In this example initialize result variable and call floor() function and pass 1 parameter (3.4)
sqrt(); function is a in-build function and give lower value .
print Result : and result variable.

6. ceil();

//  ceil function
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int result = ceil(3.4);
cout<<"Result :"<<result;

return 0;
}

output

4

Explanation :

In this example initialize result variable and call ceil() function and pass 1 parameter (3.4)
sqrt(); function is a in-build function and give upper value .
print Result : and result variable.

7. abs();

//  floor function
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int result = abs(30);
cout<<"Result :"<<result;

return 0;
}

output

30

Explanation :

In this example initialize result variable and call abs() function and pass 1 parameter (30)
sqrt(); function is a in-build function and give negative value to positive value .
print Result : and result variable.

//  floor function
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int result = abs(-30);
cout<<"Result :"<<result;

return 0;
}

output

30

Explanation :

In this example initialize result variable and call abs() function and pass 1 parameter (-30)
sqrt(); function is a in-build function and give negative value to positive value .
print Result : and result variable.