当前位置:网站首页>The C programming language (2nd) -- Notes -- 1.8
The C programming language (2nd) -- Notes -- 1.8
2022-07-27 11:28:00 【YovaVan】
1.8. Parameters —— Value transfer call
Get used to other languages ( especially Fortran) Programmers may be right C I feel strange about the way function parameters are passed in .C All function parameters of the language are “ Pass value ” Pass on . namely , The parameter values passed to the called function are stored in temporary instead of The original variable . This is different from some other languages , such as Fortran Such language is “ Call... By reference ”,Pascal Then Var Method of parameters , In these languages , The called function must access the original arguments , Instead of accessing a local copy of the parameter .
The main difference is C In language , The called function cannot directly modify the value of the variable in the calling function , You can only modify the value of its private temporary copy .
The advantages of calling by value outweigh the disadvantages . In the called function , Parameters can be considered as Easy to initialize local variables , So there are fewer extra variables to use . such , The program can be more compact and concise . as follows power Function takes advantage of this property :
/* Power: Raise base to n-th power; n >= 0; Version 2*/
int power (int base, int n)
{
int p;
for (p = 1; n > 0; --n)
p = p * base ;
return p;
}
among , Parameters n Used as a temporary variable , And through the following for Loop statement decrement , Until its value is 0, There is no need to introduce extra variables i;power Function internal pair n Any operation of the does not affect the calling function n The original parameter value of .
When necessary, , You can also let the function modify the variables in the main function . This requires the caller to provide Address of the variable whose value is to be set ( From a technical point of view , An address is a pointer to a variable ), The called function needs Declare the corresponding parameter as a pointer type , And indirectly access variables through it . See the first 5 Chapter .
If it's an array parameter , Things are different . When put Array name as parameter when , The value passed to the function is the location or address of the starting element of the array —— It does not copy the array elements themselves . In the called function , It can be done by The array subscript Access or modify the values of array elements . See the next section .
边栏推荐
- 记忆化搜索 AcWing 901. 滑雪
- Luogu p3052 [usaco12mar]cows in a skyscraper G
- What is the mystery of the gate of the meta universe?
- Digital triangle model acwing 1015. Picking flowers
- 01 BTC cryptology principle
- 求组合数 AcWing 886. 求组合数 II
- C# 自定义集合
- (8) Shell function
- Installation and use of GTEST and gmock
- 最长上升子序列模型 AcWing 1010. 拦截导弹
猜你喜欢
随机推荐
第13章 IO流
最长上升子序列模型 AcWing 482. 合唱队形
Description and feelings
(3) Pass parameters
状态压缩DP AcWing 91. 最短Hamilton路径
15 design movie rental system
Find the combination number acwing 888. find the combination number IV
Internal and external troubles of digital collection NFT "boring ape" bayc
求组合数 AcWing 889. 满足条件的01序列
Interval problem acwing 906. Interval grouping
Tensorflow tensor operation function set
Maximized array sum after 13 K negations
Ansible
Digital triangle model acwing 1027. Grid retrieval
ACM warm-up Exercise 1 in 2022 summer vacation (summary)
求组合数 AcWing 887. 求组合数 III
TensorFlow张量运算函数集
9 UAV array
(9) Shell I / O redirection
最长上升子序列模型 AcWing 1010. 拦截导弹






