Public Group

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

This topic is 2069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites

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

Share on other sites

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.

Share on other sites

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

Share on other sites

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?

Share on other sites

"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.

Share on other sites

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.

Share on other sites

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?

Share on other sites

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.

Share on other sites

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.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 18
• 19
• 11
• 9