Thread: Beginner's question about functions.

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    Beginner's question about functions.

    I'm trying to study how the functions in C work.

    First: Does anyone has any good book that deals specifically with functions in C?

    I'm very puzzled about the recursiveness of functions and how it really works. How exactly a function calls itself (with other values) when it's inside itself and how the values change.

    For example in the following code............:

    Code:
    #include "stdio.h"
    #define max(a,b) (a)>(b)? a : b
    
    int g=0;
    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=max(g,ab(n-1,a));
        
        printf("a(%d)==========%d\n",n,a);
        
        return g;
    }
    
    void main(void)
    {
    	ab(2,10);
    }
    ........... i had the impression that it should return the output:

    a=.....10
    g=.....0
    -----------------
    a=.....9
    g=.....-50
    -----------------
    a(1)========8
    a(2)========9


    But instead it returns the output:

    a=.....10
    g=.....0
    -----------------
    a=.....9
    g=.....-50
    -----------------
    a(1)========8

    a=.....9
    g=.....5
    -----------------
    a(1)========8
    a(2)========9


    Why it does that?
    Can anyone explain step by step why it prints the extra(red) lines?

  2. #2
    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.

  3. #3
    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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main returns int; not void.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Does anyone has any good book that deals specifically with functions in C?
    try Tanenbaum-data structures using c & c++.it deals with recursive functions in great detail.

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