• Popular Now

• 9
• 9
• 11
• 12
• 9

Archived

This topic is now archived and is closed to further replies.

Total Newbie: Passing Variables to Functions

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

Recommended Posts

Hey all, I''m really struggling on the concept of passing variables to functions. I mean, I understand it and everything, but I don''t understand PASSING the data BACK... For Example: void CreateBoard( int Board[][3] ); // Function and I pass data to it ala: int Board[3][3]; // Board CreateBoard( Board ); // Passing it Does that data get passed BACK? I don''t know, I''m really struggling here... I make everything a global variable so I can edit it in any function, but I simply just don''t understand why I can''t figure out how to avoid doing that. Am I starting off with a Tic Tac Toe game not a good thing? Should I be doing something else? Also, how can you pass like classes or structures to a function? Is it even possible? I''m so lost, please try to clarify the best that you can, anything is better than nothing!
Estauns

Share on other sites
You mean having the function return a value?

Instead of setting the function''s return as void (returning nothing), set it to the data type you want to return.

Example:

  int ITakeStuff(int stuff) //You pass stff to this function{ return stuff - 3; //You get back 3 less than what you passed}... inside main or somewhere ...int MyStuff = 6;int MyNewStuff = ITakeStuff(MyStuff);

MyStuff was passed to ITakeStuff. ITakeStuff returned (or passed back) MyStuff - 3 to MyNewStuff. MyNewStuff will equal 3.

Share on other sites
Easy. Just do the following

  void SetValue(int &Value){ Value=85;}

That''s called a reference parameter. It sends the address of the variable allowing you to change it. So in functions instead of something like

void SetValue(int);

it would be

void SetValue(int&);

get it?

A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."

Share on other sites
Thats fine and dandy...

But what would I do with a 2D array? I mean, like...

TicTacToeBoard[3][3] = CreateBoard( TicTacToeBoard[][3] );

What would the prototype of the function be, so that all the elements in the array become 1, lets say... I don''t understand how a function can manipulate an array, I guess is what I should have said..

I don''t even know how to explain it thats how confused I am about it.. I guess thats what I want, I want my elements to be able to be edited, all of ''em or 1 at a time...

UGH!
I hate this...
*sighs*

I guess I''ll figure it out, cuz I can''t even explain what I mean.

Thanks.

Estauns

Share on other sites
Just put an & symbol after the data type. I believe it is the same for arrays.

A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."

Share on other sites
Here''s the solution I''ve came up with! If you have any questions. Please ask me.

  void CreateBoard(int**);int main(){ int ** TicTacToeBoard; TicTacToeBoard = new int*[3]; for (int iCpt = 0; iCpt < 3; iCpt++) TicTacToeBoard[iCpt] = new int; CreateBoard(TicTacToeBoard); for (iCpt = 0; iCpt < 3 ; iCpt++) { for (int iCpt2 = 0 ; iCpt2 < 3 ; iCpt2++) cout << TicTacToeBoard[iCpt][iCpt2] << " "; cout << endl; } return 1;}void CreateBoard(int ** iVectBoardTmp){ for (int iCpt = 0; iCpt < 3 ; iCpt++) for (int iCpt2 = 0 ; iCpt2 < 3 ; iCpt2++) iVectBoardTmp[iCpt][iCpt2] = 1;}

Share on other sites
Hey there!

C/C++ treats all arrays passed to a function as reference parameters. Simply by passing an array to a function like any other variable, any changes you make will stay. For example...

void main()
{
int thisArray[3][3];
thisArray[1][3] = 1;
ChangeArray(thisArray);
}

void ChangeArray(int Array[][3])
{
Array[1][3] = 2;
}

In this example, thisArray[1][3] would be 2, not 1, since it was changed in the function. In order to make sure arrays are not changed in a function, you can declare them as consts. Hope this helps!

Edited by - CrystalFire on November 21, 2001 8:54:14 PM

Share on other sites

Let''s say you pass an integer in a function called CalculateStuff

Here''s the function
  void CalculateStuff(int iwhatever){ iwhatever = 15267;}

Here''s the main
  int iMyInteger = 190;CalculateStuff(iMyInteger);

That would do absolutely NOTHING because the program create a COPY of iMyInteger and use the COPY in CalculateStuff

BUT, if your function CalculateStuff take an ARRAY of pointer as a parameter.

Like that
  CalculateStuff(int iwhatever[]){ iwhatever[2] = 12345;}

And pass an array in it.
  int iMyFuzzyArray[30];CalculateStuff(iMyFuzzyArray);

Now, since you are passing an ARRAY it will do something (set the 3rd element in your array to 12345).

But why? Because the program do not make a COPY of the entire array, it just pass the address of this array. That''s why you can modify it.

iArray[5] is the same as *iArray
iMultiArray[5][5] is the same as **iMultiArray

Share on other sites
In lots of programs that I''ve seen, programmers use one-dimensional arrays to represent two-dimensional arrays. If you were making a chessboard, for example, you would have something like this:

  const int num_x = 8;const int num_y = 8;int* chessboard = new int[num_x * num_y];

If you want to find a coordinate like (0, 3) or (4, 7) on the board, you would just do this:

  // if you were trying to get (3, 2)int pos32 = chessboard[2 * num_x + 3];

I think this way is faster because you''re not doing as much indexing or something. Maybe someone else could explain it better.