当前位置:网站首页>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
边栏推荐
- fplan-电源规划
- Mobilenet series (4): mobilenetv3 network details
- A^2=e | the solution of the equation | what exactly can this equation tell us
- Il manque beaucoup de fichiers et de répertoires tels que scripts pendant et après l'installation d'anaconda3
- Stack overflow vulnerability
- PostgreSQL basic command tutorial: create a new user admin to access PostgreSQL
- fplan-Powerplan实例
- Système de collecte des journaux
- 1.5 use of CONDA
- 2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验
猜你喜欢

Matlab | drawing of three ordinate diagram based on block diagram layout

promise源码-class版本【三、Promise源码】【代码详细注释/测试案例完整】

Implementation of window encryption shell

Anaconda3安裝過程及安裝後缺失大量文件,沒有scripts等目錄

敏捷开发篇--Agile Development-自用

JMeter distributed pressure measurement

2021:Greedy Gradient Ensemble for Robust Visual Question Answering
![[array]bm94 rainwater connection problem - difficult](/img/2b/1934803060d65ea9139ec489a2c5f5.png)
[array]bm94 rainwater connection problem - difficult

resnet152 辣椒病虫害图像识别1.0
![Promise [II. Promise source code] [detailed code comments / complete test cases]](/img/ac/abf3181fa7b3345efcc9abc046cea5.png)
Promise [II. Promise source code] [detailed code comments / complete test cases]
随机推荐
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
使用promise的基本功能【四、Promise源码】
Implementation of window encryption shell
2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答
GAMES101作业7提高-微表面材质的实现过程
Promise source code class version [III. promise source code] [detailed code comments / complete test cases]
USB DRIVER
真xx相来了?测试/开发程序员为什么不愿意加班,这是个疯狂的状态......
Quickly master asp Net authentication framework identity - reset password by mail
Log collection system
Mysql database foundation: DQL data query language
再探Handler(上)(Handler核心原理最全解析)
乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。
2021:Greedy Gradient Ensemble for Robust Visual Question Answering
Cvpr2021:separating skills and concepts for new visual question answering
Learn crypto from Buu (Zhou Geng)
静态时序分析-OCV和time derate
第2章 关键技术介绍
2016Analyzing the Behavior of Visual Question Answering Models
2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering