当前位置:网站首页>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 .
边栏推荐
- File upload bypass summary (upload labs 21 customs clearance tutorial attached)
- 这是一个不确定的时代
- [brush questions] BFS topic selection
- [phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
- Ctfshow 2022 Spring Festival welcome (detailed commentary)
- MacBook installation postgresql+postgis
- 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
- Mixed compilation of C and CC
- Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
- 网络安全-记录web漏洞修复
猜你喜欢
kubernetes集群之调度系统
laravel8 导出Excle文件
Kwai, Tiktok, video number, battle content payment
Serpentine matrix
Rust blockchain development - signature encryption and private key public key
防护电路中的元器件
Interview related high-frequency algorithm test site 3
Containerd series - detailed explanation of plugins
How to get the first few pieces of data of each group gracefully
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)
随机推荐
How to realize real-time audio and video chat function
Interview byte, pass the exam and directly work on three sides. As a result, I found an architect to hang me?
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
All in one 1413: determine base
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)
Learning notes 8
[brush questions] BFS topic selection
指针函数(基础)
Common features of ES6
The development of mobile IM based on TCP still needs to keep the heartbeat alive
WGS84 coordinate system, web Mercator, gcj02 coordinate system, bd09 coordinate system - brief introduction to common coordinate systems
学习MVVM笔记(一)
【FineBI】使用FineBI制作自定义地图过程
This is an age of uncertainty
Containerd series - what is containerd?
Is "golden nine and silver ten" the best time to find a job? Not necessarily
Rome链分析
A real day for Beijing programmers!!!!!
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Threejs realizes rain, snow, overcast, sunny, flame