0

Here I have two functions and I call Both of them in my MAIN function. The first function gives me a value of "wage" and I want to use the value of "wage" in my 2nd function. But this doesn't work. Also, I will be using my variable in another function later in my code. Is there any way to call my values efficiently?

Here is the main function call:

#include <stdio.h>
#include <string.h>

    int main()
    {
    
        categoryA();
        workingInfo();
    }

1st function that has value of "wage" :

void categoryA()
{
    printf("\n\nCategory A: Administrative Worker[1]\n\t    Data Entry Operator[2]\n\t    Data Entry Clerk[3]\n\t    Betting Clerk[4]\n");

    int position;
    float wage;
    float overWage;
    char positionName[100];

    printf("Choose Your Job Title : ");
    scanf("%d",&position);



    if(position == 1)
    {
        wage = 12.5;
        overWage= wage*1.15;
        strcpy(positionName, "Administrative Worker");
    }

    else if (position == 2)
    {
        wage = 13;
        overWage= wage*1.15;
        strcpy(positionName,"Data Entry Operator");
    }

    else if (position == 3)
    {
        wage = 13.2;
        overWage= wage*1.15;
        strcpy(positionName, "Data Entry Clerk");
    }

    else
    {

        wage = 13.5;
        overWage= wage*1.15;
        strcpy(positionName,"Betting Clerk");

    }

    printf("wage in the category function : %.2f\n",wage);


}

2nd function where I want to use "wage" :

void workingInfo(float wage)
{

    printf("wage in the working info %.2f\n",wage);
}
  • categoryA is a void function, it is not returning anything. First return something from it, than you can either save it's value in variable and pass to other function. Or Directly call that function as argument in second function. – Rao Zaeem Shahid Jun 07 '21 at 15:29
  • wage is variable in categoryA which destroys as function ends. declare it in main function, than pass its address as argument if you want a function to change or use it's value. – Rao Zaeem Shahid Jun 07 '21 at 15:33
  • 2
    One off topic caution: it's generally [not a good idea to use floating point for money](https://stackoverflow.com/q/3730019/10077). – Fred Larson Jun 07 '21 at 15:34
  • @RaoZaeemShahid, What is wrong with returning a copy. No need to complicate things by introducing pointers – HAL9000 Jun 07 '21 at 15:43
  • @HAL9000 Nothing's wrong, I just enjoys complexity. It may messed up code but I suggest this idea for little more information. – Rao Zaeem Shahid Jun 07 '21 at 16:03
  • You may want a `struct` to hold similar data together. Passing a `struct` to a function _via_ a pointer is very common, see [arrow operator](https://stackoverflow.com/questions/2575048/arrow-operator-usage-in-c). – Neil Jun 07 '21 at 19:15

3 Answers3

1

Change the function so that it returns a float, i.e.

float categoryA()
{

    ... your current code ...

    return wage;  // Return the value of wage
}

and use it like:

int main()
{    
    float f = categoryA();  // Save the returned value (i.e. wage) in
                            // a variable with the name f

    workingInfo(f);         // Use f

    ....

    someOtherFunc(f);       // Use f again
}

or the more compact version:

int main()
{    
    workingInfo(categoryA());  // The returned value isn't saved in
                               // a variable but passed directly to
                               // the workingInfo function
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

Once categoryA() has returned, its wage variable is gone, as well as out of scope. There's no way it can be accessed.

Rather, I suggest you return wage; at the end of categoryA(), then pass the result in to workingInfo().

You'll need to change the signature of categoryA():

float categoryA()

And change the call to something like:

float wage = categoryA();
workingInfo(wage);

... or...

workingInfo(categoryA());
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
0

The simplest way you can do it by making wage as global variable, then also you will be able to use it with in multiple functions too and you can also use it as pointer too if you are comfortable with pointers.

Jarvis__-_-__
  • 287
  • 1
  • 13