• Create Account

## starting to get serious about programming, but hitting some bumps in the road

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1double O seven  Members

232
Like
0Likes
Like

Posted 17 February 2014 - 03:57 PM

First let me say how good it feels to be around an online community of skilled developers.  I feel like I can get some real guidance here.

Long story short, I'm trying c++ again because I'm waiting for epic to release UE4.  Apparently they are doing away with Unrealscript and replacing it with straight c++.  It was confirmed in some article I was reading and the author goes on to say something like "Start learning some c++ and prepare yourself.  Trust me, its not as hard as you think".

So I decided to give it another go.  And this time I'm having a little more success because im not really thinking about games.  Just trying to learn it.

Anywho, Im stuck on function parameters.   This youtuber guy Bucky (Thenewboston) explains it ok, but I feel like he's leaving out some valuable information.

So I searched around a little more and found this guy

All his code worked, then when I started mixing other stuff with it like other statements and functions it still worked.  The only time I was getting errors was when I screwed up and didnt realize it.

QUESTION...

The way I understand it function parameters are just functions that require more data to run.

int FuncTwo(int G);          //  This is the prototype

int main ()

{

int F = 5;

int result;

result = FuncTwo(F);

cout << "The result is "  << result << endl;

return 0;

}

int FuncTwo(int G)

{

G = G + 5;

return G;

}

Am i correct on this............

result = FuncTwo(F);

FuncTwo(F)  is doing 3 things.  First its holding a value of 5, hence variable F to take with it to the function FuncTwo.  While there it calculates that with whatever equation is in that function.  Then return G tells it to bring it back to main.  But it would already do that anyways in c++, but since the variable "result" is in front of the function call its automatically telling that function that whatever the result will be to store it in that variable when it comes back?

This stuff i getting really tricky.  But I gratefully appreciate any help.  thanks everybody.

Edited by double O seven, 17 February 2014 - 04:03 PM.

### #2KoMaXX  Members

603
Like
0Likes
Like

Posted 17 February 2014 - 04:27 PM

Sorry, I'm in a hurry right now, so no long answer yet...

What got you confused now (I think) is the concept of Call-By-Reference and Call-By-Value.

When doing Call-By-Reference the function operates on the original data, in Call-By-Value it is essentially copied (to the stack) and the copy is given to the function (your case) so the original value is not changed. So: G is not the same as F, it just starts out with the same value.

Edited by KoMaXX, 17 February 2014 - 04:29 PM.

Go on, feed your brain: http://poroba.com/flip/flipz.php

### #3mark ds  Members

1739
Like
0Likes
Like

Posted 17 February 2014 - 04:50 PM

Consider this:

int Max( int a, int b )
{
if( a > b )
{
return a;
}
else
{
return b;
}
}

int main()
{
int num1 = 10;
int num2 = 15;
int num3 = Max( num1, num2 );

cout << "The result is "  << num3 << endl;
}

Note how the values of the variables num1 and num2 are passed into the function Max(). Max() then calculates whichever is greatest, and returns the correct answer to num3.

### #4Lactose  GDNet+

9653
Like
0Likes
Like

Posted 17 February 2014 - 04:59 PM

Also, consider this...

#include <iostream>
using namespace std;

int increaseBy2(int a)
{
a = a + 2;
return a;
}

int main()
{
int num1 = 5;
int num2 = increaseBy2(num1);

cout << "num2 is " << num2 << endl;
cout << "num1 was 5, is now " << num1 << "." << endl;

return 0;
}


Note how num1 does not change, due to it being pass-by-value.

Edited by CoreLactose, 17 February 2014 - 04:59 PM.

Hello to all my stalkers.

### #5double O seven  Members

232
Like
0Likes
Like

Posted 17 February 2014 - 05:03 PM

Consider this:

int Max( int a, int b )
{
if( a > b )
{
return a;
}
else
{
return b;
}
}

int main()
{
int num1 = 10;
int num2 = 15;
int num3 = Max( num1, num2 );

cout << "The result is "  << num3 << endl;
}

Note how the values of the variables num1 and num2 are passed into the function Max(). Max() then calculates whichever is greatest, and returns the correct answer to num3.

right thats what I thought it was.  By reference means you create a variable and assign it a value, then use the variable as the part that gets passed to the next function.
But by value means you just stick an actual number in the function call to be passed right?

### #6mark ds  Members

1739
Like
0Likes
Like

Posted 17 February 2014 - 05:05 PM

"But by value means you just stick an actual number in the function call to be passed right?"

Yes - the actual numbers are passed into the function - NOT the variables. So effectively you're calling Max( 10, 15 ), and the function returns 15.

### #7Buckeye  GDNet+

10739
Like
0Likes
Like

Posted 17 February 2014 - 05:09 PM

int num3 = Max( num1, num2 );

@mark ds: Be careful. If you intended the "max()" defined in windef.h, that's a macro, not a function.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #8double O seven  Members

232
Like
0Likes
Like

Posted 17 February 2014 - 05:10 PM

Also, consider this...

#include <iostream>
using namespace std;

int increaseBy2(int a)
{
a = a + 2;
return a;
}

int main()
{
int num1 = 5;
int num2 = increaseBy2(num1);

cout << "num2 is " << num2 << endl;
cout << "num1 was 5, is now " << num1 << "." << endl;

return 0;
}


Note how num1 does not change, due to it being pass-by-value.

Not to sound noob but whats going on with  int num2 = increaseBy2(num1);

I dont think I have studied that yet.  What does (num1); mean?

### #9Lactose  GDNet+

9653
Like
0Likes
Like

Posted 17 February 2014 - 05:13 PM

Not to sound noob but whats going on with  int num2 = increaseBy2(num1);
I dont think I have studied that yet.  What does (num1); mean?

num1 is a integer variable created earlier, set to equal 5.

Hello to all my stalkers.

### #10double O seven  Members

232
Like
0Likes
Like

Posted 17 February 2014 - 05:19 PM

Not to sound noob but whats going on with  int num2 = increaseBy2(num1);
I dont think I have studied that yet.  What does (num1); mean?

num1 is a integer variable created earlier, set to equal 5.

lol oh ok.  I get it.  Sorry.  Still learning.

I feel like an idiot now.

### #11Ryan_001  Prime Members

3010
Like
3Likes
Like

Posted 17 February 2014 - 11:31 PM

Not to sound noob but whats going on with  int num2 = increaseBy2(num1);
I dont think I have studied that yet.  What does (num1); mean?

num1 is a integer variable created earlier, set to equal 5.

lol oh ok.  I get it.  Sorry.  Still learning.

I feel like an idiot now.

That's a good sign, it means you're learning   Stick with it, even if you don't quite understand it, it'll start to sink in with time.  The best thing is, after getting a tutorial or example to work, is to modify it.  Try to break it, but try to break it in a way that you can predict.  Add a few more functions.  Call functions from other functions.  Just play around, have fun for a bit, and get comfortable with it.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.