In the previous post, we have made the code in the main subroutine or int main()
very short using another subroutine. Yes, int main()
at the start is also a function that we are dealing with. But now, we will be dealing with multiple functions rather than one. We did all codes in just one function.
The question arises here that what is the fun of making many functions rather than doing all in just one?
If you know science (which you should), you will find out that science is no more a book! The knowledge of science gradually increases so much that a person can’t simply deal with it! It is extremely hard to deal with all the sciences within one section. Therefore, it is divided into many sections of which the main ones are Mathematics, Physics, Chemistry and Biology. And then, the knowledge within these sections increases tremendously. Then, these sections got further sub-sections and so on. The page about branches (or sections) of science.
But why is that much division? This is for the sake of simplicity, flexibility and convenience. Flexibility means that you can choose to study what you want within science. Of course, everyone doesn’t love everything within science. Some want Physics, others may want Biology and so on. It gets simple in the way that you learn things in small parts. Just imagine what if science is a name of a topic with no further topics. It might consist of a million pages. This will definitely, make you hate science. And just think, what if someone says, “There is a spelling mistake somewhere in just one word! Find it!”.
NOTE: Even this post is divided into many paragraphs!
Same is the case with codes! One can’t simply deal with even a thousand lines of code in the main function! Finding out a bug (other than the syntax one) in these lines is a headache! The only way to make branches out of the main function is simply to make more functions.
In the previous post, we make a function named checkRegistrationEligibility()
. Functions are recognized by a pair of round brackets at the end. Therefore, checkRegistrationEligibility
method or function must have a pair of round brackets at the end when declaring it, defining it and calling it.
Below is given the format of any function (when defining it):
-return_type- -function_name- (-parameters-)
{
-all_code_goes_here-
return -return_type_variable-;
}
where…
-return_type-
can be replaced with void
, int
, float
, double
, non-primitive string
or any other primitive type or types that are user-defined (We will learn how to define our own types).
-parameters-
can be replaced by the parameter getting into the function or simply nothing.
-return_type_variable-
can be replaced by the variable of the same type as that of the type mentioned at the start of the function (i.e type which replaced -return_type-
).
The format may look confusing. If you remove all the codes the checkRegistrationEligibility()
and see it know, it may look similar to the format.
void checkRegistrationEligibility() //no parameters in round brackets
{
//all code goes here...
//no 'return' as the return type is 'void' which means nothing to return!
}
But having a return
keyword doesn’t matter except that it should be at the end, and without any variable next to it! Calling a function in the main function is very easy. Just have the name of the function with a pair of round brackets at the end and then, of course, a semi-colon!
Let us put all the codes again in the function and make some changes to it. This function should let the main function know whether the person is eligible for going to the next section or not! We will create a boolean variable to know it.
void checkRegistrationEligibility() //make sure that the spellings are right
{
bool isEligible = false; //By default, it is false.
string name;
cout << "Enter your name: ";
getline(cin, name);
cout << "Your name is " << name << "." << endl;
int age;
cout << "Enter your age: "; cin >> age;
if (age >= 18)
{
cout << name << ", you are eligible for registration." << endl;
cout << "In other words, you can go to next section for registration!" << endl;
cout << endl << "***Thank you for visiting us!***" << endl;
isEligible = true;
}
else
{
cout << name << ", we are sorry that you are not eligible for registration!" << endl;
cout << "You can register after " << 18 - age << " years." << endl;
cout << endl << "***Thank you for visiting us!***" << endl;
}
}
int main()
{
checkRegistrationEligibility();
cout << "Is that person eligible = " << isEligible << endl;
}
Try running it! And you got an error! isEligible
is unknown to the main function! To remind you, every variable has a scope and the scope of isEligible
variable is limited to that function. If you don’t know anything about the scope of a variable, you should read this post first.
The solution is to declare this boolean variable at the top of both the two functions. But then, it will be something not new! Also, you may not want to do that for 1000 functions. You may want to use the functions in the other programs without having a variable to be declared each time for these functions to be used. The freely floating functions should be completely independent of any global variables and must depend all on themselves. From freely floating functions, I meant all those functions which don’t have any associations with user-defined types (Don’t worry! We will discuss them soon). The better solution is to return a boolean variable!
bool checkRegistrationEligibility() //not the keyword 'bool' at the start of the line
{
string name;
cout << "Enter your name: ";
getline(cin, name);
cout << "Your name is " << name << "." << endl;
int age;
cout << "Enter your age: "; cin >> age;
if (age >= 18)
{
cout << name << ", you are eligible for registration." << endl;
cout << "In other words, you can go to next section for registration!" << endl;
cout << endl << "***Thank you for visiting us!***" << endl;
return true; //Returning a constant value like 'true' is possible
}
else
{
cout << name << ", we are sorry that you are not eligible for registration!" << endl;
cout << "You can register after " << 18 - age << " years." << endl;
cout << endl << "***Thank you for visiting us!***" << endl;
return false;
}
return false; //Having a return at the end of the function without
//any additional flower brackets nesting is a very good habit.
}
int main()
{
cout << "Is that person eligible = " << checkRegistrationEligibility() << endl;
//Printing a temporary variable returned from the function after the process
}
The function stops when the return
keyword is called and throws it out of its scope to where it is called. Note the last line of the function. We use return
in order to make the compiler know that the function will return something in any case. Returning nothing or a variable of type other than boolean will result in an error!
I promise that the next post will be full of codes rather than long explanations. But for now, try to figure out why is it good to make and use as many functions as possible. I believe that the syntax of the function is very easy. Here is an exercise. Make a function which takes the user input of a number and then, squares it. The function then, let the other function (from which it is called, i.e main function) know the square of that number by returning it. Soon we are going to enter the arena of even more lovely things!
One response to “C++ Programming Series: Functions/Subroutines (Part 2)”
[…] is created! If you don’t know about the checkRegistrationEligibility() function, please visit this post […]