Functions Explained
Functions are fundamental to programming in C++ and most other languages.Let's look at this example:
Now here, we can take the function apart and look at each of it's components:
The Return Type:
The "int" at the beginning, denotes the return type of the function. The return type is what type of value is returned upon completion of the function. In a function where nothing is returned, you are just simply calling a function to perform a procedure then the return type is "void" which you will see a lot. In the case of this function it is "int" which refers to a 32 bit integer or whole number (no decimal place). For a function with a return type of anything but "void" you must add a return statement which is the "return 0;" line in the above image.
The Return Statement:
The line "return 0;" is the return statement of this particular function.
* Remember "void" functions have no return value therefore no return statement is needed. However you can use "return;" to exit the function prematurely for whatever reason, be it an error or whatever *.
The syntax of the return statement being "return" followed by the value you wish to return, note the value must be the same type as the declared return type in your function which in this case is int; "0" all ended by a semi colon like all other statements in c++ programming.
* You can use return types in functions for good use, like using a statement to assign a variable its value e.g. "int result = add(1, 2);"... this function would add 1 and 2 and return the result and store it in the integer "result". *
Which brings us onto parameters.
Parameters
When writing a function you can have parameters. A parameter is a value you pass into a function to be used in that function. So if, from the example above, you wanted to write a function to add two int variables together then the function would need two parameters, one for each value like so; "int add(int a, int b)". These two parameters can be used in the function as local variables which can only be accessed where they are passed. Carrying on with the add function example we can use the variables to perform the addition and then return the value:
The Return Type:
The "int" at the beginning, denotes the return type of the function. The return type is what type of value is returned upon completion of the function. In a function where nothing is returned, you are just simply calling a function to perform a procedure then the return type is "void" which you will see a lot. In the case of this function it is "int" which refers to a 32 bit integer or whole number (no decimal place). For a function with a return type of anything but "void" you must add a return statement which is the "return 0;" line in the above image.
The Return Statement:
The line "return 0;" is the return statement of this particular function.
* Remember "void" functions have no return value therefore no return statement is needed. However you can use "return;" to exit the function prematurely for whatever reason, be it an error or whatever *.
The syntax of the return statement being "return" followed by the value you wish to return, note the value must be the same type as the declared return type in your function which in this case is int; "0" all ended by a semi colon like all other statements in c++ programming.
* You can use return types in functions for good use, like using a statement to assign a variable its value e.g. "int result = add(1, 2);"... this function would add 1 and 2 and return the result and store it in the integer "result". *
Which brings us onto parameters.
Parameters
When writing a function you can have parameters. A parameter is a value you pass into a function to be used in that function. So if, from the example above, you wanted to write a function to add two int variables together then the function would need two parameters, one for each value like so; "int add(int a, int b)". These two parameters can be used in the function as local variables which can only be accessed where they are passed. Carrying on with the add function example we can use the variables to perform the addition and then return the value:
int add(int a, int b)
{ return a + b; } |
This would be the shortest way to write this function, passing the addition straight into the return statement.
You can have as many parameters as you want in a function, but it can get ridiculous to have too many. Simply seperate each parameter with a comma.
Bonus Tip: You can have parameters which are assigned default values. If you are creating a function which could possibly have an "optional" parameter then this can come in handy. These must be last in the list of parameters you designate and you assign the default value by adding " = 0" or some other value in place of "0". Let's edit the "add" function to utilise this:
You can have as many parameters as you want in a function, but it can get ridiculous to have too many. Simply seperate each parameter with a comma.
Bonus Tip: You can have parameters which are assigned default values. If you are creating a function which could possibly have an "optional" parameter then this can come in handy. These must be last in the list of parameters you designate and you assign the default value by adding " = 0" or some other value in place of "0". Let's edit the "add" function to utilise this:
int add(int a, int b, int c = 0)
{ return a + b + c; } |
As you can see we now have a 3rd parameter called "c" which is assigned the default value of "0". We can still call this method like so - add(1, 2); - because if we leave out the third value if will simply assign it the value "0" and the calculation "a + b + c" returns the same result as "a + b", no harm done. However, it leaves us open to use another variable should we want to like so - add(1, 2, 3); - which would of course return the value of "a + b + c" again like before, equalling "6" of course.
"Insert Code Here"
Finally, a function is fairly useless without any actual function. You can have if statements with multiple return statements if needs be (say different results caused need for different return calculation or result). Between the two curly braces "{ }" is where you "insert code here" to give it function like so:
"Insert Code Here"
Finally, a function is fairly useless without any actual function. You can have if statements with multiple return statements if needs be (say different results caused need for different return calculation or result). Between the two curly braces "{ }" is where you "insert code here" to give it function like so:
int add(int a, int b, int c = 0)
{ int result = a + b + c; return result; } |
As you can see I added the line "int result = a + b + c;" to show adding code to give our function function.
This concludes our lesson on functions. Go back in your browser to return to where you were before if you came here from the tutorial, or navigate using the drop down at the top of the page.