Why would I pass function parameters by value in C?

江枫思渺然 提交于 2020-01-14 14:22:47

问题


I am dusting off my C skills working on some C libraries of mine. After having put together a first working implementation I am now going over the code to make it more efficient. Currently I am on the topic of passing function parameters by reference or value.

My question is, why would I ever pass any function parameter by value in C? The code might look cleaner, but wouldn't it always be less efficient than passing by reference?


回答1:


In C, all arguments are passed by value. A true pass by reference is when you see the effect of a modification without any explicit indirection at all:

void f(int c, int *p) {
  c++; // in C you can't change the original paramenter passed like this
  p++; // or this
}

Using values instead of pointers though, is frequently desirable:

int sum(int a, int b) {
    return a + b;
}

You would not write this like:

int sum(int *a, int *b) {
    return *a + *b;
}

Because it is not safe and it is inefficient. Inefficient because there is an additional indirection. Moreover, in C, a pointer argument suggests the caller that the value will be modified through the pointer (especially true when the pointed type has a size less than or equal to the pointer itself).




回答2:


Because it's not as important to code for the computer as it is to code for the next human being. If you are passing references around then any reader must assume that any called function could change the value of his parameters and would be obligated to check it or copy the parameter before calling.

Your function signature is a contract and divides your code up so that you don't have to fit the entire code base into your head in order to comprehend what is going on in some area, by passing references you are making the next guy's life worse, your biggest job as a programmer should be making the next guy's life better--because the next guy will probably be you.




回答3:


Please refer to Passing by reference in C. Pass by reference is a misnomer in C. It refers to passing the address of a variable instead of the variable, but you are passing a pointer to the variable by value.

That said, if you were to pass the variable as a pointer, then yes it would be marginally more efficient, but the main reason is to be able to modify the original variable it points to. If you don't want to be able to do this, it is recommended you take it by value to make your intent clear.

Of course, all this is moot in terms of one of Cs heavier data structures. Arrays are passed by a pointer to their first variable whether you like it or not.




回答4:


Two reasons:

  1. Often times you will have to dereference the pointer you've passed in many times (think a long for-loop). You don't want to dereference every single time you want to look up the value at that address. Direct access is faster.

  2. Sometimes you want to modify the passed-in value inside you function, but not in the caller. Example:

    void foo( int count ){
        while (count>0){
            printf("%d\n",count);
            count--;
        }
    }
    

If you wanted to do the above with something passed by reference, you would haev to create yet another variable inside your function to store it first.



来源:https://stackoverflow.com/questions/14149959/why-would-i-pass-function-parameters-by-value-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!