当前位置:网站首页>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
边栏推荐
- 博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
- X509 certificate cer format to PEM format
- Llvm - generate for loop
- thinkphp5+数据大屏展示效果
- The difference between having and where in SQL
- Byte interview programming question: the minimum number of K
- Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
- 分享 20 个不容错过的 ES6 的技巧
- How to write the keywords in the cover and title? As we media, why is there no video playback
- Mysql连接查询详解
猜你喜欢

LeetCode 练习——396. 旋转函数

How to design a good API interface?

Thinkphp5 + data large screen display effect

Five data types of redis

Basic operation of sequential stack

Have you learned the basic operation of circular queue?

中富金石财富班29800效果如何?与专业投资者同行让投资更简单

Mysql连接查询详解

Tencent has written a few words, Ali has written them all for a month

每日一题-LeetCode396-旋转函数-递推
随机推荐
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
API gateway / API gateway (II) - use of Kong - load balancing
Leetcode151 - invert words in string - String - simulation
Analysis of common storage types and FTP active and passive modes
What is the role of the full connection layer?
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
LeetCode149-直线上最多的点数-数学-哈希表
Async void caused the program to crash
MySQL InnoDB transaction
About UDP receiving ICMP port unreachable
Leetcode149 - maximum number of points on a line - Math - hash table
8.4 realization of recurrent neural network from zero
MySQL Basics
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
Llvm - generate for loop
Three uses of kprobe
How to upload large files quickly?
Precautions for use of dispatching system
LeetCode151-颠倒字符串中的单词-字符串-模拟
How to design a good API interface?