1. ## no returning

Hi mates. I'm new at programming, I've tried to make a program that return 1 if a situation is true otherwise 0, but quitting the function "encaixa", the returning to the main fuction is garbage. I have no clue what's wrong...Below my code:

Code:
```#include<stdio.h>
#include<stdlib.h>

int encaixa(int x, int y);

int main()
{
int a, b, ma, me;

do
{

printf("Entre com um inteiro positivo: \n");
scanf("%d", &a);

if(a < 0)
printf("O número deve ser positivo\n");
}while(a < 0);

do
{
printf("Entre com outro inteiro positivo: \n");
scanf("%d", &b);

if(b < 0)
printf("O número deve ser positivo\n");
}while(b < 0);

if(a >= b){
ma = a;
me = b;
}
else{
ma = b;
me = a;
}

printf("%d\n", encaixa(ma, me));
return 0;
}

int encaixa(int x, int y)
{

if(x == y)
return 1;

else if(x % 10 == y % 10)
encaixa(x / 10, y / 10);

else if(y == 0){
printf("%d\n", y);
return 1;

}
else{
printf("%d\n", y);
return 0;

}
}```

2. So if x%10==y%10 what do you return? Nothing at all.

3. Originally Posted by tabstop
So if x%10==y%10 what do you return? Nothing at all.
I was calling the function again for recursion, the returning is made after all the possible calls, in fact I'm not good at recursion, could you help me to fix it?

4. Originally Posted by Oliveira88
I was calling the function again for recursion, the returning is made after all the possible calls, in fact I'm not good at recursion, could you help me to fix it?
I don't deny that you called the function again. You calculate a value, now you must do something with it. Presumably you want to return the value. After all consider a function that did this:
Code:
```int bob(int x) {
x + 1;
}```
That's what you've got: you've calculated the value. Now you need to send that value back where it belongs.

5. Thanks for trying help, but I think I need to study more about recursion and returning, I've fixed my program using a pointer and an auxiliary variable. See below:

Code:
```#include<stdio.h>
#include<stdlib.h>

void encaixa(int x, int y, int *p);

int main()
{
int a, b, ma, me, aux = 0, *p;

p = &aux;

do
{

printf("Entre com um inteiro positivo: \n");
scanf("%d", &a);

if(a < 0)
printf("O número deve ser positivo\n");
}while(a < 0);

do
{
printf("Entre com outro inteiro positivo: \n");
scanf("%d", &b);

if(b < 0)
printf("O número deve ser positivo\n");
}while(b < 0);

if(a >= b){
ma = a;
me = b;
}
else{
ma = b;
me = a;
}

encaixa(ma, me, p);

printf("%d\n", aux);

return 0;
}

void encaixa(int x, int y, int *sig)
{

if(x == y)
*sig = 1;

else if(x % 10 == y % 10)
encaixa(x / 10, y / 10, sig);

else if(y == 0)
*sig = 1;

else
*sig = 0;

}```

6. That seems overkill, compared to adding the word "return" in the one place you missed it out.

7. Yep, you could just do -

else if(x % 10 == y % 10)
return encaixa(x / 10, y / 10);

8. Originally Posted by Spidey
Yep, you could just do -
:P thanks

9. ahhh now I get this! I was using an int fuction as a void function, as int I need to use the "return" at the calling!

Sorry, newbie mind mode slow