Writing Functions
In the previous section we looked at functions that have already written. Now we are going to write our own function. In a sense, you’ve already been writing them. Remember when I told you that it didn’t matter if you used int main or void main? Well main is a function. We write functions because it helps to break up the code into sections. We also write them so that we can just keep going back sending the information to them, instead of typing it again, so it cuts down on overhead. If every function has a specific job, and no more than one, then it is a lot easier to figure out what is going on. This is especially useful if more than one person is working on the program. The first thing you need to know about functions is that they need to have a data type. Whether it be int, string, double, etc. whichever type of value is returning, has to be that data type. For functions not returning one value then you’ll want to use a void function. Either way, you’ll want to declare the function before you use it before main, and if you choose, then put the actual function after main. The second most important thing to know about functions are parameters. Remember that parameters are what is passed to the function. Well, there are two ways to pass parameters to funtions, by value and by reference. When dealing with parameters, the variables passed down do not have to equal the same name as the variable in the function, they just need to be the same .
When we pass parameters down by value, what it means is that we’re passing down the actual value. Passing a variable down by value means that we’re giving up the right to change the variable in that function. While we may change in that function, when we go back to the main function, the values will be the originals. This program will take a number from a user, any number, and then call the function, reverse, which will then reverse the numbers using the modulous dvision and a loop.
#include <iostream> using namespace std; long reverse(long); void main() { long numin, newnum; cout<<“Enter a number”<<endl; cin>>numin; newnum = reverse(numin); cout<<“\nThe number backwards is “<<newnum<<endl; system(“PAUSE”); } long reverse(long num) { long reversenum=0, digit; while(num != 0) { digit=num%10; num/=10; reversenum *= 10; reversenum += digit; } return reversenum; } |
Variables are all stored in memory and have a specific memory address when we create them, so when we pass parameters by reference, we’re passing the memory address. When we change the value in a function of a reference variable, then the value in the calling function is then changed. To differenciate between by value and by reference in C++ we use the & sign. We put it after the datatype on the declaration of the function, and in the parameters section of the function itself. There’s only one exception to this, arrays. Unless you pass down a specific index in an array, then passing down the whole array will be passed by reference. For this however, you do not need an ampersand. When passing arrays, you don’t even need to tell it how many there are, empty square brackets are fine, but you must remember that when calling the function you just send in the name of the array, but not any square brackets. The following example will send an array to the sort function and then sort the array and send back the highest.
#include <iostream> #include <string> using namespace std; const int numofarray = 11; //Any variables outside the bounds of functions are global void sort(int [], int&); void sort(int x[], int& high) |
Recursive functions are functions that call themselves. If they call themselves too many times without returning, the program will crash. Overloaded functions are functions with the same name just different perameter types or different number of parameters. This is done often because you may want to send an int, a double, or a long to the function, you don’t need to check which one it is, just write the same function a few times.