Jump to content

passing parameters by references trouble

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Hey guys i got this sample problem that is supposed to help for the midterm, and i just wanted to know if im going about this right.

Quote

Write a function called Square that will take as arguments an integer and a double. With the use of references your function should return the square of both values. Test the function in main by asking the user to input an integer and a double. When the Square2 function returns print out the square of both values.

and this is my code right now. For some reason i dont think it is right, i dont think i "passed the parameters by reference"

any help will be repped.
i sent this problem to methodz|reborn in a pm, but i know he is a busy busy guy! but maybe he can still help


(another member here helped me on this part of the code)
#include <cmath>

#include <iostream>


using namespace std;


void Square(int &a, double &b)



int a;

double b;


int main()

{

cout <<"please enter an integer to be squared" << endl;

cin >> a;

cout << " please enter a number with a decimal to be squared" << endl;

cin >> b;


Square(a, b);


cout << "Square a = " << a*a << endl;

cout << "Square b = " << b*b << endl; 


return 0;

}
i think its wrong because you shouldnt have to cout anything for an answer i think. i dont know!
thanks for your help!!

#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Yes.. This is wrong. :) I'll fix it, unless MeTh0Dz does it first...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
dang it. everyone else said this was a stupid simple problem with no twists or tuns, just straight and basic. yet i'm so lost.

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Some stuff to chew on...
#include <iostream>

void Square(int &a, double &b)
{
   a *= a;
   b *= b;
}

int main()
{
   for (int i = 1; i <= 10; ++i)
   {
      int x = i;
      double y = i / 2.0;
      std::cout << x << ",";
      std::cout << y << ":";
      [B]Square(x,y); // "return" squared values[/B]
      std::cout << x << ",";
      std::cout << y << "\n";
   }
}


#5
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Here...


// My solution

#include <iostream>


using namespace std;


void Square(float & a, int & b);


int main() {

	float a = 0;

	int b = 0;

	

	cout << "Enter a floating point integer: ";

	cin >> a;

	

	cout << "Enter an integer: ";

	cin >> b;

	

	Square(a, b);

	

	cout << a << endl << b;

	

	return 0;

}


void Square(float & a, int & b) {

	a *= a;

	b *= b;

}



#6
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
thank you so much for the help!

is there a donate button for the forum? once this class is over i think the forum deserves some payback from me

#7
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Errors/improvements marked with red..

#include <cmath>
#include <iostream>

using namespace std;

void Square(int &a, double &b)[COLOR=Red];[/COLOR] [COLOR=Red]// Semicolon after function declaration [/COLOR]

int main()
{
[COLOR=Red]// You better put them variables inside main :)[/COLOR]
int a;
double b;

cout <<"please enter an integer to be squared" << endl;
cin >> a;
[COLOR=Red]// I would put some error checking here, but OK, we'll leave that for now[/COLOR]
cout << " please enter a number with a decimal to be squared" << endl;
cin >> b;
[COLOR=Red]// Same here..[/COLOR]

Square(a, b); [COLOR=Red]// That is just fine, no problem
[/COLOR] 
cout << "Square a = " << a << endl; [COLOR=Red]//  Don't do a*a, then you are actually calculating OUTSIDE of your function.. 
// Your assignment wants you to do that inside... So just DISPLAY a, Square function will modify it to become a*a.[/COLOR]

cout << "Square b = " << b << endl;[COLOR=Red] // Same here, just display the variable, mod it in the function.. What else would the function do?![/COLOR]

return 0;
}

[COLOR=Red]// You need to make a definition for Square();[/COLOR]
void Square(int &a, double &b)
{
   a = a*a; [COLOR=Red]// The function now modifies a [/COLOR]
   b = b*b; [COLOR=Red]// and b.. And all you'll have to do, is display it :)[/COLOR]
}
Ok, you need to understand parameter passing... Unless we're talking about arrays, arguments in C++ are passed by value by default. That means, your integers and strings and whatever arguments are copied, their values are literally copied into other variables for the function. That is good, because that means your function won't change your variables outside itself, like important global variables etc. You only gives the function permission to read data this way, your passed variables are still the same. If the function is gonna do something, it will then be through return values. Example:

int add(int a, int b)
{
 return (a + b);
}
See that? This function takes it by value. No way it can change whatever variable you put into a or b. It just returns a value, that's the only way it has to give you data.

Look at this:
void mess(int a)
{
 a = 15;
}
What will this do if you say this:
int main(int argc, char **argv)
{
  int x = 10;
  mess(x);

  // Display x here....

  return 0;
}
?
Nothing. It has no power over it's argument at all. X is still 10, although mess() changed it's input to 15. That is because mess() changed another variable, where the content of x was copied to. That variable contained the value 10, mess() changed it to 15, nothing happens in main().

You got that? So it's references. Now, your program does not pass your arguments by value anymore, the are not copied. Instead, their memory addresses are passed to the function, which means that the function will directly modify the argument.
If we now change mess() to this:

void mess(int &a)
{
 a = 15;
}
Look, it takes a reference. That means, whatever variable you put into mess(), that evil dude is gonna change it to 15. So in main, x would no longer be 10, it would actually be 15. So whenever you use functions with references, you actually modifies the variables passed to it.

Examples from the real world:
Look at a function like sqrt() (square root). It takes a number and returns a number. It does not modify the number it takes! It does not pass by reference (or if it does, it's const-reference, you'll come to that later :D)! That way, you can safely pass it any variable, knowing nothing bad will happen to it without your permission.
You have to do:
x = sqrt(x);
to modify x.

So another example, with references... This is more common in C, because there you would like to return an error value, while using pointers (they don't have references in C) to pass by reference and modify. An example of this is sprintf(), which takes a "buffer" argument it fills. How the heck can it modify your variable like that?! References...

Here's the function declaration of sprintf.. Notice the *buffer which can be modified by the function.
int sprintf( char *buffer, const char *format, ... ); 
You might want to look up this function for more about it..

I hope you got that, I'm no teacher.. :D Just ask if there is anything, I can repeat myself in some other way or something :D
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#8
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
genius..... that actually made me understand it 10000x better then how my professor did. will you be my teacher... lol

#9
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
lol, I don't think so, but I'm glad it helped ;) Look at what the other guys did too, their code also illustrates the point
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa