functions!!!! ahhhhh!!!

i have several c++ books and i am on functions and arguments and i am finding it really hard to understand. I feel like giving up but i really want to make it in the games industry.
What is it that you find hard to understand? Give a more specific example, please.
Don't give up!
I used to feel like giving up whenever I came across something difficult, but you realize that sticking with it is better then dropping it and quitting.
As for Functions, im going to try to summarize it.

I'm going to give you an example program, a simple calculator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main ()
{
float v1, v2; char v3;
while (1)
{
cout <<"\n\n\n";
cin >>v1>>v3>>v2;
if (v3 == '-')  cout << v1 - v2;
if (v3 == 'x' || v3 == '*')  cout << v1*v2;
if (v3 == '+' )  cout <<v1+ v2;
if (v3 == '/' ) cout <<v1/v2;
}
return 0;
}


In this program, you simple enter in an expression like 6x6 or 9/3 or 7943/384.5, ect.
And it calculates it for you. (it doesn't do modulus for simplicity)
In this program, main (); is a function.
The beginning of any program always starts in main ()
The type of function is int
It returns 0

Functions hold statements, code, for the program to execute.
Take a look at this one:

1
2
3
4
5
6
7

int main ()
{
cout <<"Hello World\n";
return 0;
}


-The name of the function is main ()
-The type is int
-outputs "Hello World"

The type of the function is int, integer. this means that the function returns and integer.
The name of the function is main and its right after the type of function it is.
The arguments of a function are the things in the parenthesis ()
the body of a function is all the statements and Everything inside brackets {}
the return statement is the type of variable it returns.



@Blue Shell
INDENT YOUR CODE!!!

Do you realize how hard that is to read when you have numerous nested code blocks? Let me show you what the difference between yours and properly indented code looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Unindented
void vp::list<T>::sort() {
if (size() <= 1)
return;

for (int merges = 0, k = 1; merges != 1; k *= 2) {
node *p = firstNode, *q = p;

for (int qsize = merges = 0; p; merges ++, p = q) {
for (qsize = 0; qsize < k && q; qsize ++, q = q->next);

while (qsize && q) {
if (q->value <= p->value) {
if (q->next) {
q = q->next;
q->prev->insert_before(p);
}
else {
q->insert_before(p);
q = NULL;
lastNode = lastNode->prev;
}

if (p == firstNode)
firstNode = firstNode->prev;

qsize --;
}
else
p = p->next;
}
}
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Indented
void vp::list<T>::sort() {
   if (size() <= 1)
      return;

   for (int merges = 0, k = 1; merges != 1; k *= 2) {
      node *p = firstNode, *q = p;

      for (int qsize = merges = 0; p; merges ++, p = q) {
         for (qsize = 0; qsize < k && q; qsize ++, q = q->next);

         while (qsize && q) {
            if (q->value <= p->value) {
               if (q->next) {
                  q = q->next;
                  q->prev->insert_before(p);
               }
               else {
                  q->insert_before(p);
                  q = NULL;
                  lastNode = lastNode->prev;
               }

               if (p == firstNode)
                  firstNode = firstNode->prev;

               qsize --;
            }
            else
               p = p->next;
         }
      }
   }
}


It makes a world of difference.

@sam dhillon
I know a lot of stuff can be discouraging when it comes to programming, and don't get me wrong, it's not an easy ride. There is tons of challenges and a lot of headaches and stress. But once you figure it out and it just "clicks" the reward is amazing. Stick with it and don't give up.

In regards to functions, the idea is simple, it's a block of code that can be called no matter where your program is (C++ is a linear language, i.e. reads from the top to the bottom). The nifty thing about functions, however, is that it contain information to do a certain task, let's say getting user input. Well, later on down the road, you want to make the user enter input for a different program. Instead of rewriting code to do the same thing, you can just code that function into your new program and you're one step closer to being done with that one. It also makes it easier to go back and modify it slightly so that, let's say your math was wrong on calculating an average, you change it in one place and it affects everytime that function is called.

That is pretty easy to grasp, so let's move on to the arguments/parameters. Essentially they're the same. A function has parameters, but you call a function with arguments (Confused yet? It gets better). These are pretty nifty as well. It allows one block of code to essentially communicate with another block of code. I also like to image functions as kids in a class room and parameters as them passing notes around. Once you can visualize that, the concept of parameters starts to make sense.

Now let's look at the two different types of parameters, by value and by reference. By value is rather simple. Your function, the one that "calls" the other function, takes it arguments and makes a copy of them (this is done elsewhere, but figuratively speaking). These copies are passed to the function under a parameter name so that you can use these values. Changing these values does nothing to the originals, only the ones that exist in that function. Once the function is done, it returns back to you and your variables never changed (again, there are exceptions, but that's typical).

By reference is a little difference. Instead of generating a copy, you allow the function to essentially take a hold of your variable (by passing a reference to the memory location...boring stuff). Whatever the function does to that variable, directly reflects on the variable in your function. This can be really handy for doing some kind of math on a variable, getting user input, etc. You'll find out when you need to use one or the other. Just keep your head up and ask questions if you're unsure. Don't give up, and above all else, keep your goal in front of you at all times.
Topic archived. No new replies allowed.