In this tutorial, we are going to learn about functions in C++.
If we need to write a code to form a circle and color it. We can create two functions to solve this problem:
Dividing a complex code into small functions to make it easy to read and also make your program short. The major benfits of a function is that, we can reuse it in one program.
There are mainly two types of functions :-
In this tutorial, we will focus mostly on user-defined functions.
C++ allows the programmer to create their own functions.
A user-defined functions group code to do a specific task and that group of code is named “identifiers”.
When the function is used in any part of the program, it runs the codes defined in the body of the function.
The syntax of declaring a function is :
return-type function_Name (parameter 1, parameter 2,......) { // function body }
For example :
# Function declaration
void test() { cout << "Hello World"; }
Here,
test()
.
void
.
{...}
.
In the above program, we had declared a function named test(). To use the test() function, it needs to be called , which is known as calling of a function.
Here's how we can call the test()
function.
int main() { // calling a function test(); }
Example 1 : Display a text
#include <iostream> using namespace std; // declaring a function void test() { cout << "Hello India"; } int main() { // calling the function test(); return 0; }
Output :
Hello India
A function can be declared with parameters (arguments). A "parameter" is a value that is passed when declaring a function.
For example, let us consider the function printNum :
void printNum(int num) { cout << num; }
In the above function, the int
variable num is the function parameter.
We pass a value to the function parameter while calling the function.
int main() { int n = 8; // calling the function // n is passed to the function as argument printNum(n); return 0; }
Example 2 : Function with Parameters
#include <iostream> using namespace std; void displayNum(int i1, float f2) { cout << "The int number is " << i1 << endl; cout << "The double number is " << f2; } int main() { int num1 = 5; double num2 = 5.5; // calling the function displayNum(i1, f2); return 0; }
Output :
The int number is 5 The double number is 5.5
In above program, we have used a function that has one “int” parameter and one “double parameter”.
We then pass num1 and num2 as arguments. These values are stored by the function parameters n1 and n2 respectively.
Note : The type of the arguments passed while calling the function must match with the corresponding parameters defined in the function declaration.
In the above programs, we have used void in the function declaration. For example :
void displayNumber() { // code }
Aove code shows the function is not returning any value.
It's also possible to return a value from a function. For this, we need to specify the “returnType” of the function during function declaration.
Then, the return
statement can be used to return a value from a function.
For example,
int add (int a, int b) { return (a + b); }
Here, we have the data type int
instead of void
. This means that the function
returns an
“integer” value.
The code return (a + b);
returns the sum of the two parameters as the function value.
The return statement denotes that the function has ended. Any code after return inside the function is not executed.
Example 3 :Add Two Numbers
#include <iostream> using namespace std; int add(int a, int b) { return (a + b); } int main() { int sum; sum = add(300 + 45); cout << "300 + 45 = " << sum << endl; return 0; }
Output :
300 + 45 = 345
In the above program, the add()
function is used to find the sum of two numbers.
We pass two int literals 300 and 45 while calling the
function.
We store the returned value of the function in the variable sum, and then we print it.
In C++, the code of function declaration should be before the function call. However, if we want to define a function after the function call, we need to use the function prototype.
For example,
// function prototype void add(int, int); int main() { // calling the function before declaration. add(5, 3); return 0; } // function definition add(int a, int b) { cout < (a + n); }
In the above code, the function prototype is :
void add(int, int);
This helps the compiler with information about the function name and its parameters. That's why we can use the code to call a function before the function has been defined.
The syntax of a function prototype is :
returnType functionName(dataType1, dataType2, ...);
Example 4 : Funtion prototype
#include <iostream> using namespace std; int add(int a, int b) { return (a + b); } int main() { int sum; sum = add(300 + 45); cout << "300 + 45 = " << sum << endl; return 0; }
Output :
300 + 45 = 345
The above program is nearly identical to Example 3. The only difference is that here, the function is defined after the function call.
That's why we have used a function prototype in this example.
Library functions are the built-in functions in C++ programming.
Programmers can use library functions by invoking the functions directly; they don't need to write the functions themselves.
Some common library functions in C++ are sqrt()
, abs()
, isdigit()
,
etc.
In order to use library functions, we usually need to include the header file in which these library functions are defined.
For instance, in order to use mathematical functions such as sqrt()
and abs()
,
we need to
include the header file cmath
.
Example 5 : C++ Program to Find the Square Root of a Number
#include <iostream> #include <cmath> using namespace std; int main() { double number, squareRoot; number = 25.0; // sqrt() is a library function to calculate the square root squareRoot = sqrt(number); cout << "Square root of " << number << " = " << squareRoot; return 0; }
Output :
Square root of 25 = 5
In this program, the sqrt() library function is used to calculate the square root of a number.
The function declaration of sqrt()
is defined in the cmath
header file. That's
why we need to
use the code #include <cmath>
to use the sqrt() function.
To learn more, visit C++ Standard Library functions.