Accessing a function variable

Asked

Viewed 92 times

0

In C, I can use a variable outside the scope where it was declared?

I know that the best thing to do is to declare her out of any role, because then she will become global. But I want to know how to operate it if the variable is within a specific function.

Example of doubt: in the code below, as I would call the variable quantidadePar, declared within the function parImpar()?

#include <stdio.h>

int parImpar(int *a){
    int quantidadePar;

    if(*a%2 == 0){
        quantidadePar++;
        return 1;
    }else{
        return 0;
    }
}

int main(){
   int num;

   scanf("%d", &num);

   parImpar(&num);

   return 0;
}
  • 2

    "can use a variable outside the scope where it was declared?" Not. If you need it elsewhere, declare it in a more external scope.

  • 2

    "I know that the best thing to do is to declare her out of any role, for then she will become global. "This statement comes from where exactly?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

3

In C, I can use a variable outside the scope where it was declared?

No, the variable only exists in the scope that has been declared.

I know that the best thing to do is to declare her out of any role, because then she will become global. But I want to know how to operate it if the variable is within a specific function.

No, this is the worst you can do, not the best. And yes, it will become global, this is the problem. Not that you can never do, but you need to know how to use it very well, it should be something well advanced to do and not as a palliative solution because it does not dominate the basic mechanism. You’re on the right track.

Example of doubt: in the code below, as I would call the variable 'quantityPar', which is declared within the function 'parImpar'?

I wouldn’t call, actually calling there is a misguided term, functions are called, variables are not. It depends on what you want, but in essence this code is all wrong.

First, this function has two responsibilities, and this is usually wrong, but if you want to insist so you would have to communicate the variable between the functions (I will disregard gambiarras just to work). Since in C can only return a single value we don’t usually make mechanisms that simulate multiple return, we would have to pass the variable by reference, something like this:

#include <stdio.h>

int parOuImpar(int valor, int *quantidade) {
    if (valor % 2 == 0) {
        (*quantidade)++;
        return 1;
    } else return 0;
}

int main(){
   int num;
   scanf("%d", &num);
   int quantidade = 0;
   if (parOuImpar(num, &quantidade)) printf("O dado é par e isto ocorreu %d vezes", quantidade);
   else  printf("O dado é impar");
}

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

Got it? The global variable seems easier and for an exercise it is, but when making a real application starts to complicate having this kind of thing, then the correct thing is to communicate this variable by parameter even, passing it by reference. There are other mechanisms, but the good ones are even more complex. If you have no idea what this is you’re probably doing something more advanced than you know at the moment.

The most correct solution would be to have a function that only does one of the two things. And it’s not hard to do this, if you don’t need to know directly if it’s even or not, because you just need to pass the value and quantity simply (without pointer), and then only returns the value of the new quantity that will be incremented according to the condition if it is even. Then in the return you compare the value you passed with the returned value, if it is different it is because it is even. The function name should change to indicate well what it does now. Can you do this? Meets your need?

Browser other questions tagged

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