A Pocket Guide To Programming Core

The Specialists

We assumed and use a function ‘printLabel’ in our previous chapter. We have also seen some other functions in other chapters. e.g. echo, print, cout, etc.

I call these function ‘The Specialists’. Let me tell you why. Let’s first discuss what is a function?

A function is a piece of code or set of instructions that perform a specific task.

Okay. But, why I call these ‘The Specialists’. Because, each function is used to perform a specific task. Let’s think of surgeons. We have neurosurgeons, plastic surgeons, orthopedic surgeons, etc. Why we call them differently depending upon type of surgery they perform? Because, they are specialists of that surgery. They have studied and practice that type of surgery for so many years.

That’s why I call programming functions ‘The Specialists’. Because, each function has been programmed to do one specific task. Yes, we can have multiple functions doing same task. And it is also true for surgeons. Right!

Here is how a function look like in most of the languages.

{function} {return value} {functionName} ({input parameters})
{ // Start of function body
     
     {Function Logic}
     
     {Return statement}
     
} // End of function body

Some programming languages support ‘function’ keyword to declare functions. For example PHP and older version of JavaScript. Python use ‘def’ for function declaration. And C++, Java and some C type languages don’t need any keyword to declare function.

Some functions do return a value. For example, a string join or concatenate function will return a concatenated string, or an arithmetic square function should return a number, square of input parameter. There are many more. Java and C++ want to use keyword ‘void’ in case your function is not returning anything. In case a function is returning a value than we use variable type keyword to specify type of returned value. E.g. String, int, etc.

Each function should have a name. And camelCase notation should be followed to name functions as well.

Each function accept zero or more input variables. We call these ‘parameters’.

Each function have a body. Body consist of one or more programming statements, including variables, conditional statements, loops and some time call to another function.

Than functions which return a value should have a return statement. Most languages support ‘return’ keyword that indicate return logic.

Let me tell you something. There is no such ‘printLabel’ function in any programming language. Or I haven’t seen or used any. Let’s create our own custom function to complete our program from last chapter. And I will also move our Loop to our custom function for better understanding. Our function will just print label on computer screen (Let’s keep it simple for now) and our function will return ‘success’ on completion.

// Here is how our function will look
// in Java.
String printLabel (int start,
          int quantity,
          string shipmentDate) {

    for (start;
         start <= quantity;
         start++) {
    
         string label = "P" + shipmentDate +
                     "-" + start;

         System.out.println(label);
    }
    return 'success';
}

// We will call this by
// one line of code.
String result = printLabel (1, 100, "20200202");

There are hundreds of functions available in all programming languages. We call these native or built-in functions. You should refer to programming language of your choice for better understanding of these.

Let’s move on to next chapter and learn about “The Collections”. Please click ‘Next’ to continue.

Image placeholder

Hi! I'm Zeeshan Elahi

I have compiled and prepared this content with love for people like me and you. And as you know it always take some time and extra cups of coffee to read, compile and prepare a course or manual. If you have like reading this content and want to say thanks, please consider supporting from more stuff like this.

Leave a comment

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

*

*