A good programmer writes a program that consists of many of there small functions a program written with numerous functions is easier to maintain and debug them in one very large program every c++ program must have a main function to calls other functions[drivers of all]
By default every function return int type when the return type not specified.
Types of function
1.Built in function(library function)
2.User defined function.
Built in function(Library function)
These functions are the part of the compiler package ,these are the part of the std library made available by compiler for example: sqrt(),strlen(),exit() etc.
Turbo C++ is an IDE package (integrated development environment)
User-defined function
The user-defined function is created by the programmer. these functions are created as per the requirement of our program
THE MAIN FUNCTION
The main function is the starting point for the execution of a program.
The definition of main () function be like that:
void main()
{
//main program code written here
}
In C++ the main function returns a value of type int to the operating system.C++ explicitly defines a prototype to any of these two :
int main();
int main (int arg, char *arg[]);
the functions that have return value should use the return statements for termination.the main function defined in C++ :
int main
{
...........
............
return 0;
}
FUNCTION PROTOTYPING
So far, we have written all of our code in the main function. This is fine if you are doing a
a single task, but once you start doing more with a program, the code will become bigger and
over a period of time, everything will be in the main function, which will look very
confusing.
A function has the following syntax:
type function name (parameter1, parameter2) {statements;}
Example: float volume (int x, float y, float z);
FUNCTION
Going from left to right, the type here is the return type. After performing a statement, a
function is capable of returning a value. This value could be of any type, so we specify a
type here. A function has only one variable at a time.
The function name is the name of the function itself. Then, inside brackets, you will pass in parameters. These parameters are variables of a
certain type that are passed into the function to perform a certain function. Here as an example: two parameters are passed in but you can pass as many parameters
you want. You can pass in more than one parameter per function and each parameter is
separated by a comma.
Let's look at this example
#include <iostream>
#include <conio.h>
// Program prints out values to screen
void add(int a, int b)
{
int c = a + b;
std::cout << "Sum of " << a << " and " << b << " is " << c <<
std::endl;
}
int main()
{
int x = 28;
int y = 12;
add(x, y);
getch();
return 0;
}
Here, we create a new function called add. For now, make sure the functions are added
before the main function, otherwise main will not know that the function exists.
The add function doesn't return anything so we use the void keyword at the start of the
function. Not all functions have to return a value. Next, we name the function add and then
pass in two parameters, which are a and b of type int.
In the function, we create a new variable called c of type int, add the values of the arguments passed in, and assign it to c. The new add function finally prints out the value of
c.
Furthermore, in the main function, we create two variables called x and y of type int, call
the add function, and pass in x and y as arguments.
When we call the function, we pass the value of x to a and the value of y to b, which is
added and stored in c to get the following output:
SUM OF 28 AND 12 IS 40
When you create new functions, make sure they are written above the main function,
otherwise, it will not be able to see the functions and the compiler will throw errors.
Now let's write one more function. This time, we will make sure the function returns a
value. Create a new function called multiply, as follows:
int multiply(int a, int b) {
return a * b;
}
In the main function, after we've called the add function, add the following lines:
add(x, y);
int c = multiply(12, 32);
std::cout << "Value returned by multiply function is: " << c <<
std::endl;
In the multiply function, we have a return type of int, so the function will expect a return
value at the end of the function, which we return using the return keyword. The returned
value is variable a multiplied by variable b.
In the main function, we create a new variable called c; call the multiply function and
pass in 12 and 32. After being multiplied, the return value will be assigned to the value of
c. After this, we print out the value of c in the main function.
The output of this is as follows:
SUM OF 28 AND 12 IS 40
VALUE RETURNED BY MULTIPLY FUNCTION IS 384.
We can have a function with the same name but we can pass in different variables or
different numbers of them. This is called function overloading.
Create a new function called multiply, but this time, pass in floats and set the return value
to a float as well:
float multiply(float a, float b) {
return a * b;
}
This is called function overloading, where the function name is the same but it takes
different types of arguments.
In the main function, after we've printed the value of c, add the following code:
float d = multiply(8.352f, -12.365f);
std::cout << "Value returned by multiply function is:<<d<<std::endl;
Post a Comment