FUCTIONS IN CPP BY MR Sonwabiso
FUCTIONS IN CPP BY MR Sonwabiso
FUCTIONS IN CPP BY MR Sonwabiso
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.
• 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.
• 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.
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.
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.
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.
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.
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,
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.
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