-1

I have this assignement to make a simple calculator. But one of the exercises is to take the value of the division and divide it for another number. I made functions for this and what I need help with is, how to take the value returned from the function and use it again. Should I use pointers or store it in an array? TY

This is what I got so far:

int op;
int x, y, z;

printf("\n4.Divisão       '/'\n\n");
scanf("%d", &op);

//Asks the user for the values
printf("x:");
scanf("%d", &x);
printf("y:");
scanf("%d", &y);
printf("\nDivisão: %d / %d = %.1f", x, y, divide (x, y)); //Basic division
printf("Outro valor:"); //Asks for another variable
scanf ("%d", &z);       //Reads the variable
/*printf("\nDivisão 2 : %.1f / %d = %.1f", div, z, divide (div, z));*/ 
//Tried to use function variable, don't mind this

//Should it be a recursive function?
double divide (int a, int b) 
{
 float div;
 div =  a / b;
 return div; 
}
  • 1
    If you must use the function, then `double result = divide(3, 4);`. However, the returned value for the code shown will be 0 (for the arguments 3 and 4). The division is integer division; the result (3/4 is 0) is converted to `double` and then returned. Fix by casting one or both arguments to `double` before dividing, or by changing the function signature to take `double` arguments instead of `int` arguments. – Jonathan Leffler Oct 25 '17 at 15:57
  • Possible duplicate of [Why dividing two integers doesn't get a float?](https://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float) – phuclv Oct 25 '17 at 16:04
  • No, that's not the problem. The function works the way I want – Henrique Guimarães Oct 25 '17 at 16:05
  • @JonathanLeffler I just need to take the result of the function of the first division and use it as a variable on the same function – Henrique Guimarães Oct 25 '17 at 16:06
  • I don't know what you mean by "use [the result of division] as a variable on the same function". However, there's nothing to stop you writing: `double x = divide(divide(p, q), divide(r, divide(t, u)));` if you have the variables defined and initialized appropriately, and none of the divisions returns 0. – Jonathan Leffler Oct 25 '17 at 16:08

2 Answers2

0

You can assign the value to a variable using =, same as you'd assign a hardcoded value.

double value = divide (x, y);
Neo
  • 3,534
  • 2
  • 20
  • 32
  • I know what you mean but the function works the way I want for now. Thing is, it should be the user inputing the values, not values already declared in the code – Henrique Guimarães Oct 25 '17 at 16:03
0

What you can do is store the value returned by function in another variable.

Simply write double div = divide (x,y);

Now take another input z and call the divide function again.

Hope this solves your problem