当前位置:网站首页>Functions (Part I)
Functions (Part I)
2022-04-23 15:18:00 【We in the journey of the stars and the sea】
function ( The first part )
List of articles
One 、 What is a function ?
Wikipedia definition of function : Subroutines
Part of the code in a large program , Consists of one or more statement blocks . It's responsible for a particular task , And compared to other generations code , Have Relative independence .
Two 、 The function classification
( One ) Library function
classification :
. IO function
**. ** String manipulation functions
. Character manipulation functions
**. ** Memory manipulation function
**. ** Time / Date function
**. ** Mathematical functions
**. ** Other library functions
give an example 1:strcpy function :
// This is a function that can be copied , If you put arr Copy values to arr1 in .
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[] = {
0 };
char arr2[] = "ABC";
strcpy(arr1, arr2);
printf("%s", arr1);
return 0;
}
give an example 2: memset function :
It means , hold “void* ptr” after “size-t num” A number is adapted into “int main” Form or content .
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "hello world!";
memset(arr, '@', 5); // It can also be written here “arr+ A number ” Rewrite characters in different positions at will .
printf("%s", arr// It means that hello this 5 Change characters to ’@‘.
return 0;
}
** notes :** But a secret that library functions must know is : Use library functions , Must contain #include Corresponding header file .
( Two ) Custom function
Custom functions are the same as library functions , There's a function name , Return value types and function parameters . But the difference is that these are all designed by ourselves . This gives programmers a lot of room to play .
Composition of functions :
// Like an addition function
int Add(int x,int y) int Is the return type , Add For function name , In parentheses are parameters .
{
int z = x+y;
return z; // These are statement items .
}
give an example : Write a function , Find the maximum of both :
#include<stdio.h>
int get_max(int x, int y) // Use whatever type is returned here , When there is no need to return , use void It's OK .
{
return (x > y) ? x : y;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d%d", &a, &b);
int A = get_max(a, b);
printf("%d", A);
return 0;
}
give an example 2: Write a function that exchanges two numbers :
#include <stdio.h>
void exchange(int x, int y)
{
int z = 0;
z = x;
x = y;
y = z;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
exchange(a, b);
printf("%d %d", a, b);
return 0;
}
In this way, the values of two numbers cannot be exchanged .
When changing to a pointer variable :
#include <stdio.h>
void exchange(int* x, int* y)
{
int z = 0;
z = *x;
*x = *y;
*y = z;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
exchange(&a, &b);
printf("%d %d", a, b);
return 0;
}
The reason why something like this happened , Because the shape in the function participates in the influence of the argument . A formal parameter is just a temporary copy of an argument , The change of the parameter does not affect the parameter , therefore , The first method cannot realize the exchange of two numbers .
3、 ... and 、 The shape of a function takes part in an argument
【1】 The actual parameter ( Actual parameters )
The parameters that are actually passed to the function , It's called an argument .
The argument can be : Constant 、 Variable 、 expression 、 function etc. .
Whatever the type of argument is , When you make a function call , They all have to have certain values , In order to pass these values to the parameter .
【2】 Formal parameters ( Shape parameter )
Formal parameters refer to the variables in brackets after the function name , Because formal parameters are only instantiated when the function is called ( Allocate memory units ), So it's called formal parameter . Formal parameters are automatically destroyed when the function call is completed . So formal parameters are only valid in functions .
Four 、 Function call :
【1】 Value transfer call
The formal and actual parameters of a function occupy different memory blocks , Modification of a parameter does not affect the argument .
【2】 Address call
Address call is a way to call a function by passing the memory address of the created variable outside the function to the function parameter . This way of transferring parameters can establish a real connection between the function and the external variables , In other words, the variables outside the function can be directly manipulated inside the function .
therefore , To make the exchange of two numbers successful , You should use addressing to call , Using pointer variables , Let the result of internal exchange in the function be transmitted to the original code to realize the exchange of two values .
版权声明
本文为[We in the journey of the stars and the sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231508164334.html
边栏推荐
- My raspberry PI zero 2W toss notes to record some problems and solutions
- Byte interview programming question: the minimum number of K
- Redis master-slave synchronization
- 【thymeleaf】处理空值和使用安全操作符
- Sword finger offer (1) -- for Huawei
- 封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
- 免费在upic中设置OneDrive或Google Drive作为图床
- The difference between having and where in SQL
- ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
- Three uses of kprobe
猜你喜欢
Kubernetes详解(十一)——标签与标签选择器
asp. Net method of sending mail using mailmessage
Differential privacy (background)
函数(第一部分)
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
8.3 language model and data set
Sword finger offer (2) -- for Huawei
Tencent has written a few words, Ali has written them all for a month
Thinkphp5 + data large screen display effect
UML learning_ Day2
随机推荐
async void 导致程序崩溃
UML learning_ Day2
Redis master-slave synchronization
OPPO数据湖统一存储技术实践
大文件如何快速上传?
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
redis-shake 使用中遇到的错误整理
Kubernetes详解(十一)——标签与标签选择器
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
Nacos program connects to mysql8 0+ NullPointerException
JS - implémenter la fonction de copie par clic
Detailed analysis of SQL combat of Niuke database (26-30)
Mysql连接查询详解
Adobe Illustrator menu in Chinese and English
重定向和请求转发详解
Async void caused the program to crash
Subnet division of flannel principle
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
每日一题-LeetCode396-旋转函数-递推
机器学习——逻辑回归