Re: Should function argument be changed in function body?



pete wrote:
> CBFalconer wrote:
>>
.... snip ...
>>
>> The only reason not to modify arguments is that the initial value
>> is required later.
>
> I don't think that's a good reason.
> If the specifcation were to suddenly change from the code example
>
> void foo1(int x)
> {
> x ++;
> printf("x+1 = %d\n", x);
> }
>
> to
> int foo1(int x);
> with the return value being the initial value of x,
> I would do it this way:
>
> int foo1(int x)
> {
> const int y = x;
>
> x ++;
> printf("x+1 = %d\n", x);
> return y;
> }

I wouldn't. What has ++ got to do with anything? I would write:

int foo1(int x) {
printf("x+1 = %d\n", x+1);
return x;
}

easily read by non C programmers and which is also easily modified
to return void. I don't believe in returning the value of entry
parameters anyhow, because that leads to such ugly foulups as:

printf("%s %s\n", b, strcat(b, a));

You will never have this foulup using strlcat.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

.



Relevant Pages

  • Re: C Programming
    ... > reason for why it matters. ... There are books around that carefully do not teach bad practice, ... this book doesn't use void main. ... is int mainor int main. ...
    (Fedora)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)
  • Re: Trying to do some signal handling ..
    ... The exit status is returned by waitin the parent process. ... > for some reason .. ... > int main ... > void dispDirFiles ...
    (comp.unix.programmer)
  • Re: Function prototype vs implementation mismatch in C++
    ... there would be to inform the user of the API (header file). ... void display(const int); ... reason a slightly different name might be more appropriate. ...
    (alt.comp.lang.learn.c-cpp)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)