The Code Butcher: A Playful Dissection of Functions

The Code Butcher: A Playful Dissection of Functions

Minced, Diced, and Deciphered with Laughter

A developer's nightmare: Your manager, calling you up in the middle of the night, and asking you to fix an issue in the app, written by that essay-writing co-developer of yours. Guess what! The d-day is just tomorrow!

Scrolling through lines and lines of code. How big can a bug be for an expert developer like you?

So, you've just fixed that app-breaking bug in the code. Confident, giving yourself a pat on the back and with a huge Colgate smile on your face, you tap that small triangle on the right corner of the screen, just opposite of the close button. The compiler starts to build the app and then your smile starts to invert to a huge frown, just realizing that the bug hasn't packed its bags and isn't ready to leave just yet.

The frown starts turning into a surprise and, soon, a frustration, as you're just staring at the pixel-perfect code (after all, who was the one who fixed the code...), just like how I stare at Sampath Balivada, and yet not find a single flaw.

How could God have not made a single mistake when designing Sampath Balivada ? How could one be so perfect and yet be soo.. knowledgeful?

After several hours of banging your head, donning the James Bond avatar, putting on those shades #LochanMathukumilli puts on... flexing that he's a famous investigative detective, and massaging that smooth touchpad your Mac comes with, you finally arrive at the blackspot.

Yup, there is a duplicate of the same block of code and you were changing the code in the wrong place. Just imagine the number of changes you'd have to make in those 'n' number of duplications of the same code. So, yeah, that's why functions exist to make our life a lot easier, stress-free and grumpy-free.

What's a Function?

A function is a self-contained block of statements that performs a coherent task of some kind.

Any program is a collection of one or more functions. If a program contains only one function, it must be main(). There is no limit on the number of functions that might be present in a program. Each function in a program is called in the sequence specified by the function calls in main(). After each function has done its thing, the control returns to main(). When main() runs out of statements and function calls, the program ends.

All functions enjoy a state of perfect equality. No precedence, no priorities, nobody is nobody's boss.

Any function can be called from any other function. Even main() can be called from other functions.

A function can be called any number of times. The order in which the functions are defined in a program and the order in which they get called need not necessarily be the same. However, it is advisable to define the functions in the same order in which they are called. This makes the program easier to understand.

A function cannot be defined in another function.

Trivia
A calling function is the function which calls another function, i.e, the called function. Arguments passed to a function are called actual arguments. Arguments received by a function are called formal arguments and are a copy of the actual arguments.

Conclusion

So don't try to cram the entire logic in one function. Instead, break a program into small units, write functions for each of these isolated subdivisions and make sure to rewrite the same code over and over again. Don't hesitate to write functions that are called only once.

If the operation of a program can be divided into separate activities, and each activity is placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand, and saves you from the wrath of your co-developers.

So that's functions, minced, diced and deciphered. Till I'm back with another excerpt, Keep on adventuring, See you on the developer trail.

💡
Although I present the contents of the book, Let Us C, authored by Yashavant Kanetkar, in a fun and thought-provoking way, I highly recommend you to read through the book once to get a sense of it. You can find the book here.