Function

We briefly introduced function as a building block to C programming, let’s take a closer look at it, as we will use it for almost all the programs we write.

We already know that program is a collection of statements, so why do we need function at all? Or, maybe we need just the main function itself, why do we ever need to write our own function?

We need function for code reusing,

– function is a good way of reusing common code, so we do not need to write the same thing over and over

– function gives us the power to use someone else’s code as well, even if the code is from someone we do not know

– function allows multiple developers to work together, each developer can work on one part of the program

Let’s look at a simple example, the following program prints the last digit of a few numbers,

Basically we do a modular division of 10, then print the value, and we are doing the same thing 4 times. How about we write a function to print the last digit of any number, then just call that function?

The above program can be rewritten the following way,

As introduced before, function takes the following format,

<return_value_type> <function_name> ( parameters) { instructions; }

The printLastDigit function we wrote above takes only one parameter, the ‘number’ we want to print the last digit of. Each time when we call that function, we pass in a different number for the function to print.

In above example, we only print the last digit, once we pass in the number, that is all we need that function to do. We do not expect anything returned from it, that is why the return type is ‘void’.

Let’s change the requirement a little bit, after the last digit is printed, we also return it, so the caller can do something with it. Let’s write the program that prints the last digit of a few numbers, then we sum up all the last digits, and print the last digit of the sum value.

We can modify the above program to the following,

The ‘getAndPrintLastDigit’ function returns the last digit after the printing. At line #11 we initialize the sum as zero, then we add each return value to it, after line #16, we have the sum of all the last digits of the 4 numbers we have above.

At line #18, we print the last digit of sum itself, and ignore the return value, since we do not have any further use of it.

Note a few things in above program,

– when calling the function, we can pass in either a constant value, for example line #13 through #16, or pass in a variable as we do in line #18.

– when the function is called, the parameter will have the value we pass in. For example, the ‘number’ parameter variable will have the value of 3456 when line #15 is calling the function. The ‘number’ parameter variable will have the value of sum when line #18 is calling the function.

– Each time the function is called, the caller must supply the parameter, but can choose to ignore the return value. For example, at line #18, we call the function but do not collect the return value.

If you wonder what the ‘static’ keyword does in above example, it is a way of saying that the function we wrote can only be called from within the same file. So if our program has more than one files, the static function can not be called from any other files. Why we add that limit? we will learn more on that later, but it is enough to know for now that it is a good practice to use ‘static’ keyword for any local function we write.

We now have a new weapon to simplify our code and get more done …

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">