当前位置:网站首页>010 C language foundation: C function
010 C language foundation: C function
2022-06-27 04:24:00 【Prison plan progress 50%】
List of articles
One : summary
A function is a set of statements that perform a task together . Every C Every program has at least one function , The main function main() , All simple programs can define additional functions .
You can divide the code into different functions . It's up to you to divide the code into different functions , But logically , Partitioning is usually based on each function performing a specific task .
The function declaration tells the compiler the name of the function 、 Return types and parameters . The function definition provides the actual body of the function .
C The standard library provides a large number of built-in functions that can be called by programs . for example , function strcat() Used to connect two strings , function memcpy() Used to copy memory to another location .
There are many other ways to call functions , Such as method 、 A subroutine or program , wait .
Two : Defined function
return_type function_name(parameter list){
body of the function
}
stay C In language , A function consists of a function header and a function body .
All components are as follows :
- Return type : A function can return a value .return_type Is the data type of the value returned by the function . Some functions perform the required operations without returning values , In this case ,return_type Is the key word void
- The name of the function : This is the actual name of the function . Function name and parameter list together constitute function signature .
- Parameters : Parameters are like placeholders . When a function is called , You pass a value... To the parameter , This value is called the actual parameter . The parameter list includes the types of function parameters 、 The order 、 Number . Parameters are optional , in other words , Function may not contain arguments .
- Function main body : The function body contains a set of statements that define the function's execution tasks .
example :
#include <stdio.h>
int max(int num1, int num2){
// Returns the larger of two numbers
int result; // Local variable declaration
if(num1 < num2){
result = num2;
}else{
result = num1;
}
return result;
}
int main(){
int a = 100; // Defining local variables
int b = 200;
int ret;
ret = max(a, b); // Call function
printf("max is : %d \n", ret);
return 0;
}
3、 ... and : Function parameter
If the function uses parameters , You must declare the variable that accepts the value of the parameter . These variables are called formal parameters of functions . Formal arguments are like other local variables in a function , Created when entering a function , Destroy on exit .
Four : How parameters are passed :
stay c Every variable in the language has two properties. One is a value , One is the address . By default ,C Use value passing calls to pass parameters . Generally speaking , This means that the code within the function cannot change the actual parameters used to call the function .
4.1: Value transfer call ( Pass value )
This method copies the actual value of the parameter to the formal parameter of the function . under these circumstances , Modifying the formal parameters in a function does not affect the actual parameters .
example :
#include <stdio.h>
// Defined function
void swap(int x, int y){
int temp;
temp = x; // Temporary storage x Value
x = y;
y = temp;
return ;
}
void swqp(int x, int y); // Function declaration
int main(){
int a = 100;
int b = 200;
printf(" Before value transfer a: %d \n", a);
printf(" Before value transfer b: %d \n", b);
swap(a, b); // Call function , stay swap Change in function x and y Value , Follow a and b It has nothing to do with itself
printf(" After value transfer a: %d \n", a);
printf(" After value transfer b: %d \n", b);
return 0;
}
result :
┌──(rootkali)-[~/Desktop/c_test]
└─# vim chuanzhi.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc chuanzhi.c -o chuanzhi
┌──(rootkali)-[~/Desktop/c_test]
└─# ./chuanzhi
Before value transfer a: 100
Before value transfer b: 200
After value transfer a: 100
After value transfer b: 200
4.2: Reference call ( Byref )
This method copies the address of the parameter to the formal parameter . Within the function , This address is used to access the actual parameters to be used in the call . It means , Modifying formal parameters will affect actual parameters .
example :
#include <stdio.h>
// Defined function
void swap(int *x, int *y){
int temp;
temp = *x;
*x = *y;
*y =temp;
return;
}
void swap(int *x, int *y); // Function declaration
int main(){
int a = 100;
int b = 200;
printf(" Before value transfer a: %d \n", a);
printf(" Before value transfer b: %d \n", b);
swap(&a, &b); // Call function ,&a To point to a The pointer to , Namely variable a The address of .
printf(" After value transfer a: %d \n", a);
printf(" After value transfer b: %d \n", b);
return 0;
}
result :
┌──(rootkali)-[~/Desktop/c_test]
└─# vim yingyong.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc yingyong.c -o yingyong
┌──(rootkali)-[~/Desktop/c_test]
└─# ./yingyong
Before value transfer a: 100
Before value transfer b: 200
After value transfer a: 200
After value transfer b: 100
边栏推荐
猜你喜欢

MySql的开发环境

How to systematically learn LabVIEW?

微服务系统设计——分布式缓存服务设计

Kotlin compose compositionlocalof and staticcompositionlocalof

Argo workflows - getting started with kubernetes' workflow engine

Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan

How can e-commerce products be promoted and advertised on Zhihu?

fplan-电源规划

文旅夜游|以沉浸式视觉体验激发游客的热情

快速掌握 ASP.NET 身份认证框架 Identity - 通过邮件重置密码
随机推荐
List of best reading materials for machine learning in communication
There are two problems when Nacos calls microservices: 1 Load balancer does not contain an instance for the service 2. Connection refused
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
缓存综合项目--秒杀架构
015 C语言基础:C结构体、共用体
【B站UP DR_CAN学习笔记】Kalman滤波1
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
文旅夜游|以沉浸式视觉体验激发游客的热情
MATLAB | 三个趣的圆相关的数理性质可视化
Matlab | visualization of mathematical properties related to three interesting circles
【Unity】UI交互组件之按钮Button&可选基类总结
In a sense, the Internet has become an incubator and a parent
Kotlin compose compositionlocalof and staticcompositionlocalof
Description of replacement with STM32 or gd32
笔记本电脑没有WiFi选项 解决办法
Method of decoding iPhone certificate file
QChart笔记2: 添加鼠标悬停显示
Agile development - self use
差点因为 JSON.stringify 丢了奖金...