Home > Net >  Can I call a local variable from one function to another function?
Can I call a local variable from one function to another function?

Time:02-05

I am new to programming and we're coding a "grocery store" and a "bag", wherein we buy things, which is added to the bag.

I tried using local variables in the "grocery store". However, can the local variables be used in the "bag"?

We're not allowed to use global variables!

#include<stdio.h>

char GroceryStore(GroceryStoreChoice)
{
    int VegetablesCount = 0;
    int FruitCount = 0;
    int MeatCount = 0;
    char GroceryStoreChoice;
    
    printf("What would you like to buy?");
    
    scanf("%c", &GroceryStoreChoice);
    
    if (GroceryStoreChoice == 'A') /* for purchasing vegetables*/
    {
        VegetablesCount  ;
    }
    
    if (GroceryStoreChoice == 'B') /* for purchasing fruit*/
    {
        FruitCount  ;
    }
    
    if (GroceryStoreChoice == 'C') /* for purchasing meat*/
    {
        MeatCount  ;
    }
}

void Bag()

{
    printf("You have %d Vegetables!", &VegetablesCount);
    printf("You have %d Fruit!", &FruitCount);
    printf("You have %d Meat!", &MeatCount);
}

int main()
{
    int Choice;
    printf("Welcome!");
    printf("Would you like to go to Grocery Store or Check Your Bag?");
    
    scanf("%d", %Choice);

    if (Choice == 'A')
    {
        GroceryStore (GroceryStoreOption);
    }
    if (Choice == 'B')
    {
        Bag();
    }
}

CodePudding user response:

Sharing information between functions in C happens in one of two ways:

  1. Reading and modifying global variables.
  2. By passing information into functions with parameters/arguments, and by returning information out of a function via its return value.

Always try to use the latter as the former is inflexible and makes for brittle programs.

CodePudding user response:

Sit back and think what you need here.

You have 3 different parameters, belonging to "you", which are the amounts of vegetables, fruits and meat that you have. When you go to the grocery store, i.e. call the function GroceryStore, you want it to act on those variables and change them. You need to give the variables to the function, not to extract them from it.

Similarly, you want the Bag function to look at these variables and print them out for you, again, you need to give them to it.

Difference between the GroceryStore and Bag functions is that the GroceryStore needs to modify them, overwrite them, if you will, it needs the variables themselves, whereas the Bag just looks at them, it only needs their values. Search the terms "passing by value" and "passing by reference", I am sure you will figure it out.

Then you can just define your variables locally in the main function, and pass them around.

Also, I strongly recommend you to read about C naming conventions.

CodePudding user response:

Welcome to C programming, it's gonna be fun and frustrating, as melonduofromage, you should look om standards of naming, but that was not a part of your question but do your self the favor and do so anyway. I will try to run though the program "with" you, and give out some pointers to what you need to look into, and I will also give you my solution to your problem, but you need to understand it before you actually use it (it will help you in the long run)

First, your GroceryStore function, is set to a char type, it don't really need that, for what I can it I doesn't return anything, so just change it to void, also you send the choice to that function, but you don't really need that as you don't use it anywhere in there, you make a new choice in that function.

Now I will jump down into the main function, there I would initialize (write up you variables and give them names). also change the Choice variable to char, as the scanf looks for a char (kinda, but more in-depth is not important right now). Another thing with the scanf is you need '&' in front of the variable you want to load the char into not '%'. I would write the main function as follows

int main()
{
    char choice;
    int VegetablesCount = 0;
    int FruitCount = 0;
    int MeatCount = 0;


    printf("Welcome! ");
   
   while (1)
   {
        printf("Would you like to go to Grocery Store or Check Your Bag?");
        scanf(" %c", &choice);

        if (choice == 'A')
        {
            GroceryStore(&VegetablesCount, &FruitCount, &MeatCount);
        }
        if (choice == 'B')
        {
            Bag(&VegetablesCount, &FruitCount, &MeatCount);
        }
    } 
}

when using the '&' in the function call, you send a reference to the address of the variable ('&' is called reference when used like this) to the function. Then you can write the GroceryStore function as follows.

void GroceryStore(int *v, int *f, int *m)
{

    char groceryStoreChoice;

    printf("\nWhat would you like to buy? ");

    scanf(" %c", &groceryStoreChoice);

    if (groceryStoreChoice == 'A') /* for purchasing vegetables*/
    {
        *v = *v   1;
    }

    if (groceryStoreChoice == 'B') /* for purchasing fruit*/
    {
        *f = *f   1;
    }

    if (groceryStoreChoice == 'C') /* for purchasing meat*/
    {
        *m = *m   1;
    }
}

When using the '' (pointer) you point to the address of the reference you just got from the function call. then in the if statements, you also need the '' as it's the value in the memory location that the variable points to you want to change.

The print function you also need to call with references like shown in the main code snip and then write the print functions as follows

void Bag(int *v, int *f, int *c)
{
    printf("You have %d Vegetables!\n", *v);
    printf("You have %d Fruit!\n", *f);
    printf("You have %d Meat!\n", *c);
}

again you print out the value in the memory location that the variable pointer points to.

Another thing, be sure to look up print things like '\n' which makes a new line, so that everything doesn't come in one line.

If you see my scanf functions they have a space before the %c, that's to make sure that there isn't a 'enter' in the buffer or something like that. Also you can make something in the while statement where if the choice is C then you should not run the while loop, so something like

while(choice != 'C')

then you can exit the function instead of now it just runs forever.

I hope it gives you an idea, and to the more pro guys than me, I know there is some better ways, but I need to think longer over them still, (only been studying 1 year) and also the guy need to learn it from the bottom, so yeah forgive me please

  •  Tags:  
  • Related