Thread: Recursive function returning 0

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    Recursive function returning 0

    This function return the sum of the numbers that are bigger than k. I understand all of its logic but iam confused with return 0. Say we have k = 9, so we adding numbers until tam ==0, but by returning 0 shouldn't we lose the value that we accumulated? How come the function return the sum and zero at same time?

    Code:
    #include <stdio.h>
    
    int SumBiggerThanK(int k, int a[], int size)
    {
    	if(tam==0)
    	{
    		printf("GOT HERE");
    		return 0;
    	}
    	else if(a[size-1]>=k)
    	{
    		return a[size-1] + SSIK(k, a, size-1);
    	}
    	else
    	{
    		return SSIK(k, a, size-1);
    	}
    }
    
    
    int main()
    {
    	int a[]={9, 18, 31, 40, 42};
    	
    	printf("%d\n", SumBiggerThanK(9,a, 5));
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it will not even compile

    first show the valid code before asking how it works
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    sorry

    Code:
    #include <stdio.h>
    
    int SumBiggerThanK(int k, int a[], int size)
    {
    	if(size==0)
    	{
    		printf("GOT HERE");
    		return 0;
    	}
    	else if(a[size-1]>=k)
    	{
    		return a[size-1] + SumBiggerThanK(k, a, size-1);
    	}
    	else
    	{
    		return SumBiggerThanK(k, a, size-1);
    	}
    }
    
    
    int main()
    {
    	int a[]={9, 18, 31, 40, 42};
    	
    	printf("%d\n", SumBiggerThanK(9,a, 5));
    }

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Function is good i just don't understand the return 0. When it does return 0 it can quit the function and at the same time return the value that accumulated?

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by telmo_d View Post
    Function is good i just don't understand the return 0. When it does return 0 it can quit the function and at the same time return the value that accumulated?
    It returns '0' at the end of the recursion - when there are no more numbers in the array that need to be evaluated against 'k'.

    The '0' is added to the values got in the recursions.

  6. #6
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    I was thinking it would ignore the rest and turn out a '0'
    Now it makes perfect sense, thanks.

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by telmo_d View Post
    This function return the sum of the numbers that are bigger than k. I understand all of its logic but iam confused with return 0. Say we have k = 9, so we adding numbers until tam ==0, but by returning 0 shouldn't we lose the value that we accumulated? How come the function return the sum and zero at same time?
    It seems like you mean to get the sum of the number that are greater than 'k', right ?

    If yes, then you should have
    Code:
     else if(a[size-1]>k)
    instead of :
    Code:
     else if(a[size-1]>=k)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  4. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  5. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM