当前位置:网站首页>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
边栏推荐
- like和regexp差别
- PSYNC synchronization of redis source code analysis
- Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
- Tun model of flannel principle
- 我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
- 封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
- 每日一题-LeetCode396-旋转函数-递推
- The life cycle of key value in redis module programming
- Advanced version of array simulation queue - ring queue (real queuing)
- Llvm - generate local variables
猜你喜欢
Leetcode exercise - 396 Rotation function
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
How to design a good API interface?
LeetCode149-直线上最多的点数-数学-哈希表
What is the role of the full connection layer?
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
X509 certificate cer format to PEM format
Advanced version of array simulation queue - ring queue (real queuing)
How to use OCR in 5 minutes
Introduction to distributed transaction Seata
随机推荐
Byte interview programming question: the minimum number of K
Difference between like and regexp
我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
async关键字
大文件如何快速上传?
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
8.2 text preprocessing
Common interview questions of operating system:
Application of skiplist in leveldb
Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
Do keyword search, duplicate keyword search, or do not match
Three uses of kprobe
8.5 concise implementation of cyclic neural network
adobe illustrator 菜單中英文對照
The life cycle of key value in redis module programming
setcontext getcontext makecontext swapcontext
Detailed analysis of SQL combat of Niuke database (26-30)
Tencent has written a few words, Ali has written them all for a month
Compiling OpenSSL
January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to