当前位置:网站首页>010 C语言基础:C函数
010 C语言基础:C函数
2022-06-27 04:04:00 【入狱计划进度50%】
一:概述
函数是一组一起执行一个任务的语句。每个 C 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数。
可以把代码划分到不同的函数中。如何划分代码到不同的函数中是由您来决定的,但在逻辑上,划分通常是根据每个函数执行一个特定的任务来进行的。
函数声明告诉编译器函数的名称、返回类型和参数。函数定义提供了函数的实际主体。
C 标准库提供了大量的程序可以调用的内置函数。例如,函数 strcat() 用来连接两个字符串,函数 memcpy() 用来复制内存到另一个位置。
函数还有很多叫法,比如方法、子例程或程序,等等。
二:定义函数
return_type function_name(parameter list){
body of the function
}
在C语言中,函数由一个函数头和一个函数主体组成。
所有组成部分如下:
- 返回类型:一个函数可以返回一个值。return_type是函数返回的值的数据类型。有些函数执行所需的操作而不返回值,这种情况下,return_type是关键字void
- 函数名称:这是函数的实际名称。函数名和参数列表一起构成了函数签名。
- 参数:参数就像是占位符。当函数被调用时,您向参数传递一个值,这个值被称为实际参数。参数列表包括函数参数的类型、顺序、数量。参数是可选的,也就是说,函数可能不包含参数。
- 函数主体:函数主体包含一组定义函数执行任务的语句。
实例:
#include <stdio.h>
int max(int num1, int num2){
// 返回两个数中较大的那个
int result; // 局部变量声明
if(num1 < num2){
result = num2;
}else{
result = num1;
}
return result;
}
int main(){
int a = 100; // 定义局部变量
int b = 200;
int ret;
ret = max(a, b); // 调用函数
printf("max is : %d \n", ret);
return 0;
}
三:函数参数
如果函数要使用参数,则必须声明接受参数值的变量。这些变量称为函数的形式参数。形式参数就像是函数内的其他局部变量,在进入函数时被创建,退出时销毁。
四:参数传递的方式:
在c语言中每一个变量都有两个属性一个是值,一个是址。默认情况下,C 使用传值调用来传递参数。一般来说,这意味着函数内的代码不能改变用于调用函数的实际参数。
4.1:传值调用(传值)
该方法把参数的实际值复制给函数的形式参数。在这种情况下,修改函数内的形式参数不会影响实际参数。
实例:
#include <stdio.h>
// 定义函数
void swap(int x, int y){
int temp;
temp = x; // 临时存储x的值
x = y;
y = temp;
return ;
}
void swqp(int x, int y); // 函数声明
int main(){
int a = 100;
int b = 200;
printf("传值之前的a: %d \n", a);
printf("传值之前的b: %d \n", b);
swap(a, b); // 调用函数,在swap函数里面改变x和y的值,跟a和b本身无任何关系
printf("传值之后的a: %d \n", a);
printf("传值之后的b: %d \n", b);
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim chuanzhi.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc chuanzhi.c -o chuanzhi
┌──(rootkali)-[~/Desktop/c_test]
└─# ./chuanzhi
传值之前的a: 100
传值之前的b: 200
传值之后的a: 100
传值之后的b: 200
4.2:引用调用(传址)
该方法把参数的地址复制给形式参数。在函数内,该地址用于访问调用中要用到的实际参数。这意味着,修改形式参数会影响实际参数。
实例:
#include <stdio.h>
// 定义函数
void swap(int *x, int *y){
int temp;
temp = *x;
*x = *y;
*y =temp;
return;
}
void swap(int *x, int *y); // 函数声明
int main(){
int a = 100;
int b = 200;
printf("传值之前的a: %d \n", a);
printf("传值之前的b: %d \n", b);
swap(&a, &b); // 调用函数,&a表示指向a的指针,即变量a的地址。
printf("传值之后的a: %d \n", a);
printf("传值之后的b: %d \n", b);
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim yingyong.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc yingyong.c -o yingyong
┌──(rootkali)-[~/Desktop/c_test]
└─# ./yingyong
传值之前的a: 100
传值之前的b: 200
传值之后的a: 200
传值之后的b: 100
边栏推荐
- Nacos调用微服务两个问题:1.Load balancer does not contain an instance for the service 2.Connection refused
- IOS development: understanding of dynamic library shared cache (dyld)
- Cvpr2021:separating skills and concepts for new visual question answering
- MySql的开发环境
- Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
- LDR6028 手机设备一边充电一边OTG传输数据方案
- 实践 DevOps 时,可能面临的六大挑战
- 【promise一】promise的介绍与手撸的关键问题
- Further exploration of handler (I) (the most complete analysis of the core principle of handler)
- Argo Workflows —— Kubernetes的工作流引擎入门
猜你喜欢
Matlab | visualization of mathematical properties related to three interesting circles
[BJDCTF2020]The mystery of ip
【Unity】UI交互组件之按钮Button&可选基类总结
1.5 use of CONDA
文旅灯光秀打破时空限制,展现景区夜游魅力
2016Analyzing the Behavior of Visual Question Answering Models
如何系统学习LabVIEW?
How to make ef core 6 support dateonly type
jmeter将上一个请求的结果作为下一个请求的参数
真xx相来了?测试/开发程序员为什么不愿意加班,这是个疯狂的状态......
随机推荐
Promise [II. Promise source code] [detailed code comments / complete test cases]
Fplan layout
2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
静态时序分析-OCV和time derate
2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答
Mysql database foundation: DQL data query language
Building lightweight target detection based on mobilenet-yolov4
Cache comprehensive project - seckill architecture
[promise I] introduction of promise and key issues of hand rolling
Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
渗透测试-文件上传/下载/包含
JMeter takes the result of the previous request as the parameter of the next request
1.5 conda的使用
fplan-电源规划
2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
Kotlin compose compositionlocalof and staticcompositionlocalof
Kotlin Compose 隐式传参 CompositionLocalProvider
Log collection system
Système de collecte des journaux