当前位置:网站首页>Function (basic: parameter, return value)
Function (basic: parameter, return value)
2022-07-05 04:18:00 【Cao Lele loves learning】
Function parameter passing :
A- The value transfer of a function :
void fun(int a,int b);// Function declaration ( The function prototype )
Is to assign an argument to a formal parameter , In function processing , Arguments themselves are unaffected . example :
Realize two data exchanges :
void swap(int ,int );
void swap(int num1,int num2)
{
int n1,n2,t;t =n1;
n1 = n2;
n2 = t;
}
reason : Actual parameters a,b Pass it to the formal parameter n1,n2 Only numerical value , and ,a And n1 It's not the same address space at all , So no matter n1,n2 How to operate , Not at all a,b It won't make any impact .
If we want to solve this problem ,2 Ways of planting :
1、 Use global variables —— Don't suggest , Because functions should be modularized as much as possible , Minimize the interaction between data .( So-called : High cohesion , Low coupling ?)
2、 Address delivery ( Pointer passing )—— Suggest
Just say 2、 Address delivery :
B- Function address passing :
Since then , Put... Directly a,b Pass in your address , To operate ; It's essentially right a,b Operate on your own ( It seems to be bullshit ).
additional :( For safety's sake , Sometimes , We send the address , Nor is it to modify the original data , Then you can use const Decorate it , read-only )
For example, find the string length :
The return value of the function
Pictured above , After finding the length of the string in the function , I also need to calculate the value , Pass it to the main function to use . The two methods :
1、 Use global variables —— Don't suggest
2、 Use function to return value
The return value of the function
But be careful :
I just added , Print the returned n Value , Just report a mistake ?
This is because : Function if you use auto Storage type ( Do not write by default ), The life cycle only exists during the function being called ( Local variables are put on the stack ), After function execution , Will release , Can no longer be visited , If reading and writing , It's illegal .
and return n after , It's OK to assign a function directly to a variable , Because :return Value , Is the return value of the function itself .
If you want to print out n Value , Or above 1 The method described : take n Set to global variable —— Don't suggest .
边栏推荐
- [phantom engine UE] the difference between running and starting, and the analysis of common problems
- Threejs Internet of things, 3D visualization of factory
- Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
- 【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
- As soon as I write the code, President Wang talks with me about the pattern all day
- Use threejs to create geometry and add materials, lights, shadows, animations, and axes
- File upload bypass summary (upload labs 21 customs clearance tutorial attached)
- 在线文本行固定长度填充工具
- Clickpaas low code platform
- Threejs realizes rain, snow, overcast, sunny, flame
猜你喜欢
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
防护电路中的元器件
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
Use threejs to create geometry and add materials, lights, shadows, animations, and axes
MacBook安装postgreSQL+postgis
蛇形矩阵
Containerd series - detailed explanation of plugins
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
随机推荐
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
Judge whether the stack order is reasonable according to the stack order
Uni app change the default component style
阿里云ECS使用cloudfs4oss挂载OSS
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
3. Package the bottom navigation tabbar
About the recent experience of writing questions
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
[uniapp] system hot update implementation ideas
“金九银十”是找工作的最佳时期吗?那倒未必
函数(基本:参数,返回值)
mysql的七种join连接查询
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
EasyCVR更改录像存储路径,不生成录像文件如何解决?
网络安全-记录web漏洞修复
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
Containerd series - detailed explanation of plugins
[thingsboard] how to replace the homepage logo