What is a Function?
Breaking a complex problem into smaller parts is a common practice. We call each of these parts of a program a module and then the process of subdividing a problem into manageable parts top-down design. The principles of top-down design and structured programming dictate that a program should be divided into main module and its related modules. Each module should also be divided into sub modules according to software engineering principles, the division of modules proceeds until the module consists only of elementary processes that are intrinsically understood and cannot be further subdivided. This process is known as factoring.
C++ program consists of one or more functions, the most important of which is the function main() where execution starts and ends with main but it can call other functions to do special tasks. When you use library functions such as cin or cout, you see how one function is able to call up another function in order to carry out some particular task and then continue execution back in the calling function when the task is complete. Except for side effects on data stored at global scope, each function in a program is a self contained unit that carries out a particular operation.
When a function is called, the code within the body of that function is executed, and when the function has finished executing, control returns to the point at which that function was called.
structure of functions
A function in C (including main) is an independent module that will be called to do a specific task. A called function receives control from a calling function. When the called function complete its task, it returns control to the calling function. It may or may not return a value to the caller. The function main is called by the operating system: main in turn calls other functions. When main is complete, control returns to the operating system.
A visual representation of the above execution of program above was shown. First, the structure chart is read top-down left right. Referring to the above illustration, first we read the main module. In this case, main module represents our entire set of code. Moving down and left, we then read Function 1. On the same level with Function 1 are Function 2. The main module consists of two sub modules. At this point, however, we are dealing only with Function 1.
The main module is known as a calling module because it has sub modules. Each of the sub modules is known as a called module. But because Function 2 have sub modules, it is also called a called and a calling modules.
Communication between modules in a structure chart is allowed only through a calling module. If Function 1 needs to send data to Function 2, the data must be passed through the calling module, main module. No communication can take place directly between modules that do not have a calling-called relationship.
With this understanding, how can Function 1 send data to Function 4? It first sends the data to main module, which in turns passes the data to Function 2, and then on to Function. Although this sounds complex, it is easily done. The technique used to pass data to a function in known as parameter passing. The parameters are contained in a list that is a definition of the data passed to the function by the caller. The list serves as the formal declaration of the data types and names
In general, the purpose of a function is to receive zero or more pieces of data, operate on them, and return at most one piece of data. At the same time, a function can have a side effect. A function side effect is an action that results in a change in the state of the program. a side effect occurs, it occurs while the function is executing and before the function returns. The side effect can involve accepting data from outside the program sending data out of the program to the monitor or a file, or changing the value of a variable in the calling function.
hierarchy of functions
The Advantages of Using Function
Using function fits naturally with top-down design approach. Use of function helps to streamline the design of a program and prevents small details from obscuring the program logic.
Functions can often be used more than once in a program and in several different programs, thereby sharing programming time. A function can be viewed as a blank box, which performs a particular task within a program. It accepts input and produces certain output. When programming with function, you are plugging various block boxes into your program to accomplish various necessary tasks. Certain common task appears regularly in totally unrelated programs. In such cases, the same function can be used repeatedly.
Using function provides a natural method for dividing a programming task among a team of programmers. By defining a function as a block box, which accepts certain inputs and produces certain output, the function can be programmed as an independent entity.
Function can be tested individually. By testing at a time, the process of debugging an entire program is organized and simplified tools of programming.
Before you can call a function, you must first declare the function and then define the function.
The function declaration tells the compiler the name, return type and types of parameters of the functions. The declaration of a function is called its prototype. It contains no body of code.
The function definition tells the compiler how the function works. No function can be called from any other function unless called function has first been declared. It contains the body of the code.
Function Declaration
The basic function declaration is as follows:
return_type function_name (parameter list)
where:
return_type - can be of type void, int, float and so on...
function_name - is any meaningful name you choose to describe the function or task of the function
parameter list - specifies the type of information passed on to the function. It can also be of type int, float, void and so on…
Function Definition
The function definition contains the code for a function. It is made up of two parts: the function header and the function body which is a compound statement. Remember that a compound statement must have opening and closing braces, and it has declaration and statement sections.
function definition
Function Header
A function header consists of three parts: the return type, the function name, and the formal parameter list. A semicolon is not used at the end of the function definition header.
Function Body
The function body contains the local declarations and the function statements. The body starts with local definitions that specify the variables needed by the function. After the local definitions, the function statements, terminating with a return statement, are coded recommend that every function, even void functions, have a return statement.
Example:
User-defined functions in C++ are classified into two categories:
Value-Returning Functions
These functions return a value of a specific data type using the return statement. It used (called) in either expression or an output statement or as a parameter in a function call.
Void Functions
These functions do not return a value to the calling function. Thus, they are not used (called) in an expression. A call to void is a stand-alone statement.
We classify the basic function designs by their return values and their parameter lists.
Parameters are of two kinds:
Actual parameters are variables found in the function call whose values will be passed to the formal parameters of the called function.
Formal parameters are the variables found in the function header that will receive values from the actual parameters.
Note:
The actual parameter and formal parameter must agree in data type
The actual parameter and formal parameter must have one-to-one correspondence
In addition, a function can have variables that are local to its Scope only or it can use variables that can be accessed throughout the program, referred to as local variables and global variables respectively. Variables that are declared inside a function are called local while global variables are created by declaring them outside of any function.
Combining return types and parameter lists results in four basic designs:
void functions with no parameters
void functions with parameters
non-void functions with no parameters
non-void functions with parameters
Void Functions Without Parameters
#include<iostream>
using namespace std;
int main () {
computeArea();
return 0;
}
void computeArea() {
int a,l,w;
cout << "Area of Rectangle\n";
cout <<"Length ";
cin >> l;
cout << "Width ";
cin >> w;
a= l * w;
cout << "Area:" << a;
return;
}
Void Functions With Parameters
#include<iostream>
using namespace std;
int main () {
int l,w;
cout<<"Area of Rectangle\n";
cout <<"Length ";
cin >> l;
cout<<"Width ";
cin >> w;
computeArea(l, w);
return 0;
}
void computeArea(int x, int y) {
int a;
a= x * y;
cout<<"Area:" << a;
return;
}
Non-void Functions Without Parameters
Some functions return a value but don't have any parameters. The most common use for this design reads the data from the keyboard or a file and returns the data to the calling function.
#include<iostream>
using namespace std;
int main () {
int area = computeArea();
cout << "Area: " << area;
return 0;
}
void computeArea() {
int a,l,w;
cout<<"Area of Rectangle\n";
cout <<"Length ";
cin >> l;
cout<<"Width ";
cin >> w;
a = l * w;
cout << "Area:" << a;
return;
}
Void Functions With Parameters
#include<iostream>
using namespace std;
int main () {
int l,w;
cout<<"Area of Rectangle\n";
cout <<"Length ";
cin >> l;
cout<<"Width ";
cin >> w;
computeArea(l, w);
return 0;
}
int computeArea(int x, int y) {
int a;
a= x * y;
cout<<"Area:" << a;
return a;
}
Non-void Functions With Parameters
#include<iostream>
using namespace std;
int main () {
int a,l,w;
cout<<"Area of Rectangle\n";
cout <<"Length ";
cin >> l;
cout<<"Width ";
cin >> w;
a = computeArea(l, w);
cout << "Area:" << a;
return 0;
}
int computeArea(int x, int y) {
int ar;
ar = x * y;
return ar;
}
In the given example above, the variables l and w are the actual parameters. These variables will pass their values to the formal parameters of the function computerArea().
On the other hand, the variables x and y are the formal parameters. They will receive values passed by the variables l and w.