Thread: Beginner's question about functions.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are calling ab() inside itself, and ab() produces that sort of output?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, think about this:
    Code:
    #include "stdio.h"
    #define max(a,b) (a)>(b)? a : b
    
    int g=0;
    
    int testfunc (int X, int Y) {
    	return max(X,Y);
    }
    
    int ab(int n, int a)
    {
        if (n==0) return 5;
    
        printf("a=.....%d\n",a);
        printf("g=.....%d\n",g);
        printf("-------------\n");
        
        g=-50;
        a-=1;
        
        g=testfunc(g,ab(n-1,a));
        
        printf("a(%d)==========%d\n",n,a);
        
        return g;
    }
    
    void main(void)
    {
    	ab(2,10);
    }
    Produces the expected:
    a=.....10
    g=.....0
    -------------
    a=.....9
    g=.....-50
    -------------
    a(1)==========8
    a(2)==========9

    ps. look for stuff about the Fibonacci set, that is the essential form of recursion I think
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C beginners question
    By incipientmind in forum C Programming
    Replies: 5
    Last Post: 02-25-2008, 02:06 PM
  2. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  3. Question about Recursive Functions
    By jack999 in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2006, 05:14 PM
  4. Replies: 3
    Last Post: 11-03-2003, 04:43 PM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM