943,903 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 611
  • C++ RSS
Nov 28th, 2008
0

what is a reference parameter and how would i go about this code?

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. float leg1, leg2, hyp;
  9. cout << "Enter one leg ";
  10. cin >> leg1;
  11. cout << "Enter one leg ";
  12. cin >> leg2;
  13.  
  14. if ((leg1 < 0) || (leg2 < 0))
  15. hyp = 0;
  16.  
  17. else
  18. hyp = (sqrt((float)leg1*leg1 + (float)leg2*leg2));
  19.  
  20.  
  21. cout << "The hypotenuse is " << hyp << endl;
  22.  
  23.  
  24. return 0;
  25. }

i think i may have posted that code correctly...sorry if i didn't. anyway, i have to change this code i made for part of my homework so that it doesn't output anything to the screen or returns anything as a return value but returns the hypotenuse in a reference parameter. i don't know what that means. can someone please help me?
Last edited by knharp3; Nov 28th, 2008 at 11:41 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knharp3 is offline Offline
2 posts
since Nov 2008
Nov 28th, 2008
0

Re: what is a reference parameter and how would i go about this code?

Hmm, parameters are typically a part of a method's call argument(s). I'm assuming the assignment requires you to create a method that has a reference parameter and returns the result through the reference parameter, and not through a return value from the function call?

Correct me if I'm wrong.

-Alex
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 28th, 2008
0

Re: what is a reference parameter and how would i go about this code?

you are correct but i'm not sure what a reference parameter is or how to incorporate it into my code. can you give me an example or something. i'm very computer programming illiterate and hardly understand half of what is going on in my class. i'm only in it because it is a required course to take as a math major.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knharp3 is offline Offline
2 posts
since Nov 2008
Nov 29th, 2008
0

Re: what is a reference parameter and how would i go about this code?

You'd be surprised how much programming and math are related! =)

In fact, before I wanted to program I pursued a math major with more of a background in creative writing. I don't know how but I managed to discover programming and realized it gave me the best of both worlds, so now I devote my time to studying Programming!

Anyways, back to your initial problem. Before explaining how the trick is done, it might be more useful to explain what is going on in your program.

Whenever you declare a storage type with an identifier (or anonymously without an identifier). without using new, you are using memory on the stack to store data that your program is working with.

Because your data types exist somewhere in memory, they must be referenceable or have some means of being located and manipulated. That is the idea of a reference - a memory address.

So when you pass something in a method by reference, you are passing a handle to the actual type that sits on top of the address.

Let's look at the following example--

c++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3.  
  4. using std::cout;
  5. using std::endl;
  6. using std::cin;
  7.  
  8. void make5(int& ref){
  9.  
  10. ref = 5;
  11.  
  12. }
  13.  
  14. int main(){
  15.  
  16. int x = 9; // x exists on the stack, and is constructed with value 9
  17. cout << x << endl; // printing out x
  18. make5(x); // passing x into the method by reference
  19. cout << x << endl; // x should be 5 now
  20.  
  21. cin.get();
  22. return 0;
  23. }

--notice that x is passed to the method by reference. The address of the object itself is passed and then it is assigned with the value of 5, then x is printed again. Notice that there is no direct assignment to x itself, rather the method simply did as it was instructed and assigned a value to whatever int reference was passed to it (it could have been anything, y even (as an int) ).

Your assignment needs to have the same logic. You must have a referenceable type passed into the method, then assigned the result.
Last edited by Alex Edwards; Nov 29th, 2008 at 12:06 am.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 29th, 2008
0

Re: what is a reference parameter and how would i go about this code?

1. The make5 is not a method, it's an ordinar function. Strictly speaking, no "method" term in C++. It's an informal alias for non-static member function.
2. Semantically a reference in C++ is another name of an object allocated in the memory (not only on the stack - moreover, no "stack" term in the C++ Standard except "call stack" and "stack" as STL class). A memory address is an idea of a pointer, not a reference. A reference "idea" is "another name of". A reference type variable looks like an ordinar variable but it's an "imperceptible" variable: a reference dereferenced in every language constructs and denotes referenced object. It's impossible to declare unitialized reference: another name of what object? It's impossible to declare reference of reference: what's "name of name"?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 29th, 2008
0

Re: what is a reference parameter and how would i go about this code?

Click to Expand / Collapse  Quote originally posted by ArkM ...
1. The make5 is not a method, it's an ordinar function. Strictly speaking, no "method" term in C++. It's an informal alias for non-static member function.
2. Semantically a reference in C++ is another name of an object allocated in the memory (not only on the stack - moreover, no "stack" term in the C++ Standard except "call stack" and "stack" as STL class). A memory address is an idea of a pointer, not a reference. A reference "idea" is "another name of". A reference type variable looks like an ordinar variable but it's an "imperceptible" variable: a reference dereferenced in every language constructs and denotes referenced object. It's impossible to declare unitialized reference: another name of what object? It's impossible to declare reference of reference: what's "name of name"?
1) That's me intermingling Java and C++. There's nothing more to say there.

2) Nothing I disagree with, but I tried making a simpler example for the OP to follow.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 29th, 2008
1

Re: what is a reference parameter and how would i go about this code?

Quote ...
2) Nothing I disagree with, but I tried making a simpler example for the OP to follow.
Yes, I understand. But let's look dangers in the face : a novice can confuse reference and pointer (different) concepts. Let Java community go that way. Thank God we are C++ people ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: DirectX in VS6..
Next Thread in C++ Forum Timeline: Cant display anything from this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC