A function is a section of code that has some separate functionality or does something that will be done over and over again.
As a basic example, you are writing code to print out the first 5 squares of numbers, then the first 5 cubes, then the next 5 squares again. We could write it like this:
for(i=1; i <= 5; i++)
{
printf("%d ", i*i);
}
for(i=1; i <= 5; i++)
{
printf("%d ", i*i*i);
}
for(i=1; i <= 5; i++)
{
printf("%d ", i*i);
}
We have to write the same loop twice. We may want to somehow put this code in a separate place and simply jump to this code when we want to use it.
This is what precisely functions are for.
A function is like a black box. It takes in input, does something with it, then spits out an answer.
Note that a function may not take any inputs at all, or it may not return anything at all. In the above example, if we were to make a function of that loop, we may not need any inputs, and we aren't returning anything at all (Text output doesn't count - when we speak of returning we mean to say meaningful data that the program can use).
We have some terminology to refer to functions:
* A function, call it f, that uses another function g, is said to call g. For example, f calls g to print the squares of ten numbers.
* A function's inputs are known as its arguments
* A function that wants to give f back some data that g calculated is said to return that data. For example, g returns the sum of its arguments.
int square(int x)
{
int square_of_x;
square_of_x = x * x;
return square_of_x;
}
To understand how to write such a function like this, it may help to look at what this function does as a whole. It takes in an int, x, and squares it, storing it in the variable square_of_x. Now this value is returned.
The first int at the beginning of the function declaration is the type of data that the function returns. In this case when we square an integer we get an integer, and we are returning this integer, and so we write int as the return type.
Next is the name of the function. It is good practice to use meaningful and descriptive names for functions you may write. It may help to name the function after what it is written to do. In this case we name the function "square", because that's what it does - it squares a number.
Next is the function's first and only argument, an int, which will be referred to in the function as x. This is the function's input.
In between the braces is the actual guts of the function. It declares an integer variable called square_of_x that will be used to hold the value of the square of x. Note that the variable square_of_x can only be used within this function, and not outside. We'll learn more about this sort of thing later, and we will see that this property is very useful.
We then assign x multiplied by x, or x squared, to the variable square_of_x, which is what this function is all about. Following this is a return statement. We want to return the value of the square of x, so we must say that this function returns the contents of the variable square_of_x.
Answered by
Nagendra
, an ibibo Master,
at
6:34 PM on September 19, 2008