|
Now we get into something I think is not good. I abhor a function or subroutine to change the contents of a passed variable. And yes, that is common too, but I think it is much worst than having to pass another variable. As a user of a function, I have to be very aware that a function is changing one or more of my variables. If I am not aware of this, I can trash my data very quickly. Consider a function that returns a value and changes a variable. Such as something like (in pseudo C syntax) /* Function to return the length of a string without leading or trailing spaces */ int StringLength(char *SomeVar); Perhaps as a side effect this function modifies SomeVar in some way. Such as Trimming it to get the length without spaces. If I am not aware of this I can do somethign like this: MyVar = ' SomeText'; MyVarLen = StringLength(MyVar); Write(OutFile, MyVar); /* Write the string to file */ Write(OutFile, MyVarLen); /* Write the TRIMed length to file */ Now, not being aware that StringLength changed MyVar and made it 'SomeText'. I now wrote 'SomeText8' or the such to the file (in my make believe Write statement). I thought I wrote ' SomeText8'. I know that, yes, I should be aware of any side effects of any functions I use. But, this is not always the case. And, yes, I have been bitten by this one before. Very difficult bug to track down. PC languages tend to be getting away from this by returning structures and not just variables. So StringLength would return a structure of struct StringLengthrReturn int TrimmedLength; char *TrimmedString; or the such, then it doesnt' have to modify the variable. Regards, Jim Langston John Taylor wrote: > Jim, > > <SNIP> > If you want use a function call, as in Jon's example: > > Eval NiceChar = Center(%Trim(MessyChar): %Size(NiceChar)) > > you're going to have to pass the size of receiving variable. To avoid this, > I just write the routine as a procedure call, so that the centering is done > within the parameter that it's passed in. For example: > > Eval NiceChar = MessyChar > CallP Center( NiceChar ) <SNIP> +--- | This is the RPG/400 Mailing List! | To submit a new message, send your mail to RPG400-L@midrange.com. | To subscribe to this list send email to RPG400-L-SUB@midrange.com. | To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com. | Questions should be directed to the list owner/operator: david@midrange.com +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.