Why a function is necessary?

story of a car:
 One day a man name Joe, started creating a car. Joe was working in a garage for the past 5 years of his life. so this is not a big task for him. He started building up from parts as he knew that one can not build it just as a whole.So he decided to make all the parts first and then will join all the parts to make the car in whole. First he built the Sheats. Next he built GearBox.Next he built Stearing and so on. In the end he got all the parts working separately and perfectly.Now he decided to put all the things together. So he started putting them together, one after the other at their respective places. In the end he got a working car. When he started driving the new build car, suddenly he realized that he had forgot to put brakes in it. Any how he managed to stop the car.Now started building the "Brake tool box".Once done, he put the "Brake tool box" inside the car. Now everything works perfect. But wihoout "Fuel" car does not run. So he put fuel inside the fuel tank and started the car. Now he can run his properly fully built car.
So the moral here is if we got a problem, we must analyse it and break it into pieces.Now try to solve these pieces separately. If you solved all the part problems, the the whole problem will automatically be solved.
Ok, what is that to take with my "Function" ? Functions are like part problems, if you can solves them then whole of your code will be working fine. Generally in a Object Orented Programming, a problem is solved not by a single long code, but by a lot of small code fragments.This also increases possibility of code reuse. If Joe had built his gear box properly, he can fit that into a new bus, he might be creating next time.As far as the functionality of the GearBox is proper, he can fit it on any vehicle. the same thing happens to our functiona too.If we write a function and its working properly, then we can put that function in any other project we might be doing with no change at all.Thats code reuse.
Now, lets think about the Fuel tank.Unless fuel is there in fuel tank, the engine can not produce energy to drive the car. The same is true for some of our function. Functions can take some value (fuel) and return some value (energy) too.
A nicely written "function" can be put into any project with a very little chnage in the original code or sometimes without even changing the original project code. Suppose Joe wanted to make his car air-conditioned, then he would go ahead and built an air-conditioner or buy one and put it into his car. Thats the way a new function is added to an existing program.
Thats a general idea of "Function".


Author: saumya

designer / developer / maker / educator @saumya

1 thought on “Why a function is necessary?”

  1. Thats perfectly understood, now lets proceed further on the scripting.
    Wats next..? Am waiting to learn more..

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s