Tom*_*Tom 12
按价值呼叫
void foo(int c){
c=5; //5 is assigned to a copy of c
}
Run Code Online (Sandbox Code Playgroud)
像这样称呼它:
int c=4;
foo(c);
//c is still 4 here.
Run Code Online (Sandbox Code Playgroud)
通过引用调用:传递指针.引用存在于c ++中
void foo(int* c){
*c=5; //5 is assigned to c
}
Run Code Online (Sandbox Code Playgroud)
像这样称呼它:
int c=0;
foo(&c);
//c is 5 here.
Run Code Online (Sandbox Code Playgroud)
返回值
int foo(){
int c=4;
return c;//A copy of C is returned
}
Run Code Online (Sandbox Code Playgroud)
通过参数返回
int foo(int* errcode){
*errcode = OK;
return some_calculation
}
Run Code Online (Sandbox Code Playgroud)
C语言不支持call-by-reference.
你可以做的是传递一个指针(它作为一个参考,但与C++称之为"引用"的不同)传递给你的函数感兴趣的数据,这使你能够完成大多数调用的事情.参考是有益的.