[C++] Functions
Programming
|
DoxCoding.com
Hello again.
This tutorial will be nice and small. THings arent too hard to explain and understand. So i will get straight into it.
main() is a function. It is a special function to a C++ program because it is the first thing to be read and performed. Any other functions that are declared are run after or because they are being called.
so once again the basic template of our C++ programs. Can you still remember it?
#include <iostream>
using namespace std;
int main() //wow look a function!
{
}
Ok we need to change this alittle before we can start. The normaly template will now change slightly, (not for all the rest of the tutorials just this one!).
Ok. So. Lets change it to what is needed.
#include <iostream>
using namespace std;
void function()
{
}
int main()
{
}
Well that is know done. I bet you are thinking to yourself what the hell does that mean.
I will explain both types of function, void and int.
first off with void.
"void" it is when it returns nothing. A function that is used to print a message has nothing to return and would be declared as a void.
"int" is when it returns and int. Logical hay? for example at the end of main() we type return 0;. We are returning an int.
So... Moving on. I think it is time for us to actually type something in our two functions we have now got.
#include <iostream>
using namespace std;
void function()
{
cout<<"void function";
}
int main()
{
cout<<"main function";
function();
return 0;
}
So i guess you can basicly see what has happened there but for those real newbies i will explain into more depth.
What has happened is the main function, (main() ) is being executed. Because we have already said in void function to print "void function" we can just type in the name of the function. 'function();'.
If you are thinking what is the point. You will come across long pieces of code, that will simple need to use functions. Also as good coders say. If you use the loop: and goto loop; it is bad coding habbits. Instead of these you can use while loops or you can even use functions.
It is worth knowing that in some other programs they call functions a different name. Other programs use method meaning the same term as function. So don't be off putting if you sometimes see this around when learning other programming languages or if anyone uses it while teaching you C++.
Ok so that is it on Functions.
If i have missed something out or you want to add in something please PM me.
Stay TUNED!
-[O]
Please login to rate coding articles.
Click here to register a free account with us.
|
|
Comments
|
| Please login to post comments. |
|
|
 |
Categories |
 |
|
|