Don't leave your functions without calling them

Don't leave your functions without calling them

Some details on how functions work in programming languages

Before we begin let me tell you a story, buckle up

There once was a man who wanted to work in construction

he walked around and reached a junction,

from there he met a citizen

who introduced his cousin,

who was an employer named Euler

and was looking for an officer,

Euler told the man to be an Analyst

but the next day the man was absent,

Euler called the man to start the job

and he said, "Ok, I will do the job",

he did the job and showed the results to Euler

Euler said "Well done, now hand it over to whoever",

and the results were then published

by the accountant.

Well, that was fun!. What does this story have to do with functions? In the story, there was a man who was looking for a job and eventually got the job, but the key point in the story is when he got the job, but he didn't report back the next day, and then his employer had to call him and tell him to do the job, only then did he do it and returned back the results. So if you already know some programming fundamentals then you might've realized by now that, "Oh, that's exactly how functions work!". Yes, that's right, it is almost exactly like how a function works in programming languages.

One of the most common mistakes programmers (if not all, at least beginners) make is that they define a function and don't call it, and say "Why is the code not working???". I'm guilty of it too, it just seems reasonable, like "I did write all the code needed for it to work, now why isn't it working?" and I look through the code and be like "Oh, shit I forgot to call it" LOL.

This is the concept that I am trying to indicate with the story. If you assign a person to a job post, he's probably not gonna start working right away, you need to tell him, when is he supposed to do it. Similarly, if you just define a function and just keep it there doesn't imply that the code inside the function is gonna run. You gotta call the function with whatever name you assigned it with and whenever that function is required.

And now moving to the next point, that is not returning any values. Sometimes the function is defined, and it probably works fine, and you did call it, but I'm not getting any feedback, and that is probably because the function is not returning any data. If you don't return any value then it's gonna return a default value of nothing like NoneType in Python or undefined in JavaScript and null in others. Whatever value is returned by the function is then read by the compiler/interpreter instead of the function name and parenthesis. I'll show you an example (the code is in Python):

def foo(): # Defining a function.
    return 5 # which returns the value of  5 .

a = foo() + 4 # declaring a new variable  a .
print(a, foo()) # print both  a  and the function  foo() .

this is then converted (for explanation purposes) by the compiler/interpreter to something like this:

def foo():
   return 5

a = 5 + 4 # And then just  a = 9 .
print(9, 5)

You can see that all the foo()s have been now replaced with the value that is returned. So, returning a value is important while defining a function.

Conclusion

  • If you defined a function, don't leave it there. CALL IT
  • and also return something so that someone can use the value and do some other calculations with it or just publish the results, as the Accountant did in the story.

Hope this was a useful article. If you liked it please leave a comment and if you think others will find this useful then share it.

❤️✌️