6 – Functions

Functions are eerily similar to those of mathematics in that they provide a template for working with pieces of data and returns a result back to the context on which it was invoked. For instance, let’s say you have to do a given task more than once throughout your application. Instead of writing the same code over and over again, you can make a function that does the task and then just call the function whenever you need to perform it.

The Concept

Now that we are introducing functions, I need to clarify that we are moving from linear code into the realm of non-linear code. This means, that your code is no longer executed linearly. This may seem confusing to the beginner, so I will do my best to make the concept of non-linear code as clear as possible.

Code Blocks
As you saw in the ‘Syntax’ lesson, there were two curly braces, marking the beginning point of a code block with the { curly brace and the } curly brace marking its end point. These define what is called the scope of a code block — we will talk about scope later in this lesson. When we call a function, the execution moves to the code block of that particular function and at the end of the functions scope it returns execution back to where the function was called.

The Anatomy of a Function

There are 5 different elements that make up a function; a return type, function name, an argument list, a scope (a code block) and a return instruction.

Return Type
The return type declares what type of data the function will be returning (eg. an int). So in place of where the function was called, the data returned by the function would be placed at run-time, when the function has finished executing.

Function Name
This is pretty much self-explanatory, it is the friendly name we give it to represent our function, just like with variables.

Argument List
Arguments are data passed from one point in the code to another, so an argument list declares what data we need in our function for it to work. For instance, if you have a function that adds to numbers together, you would pass 2 numeric variables to the function via the argument list, have the function do its thing and then return the result — this is what makes functions so flexible.

As described earlier in this lesson, the scope defines a block of code. This is the code that will be executed by the function.

Return Instruction
When a function has done its job, it will need to return execution back to where it was invoked. Depending on what the function does, you may or may not want it to return data. For instance, if your function adds two numbers together which yields a result (eg. a + b = c), you would want to return that result. If your function does not need to return any data, we use the datatype void, in which case you do not need to include the return instruction in your function.

Declaration and Definition

Function declaration and definition are always done out of scope, this means that you cannot declare or define a function within another function. The difference between declaring and defining a function is that, a declaration simply tells the compiler it exists, where as the definition defines the function itself — as with variables, you cannot declare a function without defining it, however you can define it without declaring it, this simply declares and defines it in one go.

Declaring a Function
We declare a function by writing out its properties in the following manner: return_type function_name ( argument_1, argument_2 ); and it is important to declare a function before it is used — you cannot declare a function on line 8 if it is used on any line before it (eg. line 7).

Example 6.1
Declaring a function with a return-type of an int with the name ‘myFunction’ and it takes two arguments of datatype int, these arguments are called ‘a’ and ‘b’.

int myFunction ( int a, int b );


Defining a Function
The definition of a function is almost the same as the declaration, with the exception that we append the code block that goes with this function.

Example 6.2
Definition of the same function found in example 4.1. Here the difference of declaration and definition is made clear, the definition appends the scope of the function and defines what the function actually does internally. In this function, we see that it declares an int called ‘c’, calculates the sum of the data passed to the function and assigns the result to ‘c’, it then returns the variable ‘c’ — this is of course an over-simplified function, a function can do anything you would like it to do for you. If you have used functions in mathematics before, the equivalent math function would look something like: f(a, b) = a + b.

int myFunction ( int a, int b )
	// declare a local int, called ‘c’
	int c;
	// set ‘c’ equal to the result of a + b
	c = a + b;
	// return the value of c
	return c;

Notice that the declaration ends with a semi-colon, while the definition does not. Another thing to take note of at this point; doesn’t this look kind of familiar to you? Doesn’t this look a bit like the main function?

Calling a Function
We call a function by writing out its name followed by the arguments we want to pass in parentheses, as such: myFunction(3, 2);.

Do you see the flexibility of functions from looking at example 4.2? This really shows you for the first time how powerful variables are in programming. The variables ‘a’ and ‘b’ are set whenever you call the ‘myFunction’, and your function then operates on the data passed to it, returning the result.

.. to be continued

Previous: Loops
Next up: …


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s