#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	float leg1, leg2, hyp;
	cout << "Enter one leg ";
	cin >> leg1;
	cout << "Enter one leg ";
	cin >> leg2;

	if ((leg1 < 0) || (leg2 < 0))
		hyp = 0;

	else 
		hyp = (sqrt((float)leg1*leg1 + (float)leg2*leg2));


	cout << "The hypotenuse is " << hyp << endl;


	return 0;
}

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?

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

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.

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

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

void make5(int& ref){

    ref = 5;

}

int main(){

    int x = 9; // x exists on the stack, and is constructed with value 9
    cout << x << endl; // printing out x
    make5(x); // passing x into the method by reference
    cout << x << endl; // x should be 5 now

    cin.get();
    return 0;
}

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

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

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 ;)...

commented: Good point. +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.