paul
paul

Reputation: 27

How can I use the result from a function as a parameter to another function in C?

In a function that returns void and has no parameters I want to use two other functions (unfortunately I cannot modify that). The first of these functions calculates the result of an int and I want to pass that result as a parameter to the second function. How could I achieve that? thank you.

void descriptionEdit(void)
{
   while(1)
   {
      int fieldno;
      if (!pluFields())
      {
        break;
      }
      start_editor(setf, esize);
      do
      {
         //code
      } while(fieldno>=0);
      saveEditor();
   }
}


bool pluFields(void)
{
   **edplu** = search_productcode();
   if (edplu == 0)
   {
      return true;
   }
}

void saveEditor()
{
   if (save_plu(**edplu**, &plu))
   {
      CloseProductIniFile();
      cls();
      writeat(30, 6, INV2X3, "Save Confirmed");
   }
}

So, I want somehow to use the edplu from pluFields function as a parameter to saveEditor function, I thought to make edplu global variable but I think this is a bad practice

Upvotes: 1

Views: 779

Answers (1)

wagnerdelima
wagnerdelima

Reputation: 360

You algorithm is very non-intuitive. However, to get a value of a function and pass to another function do this:

int functionName(int param1) { 
  // calculate values
  return variable_here
}

int value = functionName(param1)

then value will go onto the parameter of your next function, like that

void function2(int value) ...
function2(value)

Let me know if I helped.

Upvotes: 2

Related Questions