当前位置:网站首页>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 .
边栏推荐
- Machine learning decision tree
- Kwai, Tiktok, video number, battle content payment
- Open graph protocol
- Ffmepg usage guide
- About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
- Looking back on 2021, looking forward to 2022 | a year between CSDN and me
- JVM garbage collection
- Judge whether the stack order is reasonable according to the stack order
- [phantom engine UE] package error appears! Solutions to findpin errors
- 长度为n的入栈顺序的可能出栈顺序
猜你喜欢
A real day for Beijing programmers!!!!!
How to realize real-time audio and video chat function
【虚幻引擎UE】运行和启动的区别,常见问题分析
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
【UNIAPP】系统热更新实现思路
Differences among 10 addressing modes
JVM garbage collection
函数(基本:参数,返回值)
Uni app common functions /api
How to get the first few pieces of data of each group gracefully
随机推荐
如何进行「小步重构」?
网络安全-记录web漏洞修复
Open graph protocol
如何实现实时音视频聊天功能
Threejs Internet of things, 3D visualization of farm (III) model display, track controller setting, model moving along the route, model adding frame, custom style display label, click the model to obt
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Seven join join queries of MySQL
C language course setting: cinema ticket selling management system
MacBook安装postgreSQL+postgis
函数(易错)
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
根据入栈顺序判断出栈顺序是否合理
How to get the first few pieces of data of each group gracefully
Burpsuite grabs app packets
The development of mobile IM based on TCP still needs to keep the heartbeat alive
Rust区块琏开发——签名加密与私钥公钥
Threejs Internet of things, 3D visualization of farms (II)
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)