当前位置:网站首页>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 .
边栏推荐
- 学习MVVM笔记(一)
- How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
- 在线文本行固定长度填充工具
- Ctfshow 2022 Spring Festival welcome (detailed commentary)
- [understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
- Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
- The order of LDS links
- [Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
- Study notes 7
- lds链接的 顺序问题
猜你喜欢

陇原战“疫“2021网络安全大赛 Web EasyJaba

How to get the first few pieces of data of each group gracefully

Threejs Internet of things, 3D visualization of factory

Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates

Threejs implements labels and displays labels with custom styles
![[uniapp] system hot update implementation ideas](/img/1e/77ee9d9f0e08fa2a7734a54e0c5020.png)
[uniapp] system hot update implementation ideas

About the recent experience of writing questions

Fuel consumption calculator

The development of mobile IM based on TCP still needs to keep the heartbeat alive

American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
随机推荐
Machine learning decision tree
Ctfshow 2022 Spring Festival welcome (detailed commentary)
Online sql to excel (xls/xlsx) tool
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
3. Package the bottom navigation tabbar
FFmepg使用指南
Mixed compilation of C and CC
Three level linkage demo of uniapp uview u-picker components
Get to know MySQL connection query for the first time
[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)
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
Pyqt5 displays file names and pictures
Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
mysql的七种join连接查询
函数(易错)
provide/inject
小程序中实现文章的关注功能
What is test development? Why do so many companies hire test developers now?
Number of possible stack order types of stack order with length n