Thread: return a function

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    59

    return a function

    Hi there!

    Could anyone tell me how to return a function back to main()??



    Cheers



    ps i have looked on the boards......

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Use a function pointer:
    Code:
    #include <iostream>
    
    typedef void (*pf)();
    
    void print()
    {
      std::cout<<"Now we're havin fun!\n";
    }
    
    pf func()
    {
      return &print;
    }
    
    int main()
    {
      func()(); // How's this for wacky syntax? :p
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Then, how's this?
    Code:
     
    void  (*func())()
    {
      return &print;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You can't return functions in c. You can in scheme and some other languages but not in c++.

    There's stuft you can do such as returning function pointers
    or function objects which are classes like

    Code:
    class C
    {
    public:
           int operator()(int n) const { printf("n = %d\n"); }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM