Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

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

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

In this post, we are going to discuss something called “Functions”. They are also called “Subroutines”. Like arrays, functions or subroutines are very significant, awesome and helpful. These are some of the basic things which we will keep using throughout our entire programming sessions.

The first thing to figure out is the definition of a function and a subroutine. Actually, both have slightly different meanings but are almost synonyms. A function is a group of steps performed on an input data and then, is received as an output (which is actually, the data formed by the modification of the input data).

A subroutine is a group of commands carried out in order. A subroutine may or may not have an input or an output.

Let us keep the definitions brief and step forward into the programming!

Every code that we type in, is some sort of command which then, runs or executes. In programming languages, a function or a subroutine is a block of codes which can have inputs and outputs.

Below are many lines of code which you may want to copy and will not bother typing it all by yourself.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    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;
    }
    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;
    }

    //For the sake of simplicity, just imagine that there are next sections for registration in the program...

    return 0;
}

This is a program which registers the person in some sort of a company or an institution. We have hard-coded the first part only. This whole procedure has to be done for many people. We can loop through it to recall all the codes. But it is still a mess in the main block.

I can’t type in all the code for the registration because it will make this post a mess. Just imagine, what if it is more than 1000 lines of codes, will you still prefer to read this post rather than going somewhere else for learning? This is pretty low-level.

From low-level codes, we mean that we are working with such codes which are extremely customizable yet, at the same time, extremely uncompressed and powerless! From powerless, I mean that you have to type three lines of code to just get one input from the user. And you repeat it for all the inputs you want from the user. And what I mean by power, is to have the whole codes that you see in the program above, to be executed by just one line. Compressing that much lines into a line sounds crazy! This is what we can call high-level.

From high-level codes, we mean that we are working with such codes which are barely customizable yet, at the same time, extremely compressed and powerful! Of course, by power, I mean something like replacing the set of commands,

“Hey, computer! Make a variable of window and make another variable of size then, make that window size equal to that size then, stretch the window gradually to that size then, put the window in the center and at last, force the window to be shown.”

with

“Computer, open up the window!”

It looks like I changed the direction of the topic to an entirely different dimension. Actually, this is a very important concept about the functions or subroutines.

The purpose of a function or a subroutine is to simplify our programming by converting our low-level codes into high-level codes. This can never be achieved through looping because there is much more to functions and subroutines besides just repeating the same codes.

Above the int main() or the main function,

void checkRegistrationEligibility() //make sure that the spellings are right
{
    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;
    }
    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;
    }
}

Now, we can have only one line, to have our eligibility registration program running fine!

int main()
{
    checkRegistrationEligibility(); //make sure that the spellings are right
    return 0;
}

We will discuss the function/subroutine syntax in the upcoming post! For now, just understand the concept of function, subroutines, low-level codes and high-level codes. My definitions are mine and of course, maybe inaccurate but they are the easiest definitions according to me. If you are interested in getting deeper concepts, here are some of the pages from Wikipedia:

Function (in terms of mathematics)
Subroutine
Low-level programming
High-level programming

One response to “C++ Programming Series: Functions/Subroutines (Part 1)”

Leave a Reply

Your email address will not be published. Required fields are marked *