By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,324 Members | 1,709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,324 IT Pros & Developers. It's quick & easy.

Need Help

P: 46
Hi all,

Tell me why C doesnt have Nested Function?
Please reply me....
Nov 15 '06 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
Hi all,

Tell me why C doesnt have Nested Function?
Please reply me....
Nested function? I've never programmed in a laguage with nested functions.
Nov 15 '06 #2

100+
P: 145
I wasn't sure what you meant by nested functions until I found this wikipedia entry. It reminds me a lot of a "subfunction" in matlab.

Well, C++ doesn't have nested functions, but some elements of it are here. For instance, a class can have a private function:

Expand|Select|Wrap|Line Numbers
  1. class Demo{
  2. private:
  3.  double* data;
  4.  int size;
  5.  
  6.  bool Randomize();
  7. public:
  8.  Demo();
  9.  ~Demo();
  10.  
  11.  int GetSize( void );
  12.  bool SetSize( int );
  13.  double GetData( int );
  14.  bool SetData( int , double );
  15. };
  16.  
  17. bool Demo::Randomize( void )
  18. {
  19.  for( int i=0; i < size ; i++ )
  20.  {
  21.   data[i] = (double) rand() / (double) RAND_MAX;
  22.  }
  23.  return true;
  24. }
  25.  
Here, randomize could only be called by the member functions of the class. (E.g., you could make SetData() an angry little function that scrambles the data every now and then.)

Now that I think of it, it might not be a bad idea to pub ~Demo() in the private section, since you'd never want anybody to explicitly call ~Demo(). (Although I'm not sure if there would be other ramifications to this.)

Interesting point you brought up. Thanks -- Paul
Nov 16 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.