New to C++ - Creating functions

Let me just start this off by saying that I am pretty new to c++/ game development. I decided to learn C++ to get an idea of what game development is about to see if I wanted to persue a career in games.

I have a book that I am reading in order to learn C++. So far I haven't had any problems picking up on it but I cannot seem to get the hang of creating functions (with or without arguments)

First of all: What is a function?
What is the purpose of a function?

Also, In this book I noticed that the functions all have different names
ex: double sumSquareSequence(void)
but they never explain how these are named. Are they the same as declaring variables where you can name them whatever you want?

I'm pretty confused on this and any advise would be really appreciated.
Functions make using repeated blocks of code easier. Think of it as a 'shorthand' method of 'inserting' code. For instance:

1
2
3
4
5
6
7
double convertToFahrenheit(double c)
{
  double result;
  result = (c * 1.8) + 32.0;

  return result;
}


Now, I realize that this is a very trivial example, but it makes the point. Anywhere you need to use code to convert a Celsius temperature to Fahrenheit, you can substitute this function in its place:

 
double f = convertToFahrenheit(26.0);


Most of the time (but not always) a function will contain many lines of code that would take a lot of work to type out over and over again. If you substitute a function in its place, you can complete your task much faster with fewer mistakes. And yes, you can name them whatever you want (as long as you don't name your function the same as a reserved word).
Last edited on
Cplusplus.com has a great C++ tutorial in the Documentation section. You can read it online or download it as a pdf.

double sumSquareSequence(void)

C++ is a strongly typed language, meaning it needs to know what type of object it is dealing with (in order to compile the most efficient program possible).

double
Is a double precision floating point number, 8bytes long usually (depending on processor), representing a number within the range of +/- 1.7e +/- 308 (~15 digits), as opposed to an
int
Which also is a number, but of a different type. It can only hold an integer value (no decimal points) and it actually has two subtypes: signed (eg. +500 or -500) and unsigned (+500 only).

Anyway, your best bet is to go to the above mentioned tutorial-- it will get you started quickly.
A function pretty much does whatever you want it to. It can be called on to do that thing or things you assign it to as many times as you want. An example is:

Say I'm making a blackjack program. I want to create a function that will do something simple like show the player how much money they have. Here is an example of a function that would do that.

By the way, you probably already know this, but just in case, if you have two forward slashes, it will be considered a comment by the program, and won't affect it.

//This is the function declaration. This is where we say that the function is there. The parentheses //appear because inside them are parameters. That's pretty much what the function is going to be //using. Void is there because the function doesn't return anything, or take it and store it. You //don't alway have to have parameters like that; you can just leave them empty. The only thing //that matters is the first word, which could be int(integer), double(a number with decimals), //char(single characters), and string(a string of characters) or some other stuff. I don't know them //all.
void show_playermoney()
{
//Cout is just used to display stuff.
cout << "You have ";
//blah is a variable. You can call it "money" if you want. Probably nobody except you really cares. //But you can only use "blah" or whatever, if you declare it. This can happen just inside the bracket //under void show_playermoney(). It'll look like : string money; and that's it.
cout << blah;
cout << " dollars."
}

Then, later in your program you could call that function.

//This is an if statement. If you don't already know about them, you probably will soon, or should //soon. They are pretty simple and easy to learn.
if(in.find("money")!=string::npos)
{
//Here you don't need the "void" because that is only necessary in the function definition. But as //you see, we can just put it in and that will call the function and it will run.
show_playermoney();
}



Hopefully that wasn't too confusing. I hope it helped.
Ok. (sorry if this is wrong). But are you saying that basically a function stores a block of code so that later on you don't have to re-type the entire block of code again but instead, just use the created function?
Topic archived. No new replies allowed.