0

Hi I was hoping that I could get a better understanding of a few things regarding C++. We are learning about functions and I missed a class and I'm thinking it was the wrong class to of missed because I am so confused. I have two problems regarding clear and concise direction and understanding of terms. This was my first one:

a) Write a C++ function named "product" that calculates the product of two integer values passed to it as. The function will takes two input arguments and return the computed answer as an output parameter of the function. b) Write a single line of code that might be used to call(invoke) this function.

This is the second one: a) Write a C++ function named "product" that calculates the product of two integer values passed to it as. The function will takes two input arguments and returns the product answer as the result of the execution of the function. b) Write a single line of code that might be used to call(invoke) this function.

So my guess is that the second one is meant to be something like this:

 int product(int a, int b)
 { return a*b; }

and part b as cout << "The product of your two numbers is " << product(a,b);

First off am I right and either way what would be meant by return answer as output parameter. And while I am at and you feel like taking pity on me could you tell me should I use void instead of int and finally can someone explain better what exactly it is to return by reference. I get the whole aspect about it needs to have an & at the end of the parameter and "technically" that it allows me to change it rather than just return it but I was hoping for something a little more in depth? Sorry to be so needy but any help would be greatly appreciated. Thanks Lynda

P.S. I would have tagged as homework but it was deprecated. Sorry!

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Lynda Blanton
  • 33
  • 1
  • 2
  • 11
  • Homework tag is deprecated in part because many homework questions are too localized to be of use to future visitors, as such I've voted to close this one. – djechlin Nov 20 '12 at 03:43
  • A type of `void` means "no type". So a function returning `void` doesn't return anything, and a function taking `void` as its only argument doesn't take any arguments. In C++ the last can be shortened as an empty set of parentheses. – Some programmer dude Nov 20 '12 at 03:46
  • The lecture you missed should have accompanied a reading assignment from your textbook. Have you done the reading? Can you get the lecture notes from a classmate? Maybe your instructor provides notes or slides on the course Web site. – Rob Kennedy Nov 20 '12 at 05:17

2 Answers2

1

An output parameter is another way to send data back from a function. Essentially you don't return the value, but instead send the function a box where the answer will go. Putting the answer in the box modifies it, and the answer can be grabbed from it after the function runs.

A "standard" return value (as you did):

int product(int a, int b)
{ return a*b; }

Calling the standard function:

int answer = product(10, 5);
cout << answer << endl;

An "output parameter": instead of passing answer as an actual value (like 5 or 3), you send a box where the answer should go; changing the contents of this box will change the value of the variable in your main function as well (this is denoted int & answer instead of int answer).

void product_output_parameter(int a, int b, int & answer)
{ answer = a*b; }

Calling the function:

int answer;
// send answer in as a place to put the answer
product_output_parameter(10, 5, answer);
// the value stored in answer has changed
cout << answer << endl;

If the function was defined thus:

void product_output_parameter(int a, int b, int answer)
{ answer = a*b; }

the changes to answer would only happen inside the function, and so it wouldn't work.

Hope this helps!

user
  • 7,123
  • 7
  • 48
  • 90
0

The function you wrote out those calculate a product and does invoke the product function. You could also invoke it like this.

int productOfAandB = product(a,b);

Referecing allows change to a variable inside of the scope of the caller to the callee. Classic example would be to send Bob to the locker room to change for PE, in this procedure Bob simply goes to the locker room and changes then the procedure that told Bob to do so continues with the changed Bob. If Bob were to be copied into the locker room then returned from the locker room replacing the original Bob it would behave more like a return value function.

  1. Pass by value, Return by value BOB = changForPE(BOB);// this effectively replaces BOB with a whole new BOB
  2. Pass by Reference changeForPE(&BOB);// this function has the ability to do something to the variable stored at BOB //but it does not appear to be overwriting the BOB variable
That Guy
  • 56
  • 1