What is Function in C++?
Introduction
A good programming technique is to modularize the programs
into small segments of codes. These small segments are called modules or
procedures and perform some specific tasks. In C++ language, these modules are
usually called functions. This unit
describes the basic concepts of functions, its parts, parameters, and arguments.
Here, the concept of function overloading has also been discussed with the help
of examples.
Function
In a C++ program, when a set of related statements are grouped
together, in a particular format, to accomplish a specific task is called
function. The function is one of the main building blocks of any programming
language. There is some function whose work has been fixed by the developers of
C++ programming language. These are called built-in functions. Built-in functions
are specified in its functionalities; therefore, the programmers develop their
own functions called user-defined functions. These functions are the main focal
point of this unit.
Concept and types of functions
The function is an important feature of any programming
language. It makes the work easier by writing the code once and executing it
again and again. Mainly, functions are divided into two types built-in and user-defined.
i: – Concept of function
ii: – Types of function
C functions are distributed into two type’s videlicet erected- in function and stoner functions. The ensuing sections describe these types in detail.
1:- Built-in Function
C++ language provides a number of pre-compiled functions for
some operations. These functions are called built-in functions. Built-in
functions are part of the C++ language and are highly valuable, pre-tested, and
completely reliable. The C++ Built-in functions are made for various
assignments ranging from algebra, geometry, trigonometry, finance, text and
Graphics to some complex operations.
Consider the following example that request the value of
angle and calculate its sine and display it on the screen.
// built-in function (sin())program
#include <iostream>
#include<math.h>
Using namespace std;
int main ()
{
Float angle;
cout<<” Enter value for angle:”<<endl;
cin>>angle;
cout<<”This Sine of “<<angle<<”degree is
=”<<sin(angle);
}
The output of the program
Enter a value for angle:
45.0
This Sin of 45.0 degrees is =0.850904
Here, sin() is a built-in function defined in math.h
header file. The value inside the sin() is converted to its corresponding sin
value and displayed on the screen. Similarly, we can calculate cosine, tangent , and tangent inverse by using cos(x), tan(x), and cot(x) built-in functions
respectively.
Consider another example:
//built-in function abs() program
#include <math.h>
#include<iostream>
int main()
{
int X;
cout<<”Enter
value for X”<<endl;
cin>>X;
cout<<”Absolute value of “<<X<<”is:
“<<abs(X);
}
The output of the program
Enter a value for X
-12
Absolute value -12 is: 12
For example, the value entered by the user, from the
keyboard, is taken into the abs( ) function and the result is displayed on the
screen. Like abs( ), some other functions that are included in <math.h>
header file are pow(x.n), sqrt(x), etc.
The built-in functions are defined in the C++ library which
should be included in the program before using these functions. These functions
are used for the most commonly and frequently used operations and cannot be
modified according to the user’s needs. This feature of the functions is its main
limitation. To overcome this problem, C++ provides a new set of functions that
can be defined by the users itself. The following section describes this set of
functions in detail.
2:- User-defined functions
The functions defined by the users, according to their
needs, to perform their own tasks are called user-defined functions. These
functions are used to overcome the limitations of built-in functions.
Defining the user-defined function
A function must be defined first to use it in the program.
The general syntax for defining a user-defined function is given below.
type name (parameter1, parameter2,…)
{
Statements;
}
Where:
·
Type
is the data type specified of the data returned by the function.
·
Name
is the identifier by which a function will be called.
·
Parameters
are the variables that received arguments. Each parameter has its data type
as a variable. There can be many parameters as needed but they should be separated
by commas.
·
Statements
constitute the body of the function. It is a block of statements surrounded by
braces {}.
Consider a function named addition
to take two integer values, add them together, and return the result to the
calling program. The following code segment defines this function:
//user-defined function (addition)
program
#include <iostream>
int addition (int a, int b)
{
int
sum;
sum=
a+b;
return
(sum);
}
int main()
{
int
z;
z=addition
(55, 33);
cout<<”
The sum of a and b is=”<<z;
}
The output of the program
The sum of a and b is = 88
Working of user-defined
functions
·
In the above program, addition (int a, int b) is
the example of a user-defined function defined by the user to perform the
addition of two integer values (55 and 33 in this case). These values are
passed by the function call addition (55, 33). After performing addition, the function returns the sum (88) back to the calling program (in this case, main)
using the following statement.
return (sum);
The sum is assigned to the
variable z, in main (), as shown below:
z= addition (55, 33);
The following line of code, in
main is used to display the result on the screen as shown in the above
program.
cout<<” The sum of a and b is
= “<<z;
Advantages of using
functions
Using functions we can structure
our program in a more modularized way. The use of functions provides many
benefits, including:
·
Code
reusability: The code inside the function can be reused by calling it again
and again in the program.
·
Updating
code: It is much easier to change or update the code in a function, which
needs to be done once.
·
Readability
and understandability: It makes the code easier to read and understands.