Can a function call itself?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rage724
    New Member
    • Dec 2009
    • 1

    Can a function call itself?

    i want to do an if-else statement where the if statement would call the function again...is that possible
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Yes it is. Any function can call any other function including itself. Heck, you can even call main() from your function.

    It's your responsibility to control this so you don't call yourself forever.

    Research recursion.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Actually you can only call main in C. In C++ the standard expressly forbids it in clause 3.6.1.

      Originally posted by C++ Standard 3.6.1
      3 The function main shall not be used (3.2) within a program. The linkage (3.5) of main is
      implementation-defined. A program that declares main to be inline or static is ill-formed. The
      name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be
      called main, as can entities in other namespaces. ]

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        That only applies to an inline main() or a static main().

        A regular global main() has no restrictions.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          From the section I already quoted a program that declares main inline or ststic is ill-formed.

          Comment

          Working...