functionName();
. In the previous snippet we would be calling a function called functionName
which doesn't take any values to work properly (if it took any values - we'd put them in the brackets). A classic example of a function is the sqrt
function which is defined inside math.h
. We used this back in the basic mathematics tutorial, it simply square roots any value you pass to it. If we ever need to pass multiple values to a function, we separate them with a comma - so something like this: functionName(valueone, valuetwo, valuethree);
.With this under our belts - let's start learning how to create our very own functions that can execute any preset code for us. For this tutorial we are going to work on creating a function which can add two numbers together.int
in this case because the main function needs to be an int
to be the int main
that the compiler recognises as the program's entry point. The next thing (seperated from the type by a space) is the function's name - in this case it's main, and as I just alluded to, this is so the compiler recognises it as a point of entry. Next we have some empty brackets - these are here to illustrate that the function doesn't take any values, if we wanted a function to take values in (in the case of the function we want to create - we'll need to take two values so we can add them together) then we would put them inside the brackets. From here we simply have some curly brackets, inside which is the actual code we want the function to execute when called.return
. If a function is of type int
, it must return an integer (e.g. return 0
which in the case of the main function illustrates that the program exited without any errors). Values that functions return essentially go in the place where the function was originally called - so the sqrt
function returns the result of the square root operation (hence when we put it in a cout
or something, the square root value get's inserted at the point we called it). Function can have the type of any version of our function, let's just create a function which can output some basic text - let's stick with the classics and make it output 'Hello World'. So firstly let's decide on a type and name - void
is probably a good choice here as we can just use cout
in the function rather than making it return anything. For the name, I'm going to go with 'say_hello'. So to make the function, we simply write a definition (the structure of which we've already talked about) before the main function in the code. It's important that it's before, otherwise the compiler won't know what you're talking about when you reference the function in main
. The following would work fine for our simply say_hello
function (I've just used the basic structure we've already discussed and put a cout
inside):void
and then make it simply do the addition in the cout
- but we'll change this later and make it instead use return
. Now let's make it take in two integer values, as it requires these to do its job properly (how else would it add two numbers?) - we write these just like variable definitions apart from they are inside the function definition's brackets (and inside the prototype brackets too if you're using a function prototype). If you want the function to take more than one value (like we do), then you can separate the values with commas. I'm going to call the parameters that my function takes (the proper terminology for the values it takes), 'value_one', and 'value_two'. Our function definition at the moment should look something like this:cout
, do more mathematics to it, or pass it to another function. Our function's name is also a little bit misleading as the function doesn't actually add two integers, it outputs the addition of two integers. To fix this, let's just change the function type and use return. Since the addition of two integers should always result in an integer, we can use the int
function type. Now we've done this we must make the function return a value or the compiler will (should) complain. Let's go ahead and remove the cout
line currently in there as it's getting in the way, and replace it instead with a line that return
s the addition.main
so we actually use the value it returns somehow - I'm just going to multiply the addition of the two values by 2. The full source code should look something like the following:add_two_numbers ();
with no problem. It's important to remember that after the first default parameter in a function declaration (e.g. int value_one = 0
), all preceding parameters must also have default values assigned to them (and if you think about it, this makes sense).