Variable value changes in main function

Asked

Viewed 142 times

-2

When I declare a function and put the parameter to receive a variable of the integer type, for example, within the main function when I declare the function int param(int x); and then I call the function and put the variable x: param(x), the x does not return the same value in the main function as it returns in the function int param(int x), por que acontece isso?

When I leave the function empty in the input parameter, without receiving variable (by the way, say that you do not receive variable is right?). In the tests I did the function ends up receiving the value of x even though I left the parameter empty, type int param(), the value that the variable x returns in function param(int x) is the same value as the variable x in the main function receives.

The value of x in the param function of the code has been changed, explain why this value changed in main function.

#include<stdio.h>
#include<stdlib.h>
int x;
int param( ){
   x = x + 10;
   return x;
}
int main(){
   int y;
   printf(“Digite um numero: “);
   scanf(” %d”, &x);
   y = param();
   printf("\nO valor de y e %d \n", y);
   printf("\nO valor de x e %d \n", x);
   system(“pause”);
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

4 answers

2

You’ve declared a global variable, which means out of function, it’s valid for every application, so it’s accessed everywhere. This is not appropriate, it only works well in exercises and should not do so, but since you made any change in it is reflected everywhere. The ideal is not to have this variable, it should only have local variables and communicate values through parameter and value return.

This code produces a more sensible result:

#include<stdio.h>

int param(int x) {
    return x + 10;
}

int main() {
    printf("Digite um numero: ");
    int x;
    scanf(" %d", &x);
    int y = param(x);
    printf("\nO valor de y e %d \n", y);
    printf("\nO valor de x e %d \n", x);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Now it has purely local variables, so even if they have equal names they are completely independent of each other.

  • Oh yes, thank you for the answer, the question is for didactic purpose, of my faculty, and I’m not seeing the error, I’m not understanding when I leave the parameter empty, the value of the variable x that is returned in the function int param() it behaves as a global variable, now when I put the parameter as integer x, that is, int param(int x), the value of the variable in this function is only valid for this function, and not for the main function, that is, it does not behave as a global variable

  • Yes, when you use a local variable, it is not global.

  • But why am I using local variable if the global still remains?

  • Because the site has priority over the global one, and even so it shouldn’t be used, it’s confusing.

0

In the code above x is a global scope variable, this means that at any point in your program you change the value of x it will be changed and the new value recognized throughout the program.

When you declare:

int param(int x){
    x = x + 10;
    return x;
}

the parameter x has local scope. Actually in the function call the value of the variable used in the function call is placed on the stack and the value change occurs on the stack, which is lost at the end of the run.
If you want the modification of the value of a parameter performed within a function to be recognized in the calling function then pass the parameter by reference.

============================

Complementing:
You can pass the parameter by value or by reference that is to pass the variable address and properly treat this parameter. For the example above:

int param(int * x){
    *x = *x + 10;
    return *x;
}

and the call of the function of the form:

y =param(&x);

Study the scope of variables and also pointers to better understand.

  • Thank you for the answer! But what exactly would be passing the parameter by reference in this case? I don’t understand when I leave the parameter empty, the value of the variable x that is returned in the int param() function behaves like a global variable, now when I put the parameter as integer x, ie int param(int x), the value of the variable in this function is only valid for this function, and not for the main function, that is, it does not behave as a global variable, I am not able to understand

0

The idea of this exercise was to show the effect of variable scope and the notion of name collision. You can have several x active in your program, and the notion of visibility of a variable --- scope, Scope --- is what you try to show with your program.

I will rewrite your program and try to explain. But first I will write about your program

your original program

Although your small program has several problems beyond what can lead to your doubt, and below is the original with numbered lines. Here are some notes and when the case is the line number will be between [ ].

    1   #include<stdio.h>
    2   #include<stdlib.h>
    3   int x;
    4   int param( ){
    5   x = x + 10;
    6   return x;
    7   }
    8   int main(){
    9   int y;
   10   printf("Digite um numero: ");
   11   scanf(" %d", &x);
   12   y = param();
   13   printf("\nO valor de y e %d \n", y);
   14   printf("\nO valor de x e %d \n", x);
   15   system("pause");
   16   }
  • Use some alignment in your code. It helps you and others who come to read your program
  • every C program begins with main() and if you expect to see this early on, especially if it’s a program that you didn’t write. In general main() is even in a separate file, for reasons that will soon learn. Use only prototypes at the beginning.
  • [4] if your function does not receive parameters indicate this to the compiler, declaring void as in
    int    param(void){
  • [8] the prototype of main() is
    int main(int argc, char** argv);

If you will not use the declare parameters as well main(void)

  • [11] Never stop reading the return of scanf(). Your program reads a single number. If scanf() not read anything, what is the purpose of following the program?
  • [11] What is the purpose of reading a value if nothing adds to your program? It only takes longer to test. Why not use something like x = 200; and move on? What difference does it make what value the user will enter if you will only add up 10 and see if it changes?
  • [15] do not use system(). You will not be doing anything special, nor learning anything. system() just passes a command to the shell run. And is insecure and prohibited in many circles.
  • [15] Do not use pause to stop the program. If you do this because of some IDE simply switch to another that stops the program by itself while running on the console. For years we have had several quality and free Eids: change to a better one. There is no reason to use one with such a stupid disability and have to change programs for example before delivering, just for this problem. If your instructor or school uses an automatic test system your program may not work properly. And if your teacher runs the program in a terminal he’ll stand there at the end for no reason...
  • do not test your programs just within the IDE. It is a special and less significant environment: the code generated is different, the behavior is different. Learn to test on the console or, better still, on Windows Terminal, if you use Windows. And with the production code, this Release code, which is smaller and much faster.

The same program realigned

your slightly altered program with some of the things I explained

#include<stdio.h>
#include<stdlib.h>

int x;

int param(void);

int main(void)
{
    int y;

    x = 200;
    printf( "\nEm main() x vale %d\n", x);

    y = param();
    printf( "\nDepois de chamar a funcao:\n\n");
    printf("O valor de y e %d \n", y);
    printf("O valor de x e %d \n", x);
}

int param( )
{
    x = x + 10;
    return x;
}

The difference is small, but maybe you’ll see an easier way to read the program.

A way to run, using any version of gcc on Windows

PS C:\chp21-0203-array> gcc -o tst -Wall -std=c17 so0207.c 
PS C:\chp21-0203-array> ./tst

Em main() x vale 200

Depois de chamar a funcao:

O valor de y e 210
O valor de x e 210 

The notion of scope and a more convenient program for testing

Note that this 3-line C program

1
2    int x = 300;
3

Compile without problems:

PS C:\chp21-0203-array> cat p1.c

int  x;

PS C:\chp21-0203-array> gcc -c -Wall -std=c17 p1.c
PS C:\chp21-0203-array> 

What does it mean?

Only declares a variable int by name x and that can be used in other programs. It is valid for the whole P1.c file. The fact that there is nothing else in it is irrelevant to the compiler. But if you have more code within P1.c that variable x will be set to the entire file. If you have multiple functions, whether or not you have main() the variable will be set. It says that x has global scope for P1.c.
In the case of your program, x exists in param() or in main() and in any other code in the file.
But whether inside a function --- or even inside a loop for --- you declare another x this variable, this new x overrides the initially declared value. When returning from the function or exiting the loop this value disappears and again the "global" value of x.

a more expressive example

Let’s consider two files: P1.c

int  x = 300; // [1]

// fim de p1.c

and so0207.c

#include<stdio.h>
#include<stdlib.h>

extern int x; // [2]

int param(void);
int param2(int);

int main(void)
{
    int x = 223; // novo x em main()! [3]
    int y;

    printf( "\nEm main() x vale %d\n", x);

    y = param();
    printf( "\nDepois de chamar a funcao sem argumentos:\n\n");
    printf("O valor de y e %d \n", y);
    printf("O valor de x e %d \n", x); // [4]

    y = param2(-2345);
    printf( "\nDepois de chamar a segunda funcao com o argumento -2345:\n\n");
    printf("O valor de y e %d \n", y);
    printf("O valor de x e %d \n", x);
}

int param()
{
    x = x + 10; // [5]
    printf( "\nValor de 'x' ao retornar da funcao param(): %d\n", x);
    return x;
}

int param2(int x)
{   // como declarou x ele vai se sobrepor ao valor global
    // o valor de main() e local e nao seria visivel aqui de todo modo
    x = x + 10; // [6]
    printf( "\nValor de 'x' ao retornar da funcao param(int x): %d\n", x);
    return x;
}

Compiling and running this:

PS C:\chp21-0203-array> gcc -o tst -Wall -std=c17 so0207.c p1.c
PS C:\chp21-0203-array> ./tst

The output, with numbered lines:

    1
    2   Em main() x vale 223
    3
    4   Valor de 'x' ao retornar da funcao param(): 310
    5
    6   Depois de chamar a funcao sem argumentos:
    7
    8   O valor de y e 310
    9   O valor de x e 223
   10
   11   Valor de 'x' ao retornar da funcao param(int x): -2335
   12
   13   Depois de chamar a segunda funcao com o argumento -2345:
   14
   15   O valor de y e -2335
   16   O valor de x e 223

And why after all?

  • In line 2: in main x is worth 223. The x value declared in line with the comment [2] comes from outside, and comes from P1.c, of course. Worth 300 as it is in the listing above. But in main on line 11 appears another x with the value of 223. This is the value vis;ivel in main and the previous value disappears.
  • in row 4: in param the value of x is the global value, 300 plus the increment of 10 and this is what the function returns to main and thus the value of y is 310. As the program came out of param() all of param() is gone, and the value of x becomes the value of x in main, 223. and this explains the lines 8 and 9 of the output.
  • when calling param2(int) version with param2(-2345) this will be the value of x within param2 and so the value in row 11 is -2345 + 10
  • And so when returning to main y worth -2335. x that was never changed in main remains clear worth 223

I used the x in a different file only to show that even in this case the value is available in the program, but only if it is not hidden by any other variable with the same name.

-1

You declared the variable x but only assigned a value to it in the param() function if you want to display the x value inside the function will look like this: printf("the value of x and: %d",param());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.