0

I am currently stuck on this question but don't know what I am doing wrong:

Complete the function add10() to make the program can add 10 to each user input integer number. (NOTE: you should only change the code for function add10() . Do NOT change other code.)

The original code provided to me is this:

#include <iostream>
#include <iomanip>
using namespace std;

//TODO: complete this function, do NOT change other code
void add10()
{

}

//Please do NOT change the following code
void main()
{
    int n;
    cin >> n;
    add10(n);
    cout << n;
}

I have edited the above code as shown below, but it still returns the same value as the user input:

#include <iostream>
#include <iomanip>
using namespace std;

//TODO: complete this function, do NOT change other code
int add10(int n)
{
    return n += 10;
}

//Please do NOT change the following code
void main()
{
    int n;
    cin >> n;
    add10(n);
    cout << n;
}

Can someone please help me explain how to fix my code and what I am doing wrong? Thanks!

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
Raj Heera
  • 11
  • 2

3 Answers3

2

As you can see from the main code, the return value of add10 is discarded. This means you can as well just declare it void and return nothing.

Instead to modify the value that is passed to your function as a parameter, you want to pass it by reference:

void add10(int& n)

This will allow you to perform the obvious modification to the n in the add10 function.

Consider reading some tutorials on passing by reference, here is one example.

Here is a SO question regarding the difference between passing arguments by value and by reference.

Community
  • 1
  • 1
Ap31
  • 3,244
  • 1
  • 18
  • 25
  • Okay, I think I understand what you mean. I am not too clear between the concepts of pass by value and pass by reference. If it is not too much trouble, could you briefly explain the difference between the two? – Raj Heera Apr 16 '17 at 07:08
  • 1
    @RajHeera I could, but that's kinda another separate question which is already answered many times, so I'll just add a link to my answer – Ap31 Apr 16 '17 at 07:10
  • Perfect, thanks for the valuable help. Will look more into understanding these fundamental concepts. – Raj Heera Apr 16 '17 at 07:12
  • pass by value means it's a local copy (local to the function), pass by reference means it's a handle to the original variable (changes you make to the variable persist outside the function). – Jason Lang Apr 16 '17 at 07:47
0

If you just want to print the value you could

cout<<add10(n);

If you need the value for future use, the correct way is to pass a reference of it, so that would be

add10(n);

Your method signature would be

void add10(int &n)
Kostas Andrianos
  • 1,551
  • 2
  • 16
  • 21
0

You need a reference... c++ will make a copy of the variable n, leaving the original value unmodified no matter what you do in the function add10...

int add10(int& n)
{
    return n += 10;
}

A better approach could be to return void since your code in the main method is not grabbing that result either...

void add10(int& n)
{
    n += 10;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97