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.
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Recently Posted "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
My Experiences So Far with Languages
Selecting Your First Programming Language
[PHP] - Lets kill the complex IF
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
Recently Rated "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
[PHP] - Lets kill the complex IF
Actionscript API
Actionscript Events
My Experiences So Far with Languages
Selecting Your First Programming Language
Vectors in C++
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Hardware (1)
Internet & Web Sites (3)
Life In General (2)
Networking (1)
Operating Systems (4)
Other (2)
Programming (46)
Security (8)
Software Development (5)
Web Development (14)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
Which sounds better on your coder profiles, and makes more sense to you (think twitter, facebook, etc)?

Followers / Following
Fans / Following
Fans / Fan Of


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