PDA

View Full Version : (C++) Calling Functions



TheEliteOne
04-23-2010, 03:45 PM
Tut by TheEliteOne

Calling Functions in C++

Lets say we want a title, like this:



TheEliteOne's Example!


at the top of the screen, but we don't want to type in cout<<"TheEliteOne's Example!\n\n"; every time. We can use a function and call it every time we want to. Like this:



#include <iostream.h>
void title()
{
cout<<"TheEliteOne's Example!\n\n";
}
void pause()
{
system("pause");
}
void cls()
{
system("cls");
}
int main()
{
First:
cls(); //Calls are cls function.
title(); //Calls are title function.
cout<<"See.. not that hard right?\n\n";
pause(); //Calls are pause function.
goto First;
}



I don't know about but I would rather type in some thing like "title();" than "cout<<TITLE OF YOUR PROGRAM\n\n"; " every time I wanted to ;) hope it helped.

SonniE
04-23-2010, 06:48 PM
Not bad, ill add to it:
The main purpose of methods is to shorten code so if a block of code keeps appearing in your program create a method for it and just call the method as needed. Also in OOP they are used for accessing/manipulating data in an object.

TheEliteOne
04-23-2010, 11:59 PM
Cool, thanks.