Greetings! In the last lesson, you learn how to jump backwards in the code and break out of loops, along with a few other things. Well, now I will teach you how to create functions in your code.
BTW: You better be excited! I am displaying my amazing artistic talent again in this lesson.:::What is a Function?:::Now, a function is a part in the program that is accessed by the main function (or less often, another function). It allows you to use the full potential that structured programming gives you. A function is declared pretty much the same way as the main function. It has a certain format.
[You must be registered and logged in to see this image.]The first part is the identifier, or type. It defines what type of function it is. The identifier defines what type of data it will return. You remember the return statement right?
- Code:
-
return 0;
Yeah, that one! We are going to start using it again. That is beside the point though. If your function is an 'int' then your function will ONLY return an 'int'. If your function is a 'char' then your function will ONLY return a 'char'. Seems pretty simple right? Well, it is.
The second part of the format, is the name. This is the easiest part. I hope you remember back to the Variables and Data Types lesson. We learned how to name your variables. Well, your functions' names follow the same rules. Just to refresh your memory:
- Quote :
- You can name your variable anything (as long as it doesn't correspond with any keywords used in the C++ language; no spaces are allowed; neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid.).
The third part the parameters. Basically, it is just pulling variables into your function from an outside source. You put the names and identifiers of variables into the parameters area and the variables, and their current values in the program, are carried into the function. You will see more later.
The fourth, and final, part is the statements. The statements are simply, what happens inside your function. You use the same stuff that you would use in the main() function.
Now, let's get some examples up in here.
EX1:::
The most classic example is the calculator.
- Code:
-
#include <iostream>
using namespace std;
int add(int,int); //this line is the prototype
int add(int a, int b)
{
int r = a + b;
return r;
}
int main()
{
int result;
int a;
int b;
cout << "Please enter a number: ";
cin >> a;
cout << "Please enter another number: ";
cin >> b;
result = add(a,b); // this line calls on the function called add()
cout << "Your answer is: " << result;
return 0;
}
Notice the line near the top of the program. It has a comment next to it. That is the prototype. The prototype is the statement that declares that you are making a secondary function. You can still use the function without the prototype, but you are very limited on how you can use it. All you have to do to write it is put the matching identifier and name. In the parameters section, you just have to put the matching data types in the correct order.
Okay! I think I need to expand on the idea of the parameters. If you understand the parameters and how they are used in this code and you think you can create a program with them, then skip the section of this tutorial that is colored cyan.
:::PARAMETERS:::
[You must be registered and logged in to see this image.]
As you can see in the above image. The parameters and the arguments have a very crystal clear correspondence to each other. The section where it says 'int a' corresponds to '5' just like 'int b' corresponds to '3'.
[You must be registered and logged in to see this image.]
In the above image, you can see the cycle that is happening between those two statements. The 5 and 3 that the person entered obviously add up to be equal to 8. Well, the function then returns the value 8 and since they said variable 'z' is equal to the return value of addition(), z is now equal to 8.
[You must be registered and logged in to see this image.]
Here I took both pictures and stuck them on top of each other.
If this doesn't make sense. Please email me. I WILL help you to the best of my ability.WARNING: IF YOU CAN'T HANDLE MORE ADVANCED STUFF TURN BACK NOW!!!! Just Kidding! It's not that hard.:::Overloading Functions:::In this language of which you are learning, you can declare functions with the same name. The only thing that allows you to do that is the number of parameters you have and the type of parameters.
- Code:
-
// overloaded function
#include <iostream>
using namespace std;
int operate(int,int);
int operate(float,float);
int operate (int a, int b)
{
return (a*b);
}
float operate (float a, float b)
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}
Notice the two functions named operate(). One of them has two 'integer' parameters while the other one has 2 'double' parameters. The compiler knows which one to call because of the numbers passed as arguments. If two integers are passed then it will used the first one. If two double numbers are passed then it will use the second one. Again, if you don't get it, email me.
You try: 1) That game you made earlier. Add some functions into it for different things, such as a health meter, or power meter.
NOTE: No video for this lesson has been posted.