当前位置:网站首页>函数(基本:参数,返回值)
函数(基本:参数,返回值)
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设置为全局变量——不建议。
边栏推荐
- NEW:Devart dotConnect ADO. NET
- ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
- C语言课设:影院售票管理系统
- Containerd series - what is containerd?
- Special Edition: spreadjs v15.1 vs spreadjs v15.0
- What is test development? Why do so many companies hire test developers now?
- After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
- How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
- 学习MVVM笔记(一)
- open graph协议
猜你喜欢
3. Package the bottom navigation tabbar
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
“金九银十”是找工作的最佳时期吗?那倒未必
As soon as I write the code, President Wang talks with me about the pattern all day
laravel8 导出Excle文件
在线文本行固定长度填充工具
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
【虚幻引擎UE】实现测绘三脚架展开动画制作
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
Behavior perception system
随机推荐
Un réveil de l'application B devrait être rapide
BDF application - topology sequence
Ctfshow 2022 Spring Festival welcome (detailed commentary)
Machine learning decision tree
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
Plasticscm enterprise crack
【虚幻引擎UE】运行和启动的区别,常见问题分析
A solution to the problem that variables cannot change dynamically when debugging in keil5
Get to know MySQL connection query for the first time
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
小程序中实现文章的关注功能
Enterprise level: spire Office for . NET:Platinum|7.7. x
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
On the day 25K joined Tencent, I cried
3. Package the bottom navigation tabbar
A應用喚醒B應該快速方法
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
Scheduling system of kubernetes cluster