C++ Programming Series: Functions/Subroutines (Part 3)

In the previous post, we have played with a function called checkRegistrationEligibility(). It was first, a function of return-type, void and now, bool. What is good with this, is the fact that this function is able to connect. From being connected, we mean that the data available in a limited scope of a function, can get available to that other function from which this function is called!

We can get the output from that function in the form of a boolean value. It is not necessary to collect the output of a function by having a specific variable for it. You can have a stupid line of code like 5+5; or true; without having any cout at the back! Sometimes, a function is of return-type int which returns a value 1, 0 or -1 only to indicate whether there is some error in the function or not and so, is not necessarily, taken into account.

In this post, we are not going to change even a bit of checkRegistrationEligibility() function. It is now, perfect. What we will do now, is to create another function for the next section of the registration program. But this time, we will create many small functions which will be grouped into more functions until a big function is created! If you don’t know about the checkRegistrationEligibility() function, please visit this post first!

Two very simple input functions which takes user input and then, return that input:

string enterAString()
  string str;
  getline(cin, str);
  return str;

int enterAnInt()
  int integer;
  cin >> integer; //Remember, getline() is for strings only!

  //the 'cin >>' causes rubbish in the input stream
  //which causes problems with getline()
  //the line below is a fix to it
  //it should be after every 'cin >>'

  return integer;

Another function which makes sure than the user said ‘Yes’, ‘yes’, ‘Y’ or ‘y’ for reasons that are going to be obvious in the other functions.

bool isDataSure()
  string yes_or_no = enterAString();

  if (yes_or_no == "yes" || yes_or_no == "Yes" ||
    yes_or_no == "y" || yes_or_no == "Y")
    return true;

  //Remember, function stops to go further immediately after the 'return' keyword
  //So, there is no need for 'else'
  return false;

If you don’t know or just forgot the complex if conditions or just confused about the condition as the above one, just go through this post.

Now, three more functions for getting father’s name, place of living and education.

string enterYourFatherName()
  string father = "NULL";
    cout << "Enter your father\'s name: ";

    //If 'father' is declared inside, we can't return it from outside
    father = enterAString();

    cout << "Is your father\'s name " << father << "?" << endl;

  } while (!isDataSure()); //Remember, semi-colon is necessary after do-while-statement

  cout << "Your father\'s name is " << father << "." << endl;
  return father;

string enterYourPlace()
  string place = "NULL";
    cout << "Enter your place of living: ";
    place = enterAString();
    cout << "Is your place of living " << place << "?" << endl;
  } while (!isDataSure());
  cout << "Your place of living is " << place << "." << endl;
  return place;

string enterYourEducation()
  string education = "NULL";
    cout << "Enter your education: ";
    education = enterAString();
    cout << "Is your education " << education << "?" << endl;
  } while (!isDataSure());
  cout << "Your education is " << education << "." << endl;
  return education;

Finally, a function to have all the three functions and the main function is all set!

void registerationSection()
  //To keep some distance from the first section
  cout << endl;

  //Discarding the returning strings for now...
  //It can be used for various purposes!

int main()
  //Instead of making a 'bool' variable and then, storing the value received
  //by checkRegistrationEligibility() method, how about using it directly?

  if (checkRegistrationEligibility() == true)

  //We can also use 'if (checkRegistrationEligibility())'
  //instead of 'if (checkRegistrationEligibility() == true)'
  return 0;

Here you go! All set for now accept for one thing which you will do yourself! The checkRegistrationEligibility() function requires some changes. Make sure to have enterAString() and enterAnInt() methods in it instead of the old regular methods. It is very important because we need cin.ignore() to ignore the leftovers in the input stream after cin >> to avoid problems with getline() method.

NOTE: The getline() is also a function from the standard C++ library. Same is the case for ignore() after cin. We will face many of standard C++ library functions!

After modifiying the checkRegistrationEligibility() function, it will run fine and you will be happy by the results for sure! It is easy to understand something like enterAString than the regular getline(cin,... method. Same is the case with isDataSure() which actually, gives us the hint that this function belongs to something like conforming the data to be right. In a nut shell, with the help of functions, we can make our own unique language which is understandable to everyone and not just programmers! This is something that we call as abstractions! Abstractions make things very easy. In programming, it is not simple to have even a very small task done with our basic English language. Computers don’t understand what we mean by what we say. But an approach like creating abstractions is possible. All the things we see are 0’s and 1’s which are later on, combined to form complex structures given complex names and so on. The binary can be used to define anything with reference to a name!

We defined our 0’s and 1’s into functions like enterAString(), isDataSure(), etc. The concept is to make abstractions and then, use them as much as possible to make even more abstractions. We can’t say our monitor’s screen consists of something like 0101001… but we can say it as ‘pixels’.

For now, this is it! I hope that this fulfills my promise which I talked about in the previous post and I think this much code is hard to absorb for a beginner! But don’t lose here… If code goes wrong just try, try again! Here’s a hint: See what can go wrong when we change the order of the functions! Or if you are already getting an error, see what can go right when we change the order of the functions!


One thought on “C++ Programming Series: Functions/Subroutines (Part 3)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s