当前位置:网站首页>函数(基本:参数,返回值)
函数(基本:参数,返回值)
2022-07-05 04:16:00 【曹乐乐爱学习】
函数参数传递:
A-函数的值传递:
void fun(int a,int b);//函数声明(函数原型)
就是实参赋值给形参,在函数处理中,实参本身是不受影响的。例:
实现两个数据交换:
void swap(int ,int );
void swap(int num1,int num2)
{
int n1,n2,t;t =n1;
n1 = n2;
n2 = t;
}

原因:实参a,b传递给形参n1,n2的只是数值,而,a与n1根本就不是同一个地址空间,所以无论n1,n2如何操作,根本对a,b产生不了任何影响。

如果要解决这个问题,2种方式:
1、使用全局变量—— 不建议,因为函数尽量要模块化,尽量减少数据之间互相影响。(所谓的:高内聚,低耦合?)
2、地址传递(指针传递)——建议
只说2、地址传递:
B-函数地址传递:

这样以来,直接把a,b的地址传递进来,进行操作;实质上就是对a,b本身进行操作(说的好像是屁话)。
附加:(为了安全起见,有些时候,我们传地址,也并不是为了修改原始数据,那么可以使用const 修饰一下,只读)
比如求字符串长度:

函数的返回值
如上图,函数内求了字符串长度之后,我还需要把求得的值,传递到主函数去使用。两种方法:
1、使用全局变量——不建议
2、使用函数返回值
函数的返回值

但是要注意:

我只是加了一句,打印返回的n的值,就报错?
这是因为:函数中如果使用auto存储类型(默认不写),生命周期只存在于函数被调用期间(局部变量是放到栈上的),在函数执行完毕之后,就会释放,不能再被访问了,如果进行读写,就是非法。
而return n 之后,把函数直接赋值给变量却没问题,是因为:return的值,就是函数本身的返回值。
如果想打印出n的值,还是上面1所描述的方法:将n设置为全局变量——不建议。
边栏推荐
- How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
- Phpmailer reported an error: SMTP error: failed to connect to server: (0)
- All in one 1413: determine base
- How to get the first few pieces of data of each group gracefully
- FFmepg使用指南
- 10种寻址方式之间的区别
- 蛇形矩阵
- [untitled]
- Differences among 10 addressing modes
- 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
猜你喜欢

Rust blockchain development - signature encryption and private key public key
![[finebi] the process of making custom maps using finebi](/img/3a/d638dbac6a26c37087ec9550c35e63.png)
[finebi] the process of making custom maps using finebi

Threejs Internet of things, 3D visualization of farms (II)

Technical tutorial: how to use easydss to push live streaming to qiniu cloud?

Common features of ES6
![[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)](/img/eb/a93630aff7545c6c3b71dcc9f5aa61.png)
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)

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)

Longyuan war "epidemic" 2021 network security competition web easyjaba

kubernetes集群之调度系统

Uni app common functions /api
随机推荐
Un réveil de l'application B devrait être rapide
Online sql to excel (xls/xlsx) tool
IronXL for . NET 2022.6
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
BDF application - topology sequence
【虚幻引擎UE】实现测绘三脚架展开动画制作
[finebi] the process of making custom maps using finebi
NEW:Devart dotConnect ADO. NET
ClickPaaS低代码平台
SPI read / write flash principle + complete code
Threejs realizes rain, snow, overcast, sunny, flame
Serpentine matrix
Longyuan war "epidemic" 2021 network security competition web easyjaba
[brush questions] BFS topic selection
A solution to the problem that variables cannot change dynamically when debugging in keil5
Interview byte, pass the exam and directly work on three sides. As a result, I found an architect to hang me?
网络安全-记录web漏洞修复
长度为n的入栈顺序的可能出栈顺序
lds链接的 顺序问题
MacBook installation postgresql+postgis