home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,437 of 33,346   
   Francis Glassborow to camster02813@yahoo.com   
   Re: passing uninitialized arguments   
   31 Aug 11 17:36:17   
   
   From: francis.glassborow@btinternet.com   
      
   On 31/08/2011 22:55, camster02813@yahoo.com wrote:   
   >     I know i should understand how this code works,   
   > but...............   
   > I see how int "number"  is passed to the function "Factor" after being   
   > input from the keyboard (console)-   
   > However&"squared" and&"cubed"  are passed uninitialized from   
   > main......and every thing does work as it should.   
   > Why dont the uninitialized values cause problems???- simply because   
   > they being passed as references???   
   They aren't their addresses are being passed to pointers so the function   
   knows where to store the answers.   
      
   > code as follows is from "Teach Yourself C++in 21 Days " by J. Liberty:   
      
   First let me say that IMO this is a truly dreadful piece of C++ code :)   
   and if it really is from that book I would take the book back and demand   
   my money back as the book is unfit for purpose. I am serious, until   
   readers refuse to accept junk books publishers will continue to print   
   them. The code you show works and that is the only thing that can be   
   said for it. It is not how any programmer, novice or expert should write   
   it even as a tutorial. How will newcomers acquire good coding habits if   
   they are presented with such poor examples to follow?   
      
   >   
   > #include   
   > using namespace std;   
   >   
   > short Factor(int , int *, int *)   
      
   I would have greatly have preferred to see   
   1) a bool as the return type   
   2) some parameter names to act as comments   
   3) functions doing only one thing. When we examine the definition we   
   will discover that this function does input checking, generates a suare   
   in an out parameter and a cube in a second out parameter.   
      
   I would have written three functions one for each task:   
      
   bool check_in_bounds(int input_value, int lower_bound, int upper_bound){   
      if(input_value upper_bound) return false;   
      return true;   
   }   
      
   void square(int input_value, int & destination){   
        destination = input_value * input value;   
   }   
      
   void cube(int input_value, int & destination){   
       destination = input_value * input value * input_value;   
   }   
      
   Actually I would not use an output parameter but I am assuming the   
   author wants to introduce that idea. But even if he does I would not   
   dream of using pointers in C++ (that is a C'ism that is unnecessary in   
   C++ and only adds obfuscation)   
      
      
      
   >   
   > int main()   
   > {   
   >    int number, squared, cubed;   
   >    short error;   
   Why short rather than bool?   
   >   
   >    cout<<" Enter a number (0 - 20): ";   
   >    cin>>  number;   
   >   
   >    //cout<<" squared is ???"<    //cout<<" cubed is ???"<   
   >    error = Factor(number,&squared,&cubed);   
      
   Note that he is explicitly taking the address of squared and cubed which   
   is the C way to pass a location to a function to use as an out parameter.   
      
   >   
   >    if (! error)   
   >    {   
   >        cout<<" number"<        cout<<" square"<        cout<<" cubed"<    }//end if  A completely unnecessary comment, the formatting guides you   
   >    else   
   >        cout<<" Error enconterd !!/n";   
   // Why does he not provide and informative error message and why does he   
   not use endl this time? Inconsistency is unhelpful.   
   >    return 0;   
   > }   
   > short Factor(int n, int *pSquared, int *pCubed)   
   Stupid function name (it has nothing to do with a factor) There is also   
   no real advantage in those p prefixes to the parameter names.   
      
   > {   
   >    short value = 0;   
   ridiculous variable name. Why not something like 'error_found'   
   >    if (n>  20)   
      
   Does not check the lower bound   
   >        value = 1;   
   >    else   
   >    {   
   >        *pSquared = n*n;   
   >        *pCubed = n * n * n;   
   >        value = 0;   
   but you can only get here if value is 0 so why that line? It is   
   superfluous and so confusing.   
      
   >    }   
   >    return value;   
   >   
   > }   
   > Thanks much,   
   > Cam Lowes   
   >   
   >   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca