I am trying to edit this program to pass two variables to the function call using a passing-by-reference. I need help as to how to modify what I have. Any help would be appreciated.

#include "stdafx.h"
#include <iostream>

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

int incr10(int& num); // Declare function

int main(void)
{
int num = 3;
int value = 6;

int result = incr10(num);

cout << endl
    << "incr10(num) = " << result;

cout << endl
    << "num = " << num;

result = incr10(value);
cout << endl
    << "incr10(value) = " << result;

cout << endl
    << "value = " << value;

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

return num;         // Return the incremented value
}

Recommended Answers

All 6 Replies

Welcome to DaniWeb!

(Please use code tags when posting code, it makes it much easier for us to read :) )

What you have there looks good. To add a second variable, simply do this:

int incr10(int& num, int& var2)

You didn't specify what this second variable is for, but hopefully that will get you started down the right path.

Good luck!

David

Hiya

I agree what is posted above by david is correct but id like to point something out here for you

int incr10(int& num) // Function with reference argument
{
cout << endl
<< "Value received = " << num;

num += 10; // Increment the caller argument

return num; // Return the incremented value
}

you return num, this is uneccessary as the value passed in is changed so what is happening is like so.

int passed, returned;
returned = incr10(&passed);

//in function
num+=10; // this increases the value of passed
return num; //this assign a COPY of num (passed) to returned

Basically when you pass by refrence you edit the actual variable passed. This variable will still be changed when you return from the function. by returning the value in your function the return will be a copy of the variable passed. whilst for a normal variable type this may be fine but it may not be what you intended. if you passed an instance of a class or struct to a function like this which contained pointers to some data. and then a copy is returned you can end up with 2 objects with pointers to the same data and if you may find they mess with each other.

I understand you seem newish to c++ so you wont have known that but just be aware what it is your doing in returning from a function and passing objects by refrence.

hope this helps you see just how careful one has to be in some situations.

and keep up the good work you seem to be working well with c++

Hi,I have added the second variable to the function but now I am getting compile error C2660 incr10 function does not take 1 agruments. I am sorry but I am new at this and trying to learn. The Code tags,did I use them?

#include "stdafx.h"
#include <iostream>

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

int incr10(int& num, int& value); // Declare function

int main(void)
{
int num = 3;
int value = 6;

int result = incr10(num);

cout << endl
	<< "incr10(num) = " << result;

cout << endl
	<< "num = " << num;

result = incr10(value);
cout << endl
	<< "incr10(value) = " << result;

cout << endl
	<< "value = " << value;

cout << endl;
return 0;
}
// Function to increment a variable by 10
int incr10(int& num, int& value) // Function with reference argument
{
cout << endl
	<< "Value received = " << num;

num += 10;			// Increment the caller argument

return num;			// Return the incremented value
}

Kanoisa is right, you are returning the variable by reference AND by value, which is not at all necessary.

Also, in your latest post, you are calling the function with only one argument:

incr10(num);

when you have declared it as taking two!

int incr10(int& num, int& value)

David

Hi,

In addition to what david put, this may be a bit to much for here, but what you can do if you want flexability of one or two vars to your function, you can take 2 approaches.

Function overloading

Possibly the easiest approach, you declare the function with the same return type and name but a different number and or type of inputs, the compiler picks the correct one based on passed arguments such as.

int inc10(int &num); //declares one version which takes one int
int inc10(int &num, int &value); //declares another version which takes 2 ints

//in code 
inc10(&someInt); //this calls the first version;
inc10(&someInt, &anotherInt); //this calls the 2 ints version

Default parameters

this version may or may not be more useful for you, it makes just one function but allows you either one or 2 inputs

int inc10(int &num, int&value = 0);

//in code 
inc10(&someInt); //this is fine it uses the default assigned in the prototype for param 2 which is 0
inc10(&someInt, &anotherInt); //this uses anotherInt for the second param instead of the default

The second example can be useful such as for a function who's behaviour can be changed as the program runs like say

int incVar(int num, int incBy = 1); //decleration

//the function
int incVar(int num, int incBy) //note the default value only given in prototype
{
   return(num+incBy);
}

//usage 
int myResult = 0; input1 = 5; input2 = 3;

myResult = incVar(input1);         //myResult = 6 (5+1 = 6)
myResult = incVar(input1, 15);     //myResult = 20 (5+15 = 20)
myResult = intVar(input1, input2); //myResult = 8 (5+3 = 8)

This function will by default increment by one, but you have the option to change this behaviour by specifying a different value to increment by if you wish.

Hope this gives you some ideas to improve and or expand your functions to make bigger and better things

Thank you everyone for your input. It has helped greatly.

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.