FUNCTIONS IN C++
What is a Function?
A function is a set of statements that perform a particular
task. When a particular operation needs to be done time and again in a program,
we can define a function to perform that operation. The function can then be
used anywhere in the program any number of times. Functions are one of the most
important parts of any program language. In this article we will learn about
basics of functions.
The main() function
Technically, when we compile a program, there should be a starting
point from where our program starts its execution. This starting point is the
function main(). Every C++ program must have one and only one main()
function. The main() function is an interesting aspect of C++ and we
will read in details about it in some later articles. But almost always, it has
an int return type and no arguments.
Types of functions
In C++, there are 2 types of functions.
In-built library functions :
There are many functions that are already defined under some
specific header files and are already present in the installation file for C++.
All these header files form the Standard Template Library (STL) of C++. To use
these pre-defined functions, we need to include the specific header file in our
program using the syntax :
#include<header_name>
To read in details
about STL see other articles on our website.
User-defined functions :
These functions are declared and defined by a programmer
according to his need and convenience. In this article and the articles that
follow, we will be focusing on user-defined functions only.
Function Definition
A function definition consists of the function prototype and
its body.
Function Prototype : The
entire function declaration, i.e. the return type, function name and the parameter
list constitute a function prototype.
Function signature : It is
a subset of function prototype. The parameter list is termed as a function’s
signature. The use of this term arrives while discussing Function Overloading.
return_type : It is a valid C++ datatype. A
function may or may not return any value. In case if a function doesn’t return
any value, the return_type is set as void. Otherwise, the return_type is
any valid C++ datatype (primitive, derived or user-defined) depending upon the
type of value being returned.
function_name : This the name of the
function. The function is known in the program by this name. While naming a
function, keep the following points in mind :
First, it can only consist of alphabets, digits and underscore(_).
Second, it cannot contain any special character (not
even space).
Third, it cannot start with a digit.
Fourth, it must not match any keyword in C++.
Fifth, As C++ is a case dependent language, using different case
letters means different names. Example, func & Func are two different
names.
Argument List : It is a collection of
the type of values that the function will take whenever it is called. For
example,
(int a,
float b)
This list will take 2 values whenever it is called, one of int
type and other of float type. These values will be stored in a and b
respectively.
Body : It is the set of statements
that perform the desired operation. They may call other functions as well.
Let us see a demo function definition :
//function to take 2 integers as
argument
//and return the maximum of the
two.
int max (int a, int b)
{
int result;
if(a>b)
result=a;
else
result=b;
return result;
}
Here the function named max is created which takes two integer values as a & b and then
returns an int value. See the following statement :
int great = max(5 , 10);
cout << great;
Output : 10
Here, the function puts 5 in a and 10 in b and then executes all its body statements. As b is greater than a, 10 is stored in result and it is returned. This return value is caught by the int variable great. So now great stores 10 (i.e. the larger value among a & b).
Function Declaration
A function declaration tells the compiler about a function
name and how to call that function. Basically, a function declaration is the
function prototype. The actual body of the function can be defined separately.
Also, in declaration without body, there is no need to name
the arguments. Only the type name is sufficient. Example :
int max(int a, int b);
int max(int, int);
both are correct.
There are a few things you must keep in mind while declaring
and defining a function :
First, the functions that are directly called into main()
must be at least declared before main().
Their full definition may be made before or after main(). This is
important as the compiler must know that such a function that is being called
in main() actually exists.
Second, any other function, that is not being directly
called into main(), can be declared and defined anywhere in the program.
Third, a function must be declared and defined outside of all
functions.
A demo program in C++
Output Screen :
Enter 2 integers : 25 34
Larger integer is 34
If you find something wrong or if you
wish to add some information in this article, feel free to comment on this post
or contact us at coding.nkcoder@gmail.com.
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Post a Comment