当前位置:网站首页>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
边栏推荐
- How to design a good API interface?
- Async keyword
- LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
- Daily question - leetcode396 - rotation function - recursion
- Have you learned the basic operation of circular queue?
- Differential privacy (background)
- Tencent has written a few words, Ali has written them all for a month
- What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
- X509 certificate cer format to PEM format
- adobe illustrator 菜單中英文對照
猜你喜欢
How to design a good API interface?
函数(第一部分)
Differential privacy (background)
Lotus DB design and Implementation - 1 Basic Concepts
机器学习——逻辑回归
LeetCode149-直线上最多的点数-数学-哈希表
每日一题-LeetCode396-旋转函数-递推
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
Wechat applet customer service access to send and receive messages
Analysis of common storage types and FTP active and passive modes
随机推荐
LeetCode162-寻找峰值-二分-数组
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
The wechat applet optimizes the native request through the promise of ES6
Difference between like and regexp
MySQL Basics
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
Share 3 tools, edit 5 works at home and earn more than 400
adobe illustrator 菜单中英文对照
Analysis of common storage types and FTP active and passive modes
Alexnet model
Kubernetes详解(九)——资源配置清单创建Pod实战
Sword finger offer (2) -- for Huawei
JS -- realize click Copy function
分享3个使用工具,在家剪辑5个作品挣了400多
JUC learning record (2022.4.22)
Kubernetes详解(十一)——标签与标签选择器
8.3 language model and data set
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
Subnet division of flannel principle
牛客网数据库SQL实战详细剖析(26-30)