Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

FUCTIONS IN C++

What is a Function in C++?


A function is a piece of program code that performs a specific task. It can be a small task or
a big task but the function will perform that task completely. Functions take some input as
parameters and return the result as a return value.

Functions are useful for procedural programming or modular programming. If we write a


function then we can reuse it in the program.
Multiple times we can use a function inside a program. Even we can use it in other programs.
The collection of functions is known as a library.

The C/C++ languages provide a lot of built-in functions in their library and we commonly used
them for making the programming easy. Let us see how we can write functions and see the
idea behind writing the functions.

Why Do We Need Functions?

• Functions help us in reducing code redundancy. If functionality is performed at


multiple places in software, then rather than writing the same code, again and
again, we create a function and call it everywhere. This also helps in maintenance
as we have to make changes in only one place if we make changes to the
functionality in future.

• Functions make code modular. Consider a big file having many lines of code. It
becomes really simple to read and use the code, if the code is divided into
functions.

• Functions provide abstraction. For example, we can use library functions without
worrying about their internal work.
• Void main(){
}
• Every C++ program must have a main function. Here we have written void main
even we can write as int main(). And we also know that this is the entry point of our
program and our program starts execution from here only. And whatever we want, we
can write inside the curly braces of the main function.
• Suppose if we have a very big program let’s say we have some 10000 lines of code.
Then everything we are writing inside the main function only and if we have a single
function then this style of programming is called monolithic programming. If we write
the program like this, let us see the problems we will face.

Problems in Monolithic Programming:

• First problem: if there is an error in a single line then it’s an error in the entire
program or entire main function.

• Second problem: 10000 lines of code, we cannot finish it in one hour or one day, it
might take few days and throughout the time we should remember everything. Then
only we can make changes or write new lines in the program. So, we should
memorize the whole program.

• Third problem: How many people can write this one single main function with?
Only one person can write. We cannot make it as teamwork and more than one
people can’t work on the same main function. So, work cannot be distributed in a
team.

• Fourth problem: when this program becomes very big, it may fit in some computer
memories and it may not fit in some of the memories. It Depends on the size and
depends on the hardware contribution of the computer on which you are running.

How to write a function in C++?


So let us see how to write a function. First of all, the function should have a name. Then it
should have a parameter list or also called an argument list (the parameters it is taking),
then the function should have a return type. For better understanding, please have a look at
the below image.
• Functions are identified by their name. A function can take 0 or more
parameters means it may not take any input.
• A function may or may not return a value but it can return at most one value.
It cannot return multiple values but it can take multiple values as parameters.
If the function is not returning any value, then the return type should be void.
• The rules for giving function names are the same as the rules for giving the
variable names. Same rules you should follow for giving function names also.

Now let us write one function and use it in our main function.

void display(){
cout << “Hello”;
}
void main(){
display();
}

Here we have a function called display() which will print Hello when it is called. This function
is taking no arguments. We have called this function inside the main() function. So let us see
how it runs.
When the main function starts, it will come to the line where we have called
the display function, then the control will go to the display function, and then it will execute
the cout line. After executing the print line, the control will again return back and continue
the main function.

Function for Adding Two Numbers in C++.


We are writing the function for adding two numbers.

int add (int x, int y){


int z;
z = a + b;
return z;
}

This function is taking two parameters x and y of integer type. Inside this function, we have
taken an extra variable that is z. And store the result of a + b in z variable and return this z.
Now let us write main function.

void main (){


int a = 3, b = 4, c;
c = add (a , b);
cout << c << endl;
}
Inside the main function, we have taken 3 variables. Next, we have called the function ‘add’
with ‘a’ and ‘b’ as parameters and store the result in ‘c’. And print the value of ‘c’

Output:
Program 2: Program to find the maximum of three numbers using
Functions in C++

#include <iostream>
using namespace std;
int Maximum(int a, int b, int c)
{
if (a > b && a > c)
return a;
else if (b > c)
return b;
else
return c;
}

int main()
{
int x = 10, y = 15, z = 20, r;
r = Maximum(x, y, z);
cout << r << endl;
return 0;
}

Output:

What is void?
If a function is not returning any value then its return type is mentioned as void.

Difference between int main() and void main()


• void main() means the main function is not returning any value.
• int main() means main function will return 0; 0 is a success code.
• The function has terminated successfully. main() will return the
• value to the operating system, like windows.
• In C++ int main() is standard.
What is Function Overloading in C++?
In C++, we can write more than one function with the same name but with a different
argument or parameter list, and when we do so, it is called function overloading. Let us
understand this with an example.

void main(){

int a = 10, b = 2, c;

c = add(a, b);

This is our main function. Inside this function, we have declared 3 variables. Next, we are
storing the result of the ‘add()’ function in the ‘c’ variable. The following is the add function.

int add(int x, int y){

return x + y;

Here we haven’t declared any variable, simply return ‘x + y’. When we call the ‘add’ function
inside the main function, then ‘a’ will be copied in ‘x’ and ‘b’ will be copied in ‘y’ and it will
add these two numbers and the result will store in ‘c’. Now we want to write one more
function here,

int add(int x, int y, int z){

return x + y + z;
Advantages of Function Overloading in C++
The benefit here is that we don’t have to think of new names every time. As both the
functions are for adding integers, so we don’t have to give different names. It is easy for
writing the programs and you don’t have to remember too many function names. That is the
benefit we are getting now. Let us declare one more add function which returns the sum of
two float numbers.

float add(float x, float y){

return x + y;

This function will return the float type value. Now we have two add functions that are taking
the same number of parameters. Is it possible in C++? Yes, two functions can have the
same name and the same number of parameters but the data type of the parameters
should be different. They cannot be the same.

So int add (int x, int y) and float add (float x, float y) are two different functions. In C++ two
functions are said to be different if they have the same name but different parameters list

You might also like