当前位置:网站首页>函数与参数
函数与参数
2022-07-17 05:08:00 【学会放下ta】
函数
C语言的函数需要先声明后定义再使用
声明(养成良好习惯,写好函数声明)
返回值类型 函数名(参数列表);
函数声明末尾分号别忘了。
定义
返回值类型 函数名(参数列表)
{
函数体;
}
其中返回值类型没有就写void,不写默认返回整型,没有参数可以不写,括号必须写,函数末尾没有分号
使用
像调用库函数一样写函数名(参数)就行
函数名(参数);
注意这里的参数是用户传入的实参,上面定义是形参
函数参数和指针
1、传值
void swap(int x,int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
功能在函数体里可以实现,但不改变函数之外的数据。调用了这个函数后x,y的值并没有改变,相当于复制了参数的值并对复制的值进行交换
2、传址
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
这样把实参的地址传入函数,函数直接对地址的数据进行操作,函数外的数据就会改变
3、引用
void swap(int &x,int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
作用和传地址一样,不过引用相当于给变量取了另一个名字,实际上还是对传入的变量进行操作
4、传数组
void print(int a[10])
{
int i;
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}
这跟只传一个数不一样,没有把整个数组拷贝进去这种传法,之前讲过,数组名相当于他的指针,因为数组名是第一个元素的地址,注意是地址,所以如果传入数组,在函数里面修改数组中某一个数的值的话是可以的。
边栏推荐
- Cityengine 3D pipe modeling tutorial
- GoFrame 错误处理的常用方法&错误码的使用
- Redis source code analysis dynamic string implementation (SDS)
- MySQL安装配置教程(超级详细)
- Markdown notes and related shortcut keys of typora
- mysql的锁
- Rxjs source code analysis (I) observable
- [ES6] explain in detail the common objects and other methods of set and array (full version)
- Performance bottleneck finding - Flame graph analysis
- Swagger configuration and use
猜你喜欢

Talk about the 8 pits of redis distributed lock

A comprehensive performance optimization, from 5 seconds to 1 second

分布式存储-fastdfs

Excel template export of easypoi

第一个智能合约程序Faucet.sol

Cesium bind mouse events and remove mouse events

Teach you to reproduce log4j2 nuclear weapon level vulnerability hand in hand

微信小程序 学习笔记

Addition and removal of cesium geojson data

Face scum counter attack: thread pool lethal serial eighteen questions, the interviewer praised me straight
随机推荐
操作系統常見面試題
单臂路由配置
Usage and examples of vlookup function
10问10答:你真的了解线程池吗?
【全网首发】一个月后,我们又从 MySQL 双主切换成了主-从
Rxjs source code analysis (I) observable
H5 page uses JS to generate QR code
GoFrame 错误处理的常用方法&错误码的使用
Opendds QoS and custom QoS (timing, timingqospolicy)
Redis source code analysis 2 iterator
面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?
Talk about the 8 pits of redis distributed lock
【全网首发】主线程异常会导致 JVM 退出?
web3js开发技术
MySQL安装配置教程(超级详细)
[first launch in the whole network] automatic analysis of JVM performance problems
Easypoi之excel简单导出
ArcMap 创建常量栅格并镶嵌至新栅格
Redis source code analysis 3 implementation of discontinuous traversal
手把手教你复现Log4j2核弹级漏洞