C++ Function

A function is a subpart of a program that can be reused when needed. A function is a set of statements that perform specific task.

There are two types of functions: user defined functions and library functions.

Library Functions

C++ has some pre-defined functions in C++.
For ex:- pow(x,y), sqrt(x), floor(x), exp(x), abs(x), cos(x) etc.

Example Of Library Functions:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double num, square;
cout << "Enter a number: ";
cin >> num;
// Sqrt () is a library function to calculate square root.
square = sqrt(num);
cout << "Square root of " << num << " = " << square;
return 0;
}

Output :
Enter a number: 26
Square root of 26 = 5.09902

In the example above, the sqrt () library function is used to calculate the square root of a number. For example, if we want to calculate the square root of 26, then the sqrt() is a library function that is used to calculate the square root.


User-Defined Function

C++ allow the user to define their own function. The function is a part of the program which can be executed whenever needed by simply calling its name with proper arguments.

Function prototype (declaration)

A function declaration give the compiler information about the function name, type and its parameters.

Syntax

A function declaration has the following parts.
return_type function_name( parameter list );

For the function add () defined above, the following is the function declaration.
int add(int num1, int num2);

Parameter names are not important in function declaration only their type is required
int add(int num1, int num2);

Function call

If we want to execute the code that is written inside the function then we have to call that function. On calling that function the control of program transfer to that function.

Syntax

Function_call(parameter1,parameter2);

Function Definition

When the function is called, the program transfer his control over the first statement of the program body and all further statements will be executed sequentially. And the control of the program will return to the calling function after successfully executing the code of the called function.


// Function definition
int sum(int a,int b)
{
int add;
add = a + b;
return add;
}

Syntax of User-Defined Function
#include<iostream.h>
void function_name()
{
... ... ...
... ... ...
}
int main()
{
... ... ...
function_name();
... ... ...
}


In the above syntax the program starts with the main() function. When the control of the program reaches the function_name(); then program control moves to void function_name () and executes the code inside the function and returns to the main function.



Example User-Defined Function

C ++ program to add two integers. To add integers, create a function add () and display the sum in the main () function.

#include <iostream> 
using namespace std;
int add(int, int);
int main()
{
int x, y, sum;
cout<<"Enters two numbers to add: ";
cin >> x >> y;
// Function call
sum = add(x, y);
cout << "Sum = " << sum;
return 0;
}
// Function definition
int add(int a, int b)
{
int add;
add = a + b;
// Return statement
return add;
}


Output :
Enters two integers: 8
-4
Sum = 4

Exercise:-

1. What is the scope of the variable declared in the user defined function?

A. Whole program
B. Only inside the {} block
C. The main function
D. None of the above

View Answer


2. Constant function in C++ can be declared as

A. void display()
B. void display() const
C. const void display()
D. void const display()

View Answer



Program :-

C++ program to find max between two number using function

#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main () {
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2) {
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
}


Output :

Max value is : 200





Visit :


Discussion



* You must be logged in to add comment.