Can a function call itself?

This is a discussion on Can a function call itself? within the C Programming forums, part of the General Programming Boards category; Hi all, I have to program a recursion to find the answer to a maths question, the recursion formula I ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Can a function call itself?

    Hi all, I have to program a recursion to find the answer to a maths question, the recursion formula I am working with is:

    f(a,b) = f(a,b-1) - f(a/g(b),b-1)

    where g is an arbitary function that is easy to use

    I also have the condition that f(a,0)=a

    What is the best way to go about programming this kind of thing in C?

    My initial idea was to have the function call itself if b wasn't 0, but that doesn't seem to work, will this method work or is there a much better SIMPLE way?

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    My inital try

    Code:
    MainCL{
    printf("%d", f(10,5));
    }
    
    int f(int a, int b){
    if(b=0){
    return a;}
    else{
    return f(a,(b-1))-f(a/2,(b-1));}
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: My inital try

    Originally posted by ajm218
    Code:
    MainCL{
    printf("%d", f(10,5));
    }
    What exactly is this?


    Originally posted by ajm218
    Code:
    int f(int a, int b){
    if(b=0){
    return a;}
    else{
    return f(a,(b-1))-f(a/2,(b-1));}
    }
    Your if statement is wrong. '=' is assigmnent, '==' is an equality test.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Correction to initial Code

    Sorry just to give a quick example of some input, so i gave f(10,5)

    Code:
    MainCL{
    printf("%d", f(10,5));
    }
    
    int f(int a, int b){
    if(b==0){
    return a;}
    else{
    return f(a,(b-1))-f(a/2,(b-1));}
    }

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    what is MainCL , i never heard of it.
    is it just like int main()?
    can someone please explaine?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    66
    Wish instructors did not do braindead things like #define MainCL ...

    As far as recursion, it is easy to do in C, you need 2 things:
    1. your algorithm
    2. stopping condition to unwind your recursion

    Also you don't want to recurse too deep, every call to itself takes up stack space in a form of return address and parameters, after so many calls you may run out of stack space, but that's just some technical detail.

    Code:
    #include "stdio.h"
    
    double factorial(double a)
    {
    /* Stopping condition */
      if (a < 1.0)
        return 1.0;
    
    /* algorithm based on itself */
      return a * factorial(a-1.0);
    }
    
    int main(int argc, char **argv)
    {
      printf("factorial(5)=%f", factorial(5.0));
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  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