#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    12
    Rep Power
    0

    C and C++


    I'm a new C leaner and i'm using DEc c++ as a compiler. Sometimes i got confused between C and C++ and my textbook doest mention that problem.
    Can you guys tell me about defineing one function inside another function. E.g
    .....
    int ab(void)
    {
    int abc(void);
    int abc(void)
    {
    ;
    }
    ...
    If i compile it with C, the compiler gives a warning but still runs the prob. But with C++, it does not.
    Thank you.
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    I'm not sure about C, but I know that in C++ you cannot define a function within another function. It specifically states this as an error when you compile the program. What is the warning that you get when you compile it in C?
  4. #3
  5. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    In the future please use an appropriate subject title. See the "How to Post" sticky thread at the top of this forum for more information.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    12
    Rep Power
    0
    I'm sorry. Ididnt mean making it sticky.
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    It's not a problem, this was just a gentle nudge not to do so again. The thread I pointed you to will give you some good guidelines as to how to post an appropriate subject title.
    You can also follow the links in my signature for more information.

    Also, just curious, but why would you want to declare/define one function inside of another one?
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    12
    Rep Power
    0
    I just got that idea when studying the scope of functions and variables.
  12. #7
  13. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Typically it isn't a really good idea. Hence the reason why you get errors/warnings from it.
  14. #8
  15. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Neither language, C nor C++, permits functions within functions.
  16. #9
  17. Bad Andy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    OH
    Posts
    275
    Rep Power
    12
    Originally posted by BigBadBob
    Neither language, C nor C++, permits functions within functions.

    Yeah, thats what I thought. As it was brought up before, where would be the purpose in that? You can either delare function prototypes before he main func, or simply write the entire function above and not declare anything.
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    12
    Rep Power
    0
    So how can I limit only one function can call one specific function by not defining it within the other.
  20. #11
  21. Bad Andy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    OH
    Posts
    275
    Rep Power
    12
    I'm not sure I follow. But if I do, you can define it above, and then call it only from the function that you want to use it.
  22. #12
  23. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by haison3000
    So how can I limit only one function can call one specific function by not defining it within the other.
    Perhaps you can describe more clearly what it is you're trying to do - just what, rather than how. I can think of ways to limit access to a function but I'm not sure that it'd achieve what you're after without understanding your needs better.

IMN logo majestic logo threadwatch logo seochat tools logo