Chapter 6
User Defined Functions
In this chapter I will teach you about functions. Functions are a way to reuse code without having to type it over and over again which is good right? Every console application you make will have at least one function and it is call main. Main is the entry point too, for all console programs. Functions take a while to get used to so lets get started now. First things first is the function skeleton which looks like this:
Code:
return_type function_name(formal_parameters)
{
statements;
return _something;
} This seems pretty simple. But that is all about to change. Formal parameters are really just variable declarations. You pass information to be copied into these newly created variables in the function call by using actual parameters. A formal parameter can consist of any variable type(s) and as many variables as you want. The actual parameters are usually the same amount (don't ask, i think its called overloading a function) and exist in the function call. Now the return statement needs some explaining too, because it is one of the main reasons you have functions (except for void functions which I will discuss later). The return statement is used to give the result of function back to the main function. The return statement causes the current function to terminate and gives control back to the function Main. A good example of a user defined function is the function that finds the absolute value of a number. This example is as follows:
Code:
int absol_val(int num_1)
{
if(num_1 >= 0)
{
return num_1;
}
else
{
num_1 *= (-1);
return num_1;
}
} Now lets break this down piece by piece. It starts out by declaring that the function absol_val will return an int, no exceptions. It then checks to see whether or not num_1 is below zero or not. If it is not it just returns the number that was copied to it back to main (It is important that you remember that is only a copy and nothing else). If is is below zero, it makes it positive, by saying take num_1, multiply it by (-1) and assign the value to num_1 (in a compact way) all in the statement num_1 *= (-1), and then returns the positive value to the function Main. So no matter what, absol_val will be returning a positive value. now lets talk about function calls and actual parameters. The syntax and an example all rolled into one for the function call to the previous example would be as follows:
Code:
int number;
int num_abs;
cin >> number;
num_abs = absol_val(number);
Think of absol_val as a mathematical equation in which it says the value returned by copying the variable number into the function absol_val where it takes on the name of num_1, then it calculates and returns is assigned into the variable num_abs. That is the basic process as I understand and see it. Now have you ever forgotten to declare a variable but still tried using it. The compiler gets mad and says undeclared identifier! Well functions are like complicated variables. When the compiler sees that you are trying to use the function absol_val but there isn't an absol_val function until later (compilers are like people, in that they don't skip around reading a book, but instead read from left to right, top to bottom) so to the compiler, the function absol_val does not even exist! well this is a problem because I hate putting big ugly functions near the top but instead like to keep things nice and neat. The remedy? Use a function prototype. Its like a little stubbier version of the actual function. The syntax for the absol_val function prototype is:
Code:
int absol_val(int);
It's that simple! You don't even have to say what the variable name will be. So lets put it all together int one C++ program and check it out before we move on to the next part of functions.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int absol_val(int);
int main()
{
int number;
int num_abs;
cout << "Enter in the value whose absolute value will be calculated: ";
cin >> number;
num_abs = absol_val(number);
cout << "The absolute value of " << number << " is " << num_abs << endl;
cin.get();
return 0;
}
int absol_val(int num_1)
{
if(num_1 >= 0)
{
return num_1;
}
else
{
num_1 *= (-1);
return num_1;
}
} Now then, that's as simple as I could get it and it still be useful. It declares variables, asks for input, sends a copy of said input to absol_val, checks for its position in relation to zero, returns a positive value, then outputs the value. Pretty simple like I said. Now lets move on to void functions. Void functions main purposes are for output usually. Void functions do not have a return type or a return statement. They are devoid of a value returning ability of any kind. BUT...you can still pass values to the function. The way this works without a return is that it gets to that bottom bracket or final semicolon (not sure which) and then goes to where it left off in the function that made a call to it. It pretty much leaves a bookmark of sorts and then picks right back up where it left off. The syntax for a void function is:
Code:
void function_name(formal_parameters)
{
stuff;
} No return type needed. The return type in the function header is just replaced by the keyword void. MOVING ON! What if you don't want to pass a copy to a function but instead want to have a way to have multiple values returned have a value changed by the function instead ooooorrrr you don't want to declare it as a global variable (which is just a variable declared outside of a function and can be seen by anyone and used by any function)? You can do this by passing a reference parameter. A reference parameter is just basically a hexadecimal number that is assigned to a memory location in RAM and it says what ever is in this spot, this is the value I want. Instead of passing a copy, it passes the memories address in RAM and then changes the value by using that address. It's actually part of a later topic called pointers in which the hex number is stored as a variable and that hex number "points" to a variable location. But I digress. The syntax for using a reference parameter is as follows:
Code:
return_type_or_void function_name(var_type &var_name)
{
stuff;
} And the function call:
Code:
function_name(&var_name);
The use of the ampersand changes the variable from a copy to the same variable. Of course you can still use different names in the different functions for the variables to keep from confusion but it is all good. Well that is all for this chapter. Just try to make a few programs using this knowledge and if you have any questions ask Checker or |G3| lol jk just ask me and I will do my best to explain it. Until the next chapter guiz, cya around in spamzville or sumthin!
~Arachnid