Coder Profile - Show off your skills, get a coder profile.
 
 
 
[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]


Posted By otoom
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
Page 1 of 1
More Articles By This Author
Quick Threading Tutorial - C++
First look at C
[PHP] How to make a log file.
[VB] Hello World
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Recently Posted "Programming" Articles
[PHP] - Lets kill the complex IF
Java: Obtaining User Input - Part 1
Basic PortScanner in VB6.0
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
Recently Rated "Programming" Articles
[PHP] - Lets kill the complex IF
Java: Obtaining User Input - Part 1
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Networking (1)
Operating Systems (4)
Other (2)
Programming (51)
Security (10)
Software Development (5)
Standards (1)
Web Development (15)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
What would you rate the design of Coder Profile?

9 to 10
7 to 8
5 to 6
3 to 4
1 to 2


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
5 Days Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved