Bill Jonas on Tue, 24 Sep 2002 15:10:15 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] [C QUESTION] how to tell caller of a function


On Mon, Sep 23, 2002 at 07:43:22PM -0400, Naresh wrote:
> Isn't it bad programming practice to call a function (other then main())
> inside a function?

If that were the case, you'd better not call printf(), for example, from
anything other than main(); it's a function.

Try this code snippet[1] on for size:

long long int factorial (long int number) {
  if (number == 2) {
    return 2;
  } else {
    return (number * factorial(number - 1));
  }
}

Not only are you calling functions from other functions, you have a
function calling itself.  You *can* write a factorial() function so that
it doesn't use recursion, but it would be more complicated and not
really as clear.

[1]May not be actual valid C due to simple syntax errors..

-- 
Bill Jonas    *    bill@billjonas.com    *    http://www.billjonas.com/
"They that can give up  essential  liberty to obtain a little temporary
safety deserve neither liberty nor safety."        -- Benjamin Franklin

Attachment: pgpnO3Zov9kh5.pgp
Description: PGP signature