473,563 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should function argument be changed in function body?

Hi All,

I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}

Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.

Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?
Thanks & Regards
Nov 15 '05 #1
64 3334
Morgan Cheng wrote:
.... snip ...
Now I am confused. Is there anybody can give some explaination
why it is a code rule that argument should not be changed?


The only reason not to modify arguments is that the initial value
is required later. Any such rule is pure foolishness. You should
think of arguments as externally initialized local variables (which
is precisely what they are). For example:

int putblanks(size_ t n, FILE *f)
{
while (n--)
if (EOF == putc(' ', f)) return EOF;
return ' ';
}

--
"If you want to post a followup via groups.google.c om, 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
Nov 15 '05 #2


Morgan Cheng wrote:
Hi All,

I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}

Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.

Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?
Thanks & Regards


Yeah,changing argument values is really not a good habit.I wanna try to
explain it using asm,though I just BEGIN to learn it.
Use gcc to change the code above into asm, and look for the
differences.You
will find function arguments are pushed into 'stack' and the return
value is in %eax register.If you change the argument in the 4(%esp),it
is bad of course.Er,just so so.
If I am not right,correct this at once! I just have a try! :-(

Nov 15 '05 #3
Morgan Cheng wrote:
Hi All,

I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}

Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.

Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?


As Chuck Falconer pointed out, the only pragmatical reason _not_ to
change an argument value is that you need it later on.
One _could_ probably argue also that an argument should not be "abused"
for another purpose, e.g. using an argument green_val to store the
result of a completely unrelated computation (instead of introducing
a temporary variable with an apt name, say wobble_factor).
These are the only reasons I can come up with right now.

Making your "rule" into a mandatory rule for a coding standard is indeed
unnecessary dogmatism bordering on sheer folly.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #4
Cong Wang wrote:

Morgan Cheng wrote:
Hi All,

I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}

Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.

Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?


Yeah,changing argument values is really not a good habit.I wanna try to
explain it using asm,though I just BEGIN to learn it.
Use gcc to change the code above into asm, and look for the
differences.You
will find function arguments are pushed into 'stack' and the return
value is in %eax register.If you change the argument in the 4(%esp),it
is bad of course.Er,just so so.
If I am not right,correct this at once! I just have a try! :-(


You are not right.
This is a pseudo-efficiency, pseudo-clarity argument.
C is not assembler, so this does not play a role. At all.
<OT>
Apart from that: Where do you think variables come from/are stored to?
Translating the C code into assembler may well lead to loading an often
used argument into a register, retrieving it only from this register,
nothing forces the compiler to "leave" the variable in the stack.
</OT>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #5
CBFalconer wrote:

Morgan Cheng wrote:
... snip ...

Now I am confused. Is there anybody can give some explaination
why it is a code rule that argument should not be changed?


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 think the code is easier to read
if the computations use the parameters.

char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;

do {
*s1++ = *s2;
} while (*s2++ != '\0');
return p1;
}

If the intial value of a parameter is important,
it's easy to just pop in const qualified object.
Any such rule is pure foolishness. You should
think of arguments as externally initialized local variables (which
is precisely what they are).


--
pete
Nov 15 '05 #6
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.c om, 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

Nov 15 '05 #7
CBFalconer wrote:
The only reason not to modify arguments is that the initial value
is required later. Any such rule is pure foolishness. You should
Well, I have some industry experience. All of the code standard requires
that arguments are not to be changed. Even in embedded system
development, this rule is still required. I believe this rule is good
for code maintenance.
think of arguments as externally initialized local variables (which
is precisely what they are). For example:

int putblanks(size_ t n, FILE *f)
{
while (n--)
if (EOF == putc(' ', f)) return EOF;
return ' ';
}

Nov 15 '05 #8

Well, I have some industry experience. All of the code standard requires
that arguments are not to be changed. Even in embedded system
development, this rule is still required. I believe this rule is good
for code maintenance.


Well, I have some industry experience as well and have never come
across this in a coding standard.

Nov 15 '05 #9
Morgan Cheng wrote:
I was taught that
argument values are not supposed to be changed in function body.
Say, below code is not good.

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

should be "refactor-ed" to be

void foo2(int x) {
int y = x;
++y;
printf("x + 1 = %d\n", y);
}
This is *not* refactoring. You changed the API.
Recently, I found one guy posted code like foo1 in one C lang forum.
I pointed out that it is not good code standard.
Surprisingly, it seems that everybody criticised me as dogmatic.
It is claimed that changing arugment value will not affect caller code
and it saves stack size. I understand their point
but I *do* remember that it is commmon that
argument is not suppsoed to be changed.

Now I am confused. Is there anybody who can give some explaination,
"Why it is a code rule that argument should not be changed?"


There are several possible sources of confusion.
First, a function should *not* modify any of its *actual* arguments.
Instead of

void f(int* p) {
++(*p);
}

you should write

int f(const int* p) {
return *p + 1;
}

The example that you have provided passes by value
so it cannot modify its actual argument.
The *formal* argument is a copy of the *actual* argument.
If the *formal* argument should not be modified,
you should have written

void foo2(const int x) {
int y = x;
++y;
printf("x + 1 = %d\n", y);
}

Strictly speaking, it is up to the programmer
to decide whether [formal] arguments passed by value
are local constants or local variables.
Generally, you will *not* want to make a copy

void foo3(const HugeObjectType* p) {
// Use *p but don't modify it.
}

Your shop may enforce a rule
that generally forbids modifying formal arguments
to avoid accidently modifying actual arguments
(arguments passed by reference [through a pointer])
but objects must sometimes be modified *in-place*

HugeObjectType* foo4(HugeObject Type* p) {
// Modify *p
return p;
}

For example

char *strcpy(char *dest, const char *src);
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
17489
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which...
3
14916
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
50
3286
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? "None" doesn't seem to work.. Thanks in advance.
28
4293
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
7658
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7877
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7631
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7943
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.