4 Replies - 735 Views - Last Post: 01 September 2013 - 07:48 PM Rate Topic: -----

#1 JohnPadgett55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-September 13

use a pass-by-value and a pass-by-reference program as reference

Posted 01 September 2013 - 06:57 PM

Write a similar program, but change the code to pass two variables to the function call rather than one. I have written a new code but am having trouble figuring out how to add two variables rather than one? here's what I have now, sorry about an easy question but I'm new to C++ code and just learning basics. Hope you can help? Here is what I have now for both code's.
Passing-by-value:

#include <iostream>
using std ::cout;
using std ::endl;

int incr20(int num);   // Function prototype
int main (void)
{
  int num (5);
  cout << endl << "incr20(num)="<< incr20(num) << endl
       << "num" = " << num << endl;
  return 0;
}


Passing-by-reference:
#include <iostream>
using std::cout;
using std::endl;
int incr10(int& num); // Function prototype
int main(void)
{
int num(3);
int value(6);
int result = incr10(num);
cout << endl << “incr10(num) = “ << result
<< endl << “num = “ << num;
result = incr10(value);
cout << endl << “incr10(value) = “ << result
<< endl << “value = “ << value << endl;
return 0;
}
// Function to increment a variable by 10
int incr10(int& num) // Function with reference argument
{
cout << endl << “Value received = “ << num;
num += 10; // Increment the caller argument
// - confidently
return num; // Return the incremented value
}

This post has been edited by jimblumberg: 01 September 2013 - 07:02 PM
Reason for edit:: Added code tags, please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: use a pass-by-value and a pass-by-reference program as reference

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: use a pass-by-value and a pass-by-reference program as reference

Posted 01 September 2013 - 07:05 PM

Please find an indentation style you like and use it consistently. This will make your program much easier to read.


Quote

Write a similar program

Similar to what?


You may want to study the function tutorials contained in my signature.

And please use code tags when posting code.

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#3 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: use a pass-by-value and a pass-by-reference program as reference

Posted 01 September 2013 - 07:17 PM

To pass a another variable, also called a parameter, to the function. Change the function prototype and definition by adding another declaration. Variables are separated by commons.

So it as simple as this:

int func(int num1, int num2);

To pass by reference you add a '&' to the beginning to the variable name.

Like this,

int func(int & num1, int & num2);

You should read up on function tutorials such as in Jim's signature.
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: use a pass-by-value and a pass-by-reference program as reference

Posted 01 September 2013 - 07:21 PM

Welcome to D.I.C., John! :)

What about writing a function that adds two numbers together. One gets passed one number, and a variable in the function is the second number that gets added.

The second (similar to the first) function, gets passed both numbers, and also adds the two numbers together.

That sounds easy and illustrates the process, as well.

And indentation style, is a BIG deal, when you want to study other people's code. The more you code, the more you appreciate a good indentation style. Our brains get "wired" to recognize common C idiom's, straight away.

So work on that indentation style, first thing.
Was This Post Helpful? 0
  • +
  • -

#5 JohnPadgett55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-September 13

Re: use a pass-by-value and a pass-by-reference program as reference

Posted 01 September 2013 - 07:48 PM

View Postjimblumberg, on 01 September 2013 - 07:05 PM, said:

Please find an indentation style you like and use it consistently. This will make your program much easier to read.


Quote

Write a similar program

Similar to what?


You may want to study the function tutorials contained in my signature.

And please use code tags when posting code.

:code:/>

Thanks Jim, I read your function tutorials and they were a big help, my text in school was very confusing to me as I was reading and looking at the examples they offered. I wish they weren't so confusing, your tutorial was very well explained on how to write the code. I will definitely work on my indentations. Thanks again !!


Jim

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1