1

I'm very new to C, and am writing a few programs to try and understand how variables can be passed and changed in between functions. I wrote a simple program below:

int number;

int test( int number ) {
    number = 3;
    return(number);
}

int main () {
    printf ("\nPlease enter in any number between (10 - 99,999)!\n");
    scanf("%d", &number);

    test(number);

    printf ("\nYour number probably wasn't: %d\n", number);
}

The point was to enter in a number, but no matter what you enter that number would be changed to 3. For some reason though when i call test on that number, it will not change to 3 but instead be whatever the user entered. Note that I am trying to change the variable number, and not have any other variable hardcoded as 3.

How come this does not work?

  • Please [edit] your question title to something relevant to the subject of your question. Your question has nothing to do with "starting a c program", and that title would not have any meaning if it turned up in a search result for someone in the future. Thanks. – Ken White Jun 13 '14 at 18:35
  • Apart from the other issues with your code that the other answers deal with, the language feature you're looking for is called *pointers*: http://en.wikipedia.org/wiki/Pointer_%28computer_programming%29 – Michael Rawson Jun 13 '14 at 18:40
  • Thank you for all the wonderful answers!! – user3530059 Jun 13 '14 at 18:52

8 Answers8

1

You are changing a local variable in the function test, not the actual variable. You want number = test(number); as the second to last line in your main() function.

Your function test doesn't deal with the original number that you passed to it. Instead, it gets a copy of this variable and plays around with it locally. Any changes you make to number in this function will get lost (unless you pass in a reference, which you will learn about later, presumably). The correct way to get around this is, as you have already done, return the value of number. However, you don't use that returned value at all. When you say number = test(number);, you are telling the computer to take that returned value and assign it to the original number (the one in your main), which is what you want.

wolfPack88
  • 4,163
  • 4
  • 32
  • 47
1

Change

test(number);  

to

number = test(number);  

Otherwise the value returned by test will be discarded and you will get the number you entered, not 3.

Another way to get the desired effect is to pass the pointer to number:

void test( int *number ){..}

and call the function as

test(&number);  

Some answers suggested that to use number as global variable. I advice you that do not use global variable when it is unnecessary.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
1
int number; //a

int test( int number ) { //b
    number = 3;
    return(number);
}

Within test, there are actually two variables named number,
a (which is the number used in main too), and b which has priority.
You´re changing the parameter b to 3.
However, the parameter is only a copy of the original passed value in main
(as usual in C and C++), and gets discarded and the end of test.
Furthermore, you´re not using the return value in main, so 3 is gone.

deviantfan
  • 11,268
  • 3
  • 32
  • 49
1

All answers so far seem to ignore the fact that you are trying something which does not make much sense. You are returning what is basically a constant from your function, but using a passed value as some intermediate storage while that is completely unnecessary. Either of those two will work:

 void test( int& number ) {//We don't need to return anything, because we pass number by reference, and so changes made to it will persist outside this functions scope
    number = 3;
}

int test(){
return 3; //We just return 3, we don't need any arguments passed to the functions in this version
}

Now in the first case you call the functions as

test(number); //number will be changed by test because it's passed by reference

in the second case you call the functions as

number = test(); //Here test just returns 3 and we assign that value to number.

Edit:

Just for completeness sake I will add the third version that you can use:

void test(int* number){//Here we pass a pointer to an integer to test
 *number = 3;//and change the value pointed to be number to 3. Read * as 'value pointed at by'. So this says 'value pointed at by number = 3'
}

in this case you call the function as

test(&number)//read & as 'pointer to'. So now we pass 'pointer to a' to test.

Note that this version is basically a less elegant and more error prone version of the first version I mentioned (at least in this specific scenario). Their usage and effects are more or less equal, but in the first version you do not need to concern yourself with dereferencing and the general fact that you are working with pointers.

user2520938
  • 411
  • 3
  • 15
1

Perhaps you misunderstand the idea of 'variable scope'.

int number;

The first occurrence of the variable 'number' is declared outside of all functions (and braces). Hence, it has "global" scope.

int test( int number ) {

The variable 'number' defined as an argument to test() has a limited scope. Its life, and visibility, is limited to the braces of the test() function. Outside these braces, this 'number' variable does not exist.

The 'global' variable 'number' and the 'test()' variable 'number' are distinct from each other, each with its own unique value.

Being they are named the same, the 'global' variable 'number' becomes inaccessible inside the 'test()' braces. Hence, any manipulation of the 'number' within the braces of 'test()' does not have any effect on the 'global' variable 'number'.

number = 3;

The above has no effect on the 'global' variable 'number'. Rather it only initializes the 'local' variable 'number' to 3.

return(number);

The above will return the value of the 'local' variable 'number' (3).

}



int main () {
    printf ("\nPlease enter in any number between (10 - 99,999)!\n");
    scanf("%d", &number);

The line above sets the 'global' variable 'number' to a value entered by the user.

    test(number);

The line above passes the value of the 'global' variable 'number' to test. It does not capture the 'return' value (3) returned by test. If desired, the above line could be rewritten:

    number=test(number);

The above line would set the 'global' variable 'number' to the return value of test (3).

    printf ("\nYour number probably wasn't: %d\n", number);

}

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
0

Your code never does anything with the value test returns. Your code would be easier to understand if you didn't call both variables (the one in main and the one in test) number.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

You have 2 options. One is

number = test(number);

Second is refer Pointers in C. Pass by address would do the trick for you.

unbesiegbar
  • 471
  • 2
  • 7
  • 19
0

It doesn't work because you're doing a pass by value instead of a pass by reference. If you want the value of number to be always 3 without doing an assign when you call the function, then you should do something like:

void test(int *number){
    *number = 3;
}

and call like:

test(&number);
edC0der
  • 1
  • 7