Functions implement modularity which in turn implements a well structured programming. Modularity ensures better control of the program. Modularity is close to the real world so every OOP has modularity in it.
Consider a scenario:
- 1. A manager in a mobile store alone manages all the operations i.e. marketing, inventory control, stock, accounting etc. The manager finds himself struggling with the store operations. The store has a bad reputation. The business is suffering
- 2. A manager in a mobile store has individual supervisors for each operation i.e. marketing, inventory control, stock, accounting etc. The manager controls only the supervisors and the supervisors in turn control the operations. The mobile store is running smooth. The store has a good reputation. The business is flourishing
It is clear from above scenarios that a well structured organization runs smooth. In the same way a well structured program runs smooth and ensures efficiency.
If you are developing a calculator program. Rather than doing all the operations i.e. addition, subtraction, multiplication, division etc. in main() function only, make separate functions for each operation. It will have the benefits like:
- 1. Program has modularity
- 2. If a particular operation is wrong, you have to debug that function only.
- 3. It is easy to understand the functions.
- 4. Control is better
How to use functions in C++
Before making a function the following things should be decided
- 1. Does the function return any value or return nothing means void
- 2. Does the function have any arguments or parameters
- 3. Meaningful name of the function
- 4. The working of the function
A typical function to return the sum of two integer numbers can be like this:
int add(int,int);
The picture will be clearer by the programs in this tutorial.
Now remember these points that a function appears 3 times in a program. For example
- 1. Function Prototype: A Function Prototype means an indication to compiler that a particular function will be used later in the program.
- 2. Function Call: A Function Call means to call a function to work
- 3. Function Definition: A Function Definition means the actual working of the function
There can be the following categories of the functions:
- 1. Function without arguments and without return value
- 2. Function with arguments and without return value
- 3. Function with arguments and with return value
These things will be clearer by the programs in this tutorial.
Look at the following programs:
A program to show Functions without arguments and without return value
/* the following program has 4 functions with no arguments and no return type*/ #include<iostream.h> #include<conio.h> void add(); //function prototype of add() void subtract(); //function prototype of subtract() void multiply(); //function prototype of multiply() void division(); //function prototype of division() void main() { clrscr(); int choice; char ans; cout<<"\nWant to see Menu Y or y for yes\n"; cin>>ans; while(ans=='y'||ans=='Y') { cout<<"\nMENU\n"; cout<<"1. Add\n"; cout<<"2. Subtract\n"; cout<<"3. Multiply\n"; cout<<"4. Division\n"; cout<<"\Enter the Choice 1-4\n"; cin>>choice; switch(choice) { case 1: add(); //function call to add() break; case 2: subtract(); //function call to subtract() break; case 3: multiply(); //function call to multiply() break; case 4: division(); //function call to division() break; default:cout<<"\nSorry Wrong Choice Try Again\n"; } cout<<"\nWant to see Menu Y or y for yes\n"; cin>>ans; } getch(); } void add() //function definition of add() { int a,b; cout<<"\nEnter two numbers a and b\n"; cin>>a>>b; cout<<"Sum of the numbers a and b is"<<a+b; } void subtract() //function definition of subtract() { int a,b; cout<<"\nEnter two numbers a and b\n"; cin>>a>>b; cout<<"a-b is"<<a-b; } void multiply() //function definition of multiply() { int a,b; cout<<"\nEnter two numbers a and b\n"; cin>>a>>b; cout<<"Product of the numbers a and b is"<<a*b; } void division() //function definition of division() { int a,b; cout<<"\nEnter two numbers a and b\n"; cin>>a>>b; cout<<"a/b is"<<a/b; }
A program to show Functions with arguments and without return value
/* the following program has 4 functions with arguments and no return type*/ #include<iostream.h> #include<conio.h> void add(int,int); void subtract(int,int); void multiply(int,int); void division(int,int); void main() { clrscr(); int choice; char ans; cout<<"\nWant to see Menu Y or y for yes\n"; cin>>ans; while(ans=='y'||ans=='Y') { int a,b; cout<<"\nEnter two numbers a and b\n"; cin>>a>>b; cout<<"\nMENU\n"; cout<<"1. Add\n"; cout<<"2. Subtract\n"; cout<<"3. Multiply\n"; cout<<"4. Division\n"; cout<<"\Enter the Choice 1-4\n"; cin>>choice; switch(choice) { case 1: add(a,b); break; case 2: subtract(a,b); break; case 3: multiply(a,b); break; case 4: division(a,b); break; default:cout<<"\nSorry Wrong Choice Try Again\n"; } cout<<"\nWant to see Menu Y or y for yes\n"; cin>>ans; } getch(); } void add(int a,int b) { cout<<"Sum of the numbers a and b is: "<<a+b; } void subtract(int a,int b) { cout<<"a-b is: "<<a-b; } void multiply(int a,int b) { cout<<"Product of the numbers a and b is: "<<a*b; } void division(int a,int b) { cout<<"a/b is: "<<a/b; }
A program to show Functions with arguments and with return value
/* the following program has 4 functions with arguments and return type */ #include<iostream.h> #include<conio.h> int add(int,int); int subtract(int,int); int multiply(int,int); int division(int,int); void main() { clrscr(); int choice; char ans; cout<<"\nWant to see Menu Y or y for yes\n"; cin>>ans; while(ans=='y'||ans=='Y') { int a,b,c; cout<<"\nEnter two numbers a and b\n"; cin>>a>>b; cout<<"\nMENU\n"; cout<<"1. Add\n"; cout<<"2. Subtract\n"; cout<<"3. Multiply\n"; cout<<"4. Division\n"; cout<<"\Enter the Choice 1-4\n"; cin>>choice; switch(choice) { case 1: c=add(a,b); cout<<"Sum of the numbers a and b is: "<<c; break; case 2: c=subtract(a,b); cout<<"a-b is: "<<c; break; case 3: c=multiply(a,b); cout<<"Product of the numbers a and b is: "<<c; break; case 4: c=division(a,b); cout<<"a/b is: "<<c; break; default:cout<<"\nSorry Wrong Choice Try Again\n"; } cout<<"\nWant to see Menu Y or y for yes\n"; cin>>ans; } getch(); } int add(int a,int b) { return a+b; } int subtract(int a,int b) { return a-b; } int multiply(int a,int b) { return a*b; } int division(int a,int b) { return a/b; }
If anyone wants more clarification, he or she can post comments in this article. I will clarify the doubts.Thanks for reading this article.
No comments:
Post a Comment