no returning

This is a discussion on no returning within the C Programming forums, part of the General Programming Boards category; Hi mates. I'm new at programming, I've tried to make a program that return 1 if a situation is true ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    7

    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;
    			
    		}
    	}
    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if x%10==y%10 what do you return? Nothing at all.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Quote Originally Posted by tabstop View Post
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Oliveira88 View Post
    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. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That seems overkill, compared to adding the word "return" in the one place you missed it out.

  7. #7
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yep, you could just do -

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

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Quote Originally Posted by Spidey View Post
    Yep, you could just do -
    :P thanks

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. importance of returning reference in operator overloading.
    By vaibhavs17 in forum C++ Programming
    Replies: 20
    Last Post: 05-13-2009, 01:28 PM
  2. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 11:25 PM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 06:38 PM
  4. Function returning incorrect value
    By CHurst in forum C Programming
    Replies: 3
    Last Post: 12-13-2005, 01:27 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21