In programming languages, functions are one of those things that sound complicated or abstract. Yet, they can easily be explained in simple terms, and with comprehensible examples.
Let’s say you want to write a program that involves a lot of calculations with 2D points, like calculating the distance between two points. You could just write the formula for Euclidean distance in place, every time you want to calculate the distance. However, you could also write the formula only once and “wrap” that formula a bit, so that you can call that segment of code every time you want to use the distance formula. Since we want a piece of code that will work for every two points, and not just specific points, we need special syntax that will enable us to call the function we’ve written when it’s needed.
This syntax is almost identical in most widely used languages such as C or Java. Since all of our examples have been written in C so far, we’ll also present the syntax for writing and calling functions in C. For simplicity’s sake, the example we’ll be using is quite trivial—a function that adds two numbers.
int Add (int a, int b)
return (a + b);
Now, let’s explain what this means.
Add is the name of the function. The int before the name means that the function’s return type is an integer (return type i.e. the type the result will be). Integers a and b are called parameters of the function, and the return (a + b) statement means that the result of the function will be a + b.
It’s important to realize that a and b parameters represent any two integers for which you want to call the function.
Note: if your function doesn’t return anything, you will write the keyword void instead of the return type.
Here’s how you actually call the function:
int aa = 5;
int bb = 3;
result = Add (aa, bb);
We declared and defined two variables, aa and bb—note that the names do not have to be the same as the names of the parameters in our function definition. Then, we used these variables as arguments of our Add function. Its important to differentiate between the terms “parameter” and “argument”: parameters are only related to function definition, and when you call the function, you are passing arguments for parameters of the function. Since the function returns an integer, we can assign that integer to our result variable, since their types are the same.
Now that you know the syntax for writing and calling a function, you can write whatever function you want; here are a few more examples:
Note: everything written between /* and */ signs is a comment; it will be ignored by the compiler.
You can also comment a line, by writing // at the beginning of the line, but the first way is more in the style of C.
/*a function that writes the first n numbers onto the console*/
void WriteNumbers (int n)
for (int i=0; i<n; ++i)
printf(“%d ”, i);
/*a function that returns 1 if the number is dividable by 3, and 0 if it isn’t*/
int IsDivisable (int num)
if (num%3 == 0)
Readers of our series may notice that we have used new constructs here, which will be explained next week in video demonstration. So stay tuned!