As You Like It

22.04.2016 |

Episode #8 of the course Getting started with C programming by Sudeshna Das

 

In today’s lesson, we will learn about functions. In a program, a function is a group of statements that perform a specific operation.

So far, in each and every C program, we started with a void main().

If you look carefully, you’ll notice that the statements contained within main() perform a specific operation. So, going by the definition of a function, main() is also a function.

Notice the brackets next to main. Where else have you seen round brackets just next to a keyword?

Yes, that’s right! printf() is another such example. Well, printf() is a function as well. But there’s a difference between main() and printf(). Can you guess what it is?

We don’t know what the statements that make up printf() are, but we do know what the statements within main() are. This is because the contents of printf() are stored within the stdio.h that we include in every program. It is what we call a pre-defined function.

You can create your own functions as well. These are called user-defined functions. Let us create a function that will find out the square of a number—that is, the number multiplied by itself.

 

#include<stdio.h>

void square();

void main()
{
int num=5;
square(num);
}

void square(int num)
{
int result;
result = num * num;
printf(“The result is %i”, result);
}

http://codepad.org/QLUCPxB7

 

The line void square(); tells the computer that we are about to work with a function called square(). We don’t do this with the main(), but we need to write this line for every function that we build on our own; otherwise, the computer will not be able to recognize our function.

The line square(num); means that we are sending the value of num to the function square().

We write the contents of square() in a manner similar to the way we write the contents of main(). The int num inside the round brackets next to void square means that square() has to accept an int (integer) value in the variable called num.

But we have already declared a variable called num in main(), you say. Why do we have to do this again? Well, that’s because whatever you do inside main() stays inside main(). The moment you are outside main(), the values that you had worked with are lost, unless you specifically send a value to another function, which is, in our case, square().

So, the num variable has no existence outside main(). The num variable in square() is a new variable that is being assigned the same value as the num of main(). You can use a different name for the num variable of square() and your program will work just as well: http://codepad.org/I7d4HIJ6.

What square() does essentially is multiply the given number by itself and display the final answer using yet another function, which is printf(). So, we see that a function can call another function to do its bidding.

In tomorrow’s lesson, we will learn more about functions. Till then, happy coding!

 

Recommended book

“Head First C” by David Griffiths

 

Share with friends