当前位置:网站首页>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
边栏推荐
- adobe illustrator 菜单中英文对照
- Baidu written test 2022.4.12 + programming topic: simple integer problem
- LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
- Leetcode162 - find peak - dichotomy - array
- Detailed explanation of kubernetes (XI) -- label and label selector
- API gateway / API gateway (II) - use of Kong - load balancing
- 8.3 language model and data set
- 小红书 timestamp2 (2022/04/22)
- eolink 如何助力遠程辦公
- UML learning_ Day2
猜你喜欢

Have you learned the basic operation of circular queue?

T2 iCloud日历无法同步

Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search

Nuxt project: Global get process Env information

Five data types of redis

Have you really learned the operation of sequence table?

Differential privacy (background)

What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier

setcontext getcontext makecontext swapcontext

重定向和请求转发详解
随机推荐
redis-shake 使用中遇到的错误整理
Detailed explanation of MySQL connection query
Collation of errors encountered in the use of redis shake
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
win10 任务栏通知区图标不见了
MySQL InnoDB transaction
Compiling OpenSSL
OPPO数据湖统一存储技术实践
Will golang share data with fragment append
HJ31 单词倒排
Little red book timestamp2 (2022 / 04 / 22)
Detailed explanation of kubernetes (XI) -- label and label selector
C语言超全学习路线(收藏让你少走弯路)
机器学习——逻辑回归
C语言超全学习路线(收藏让你少走弯路)
Common interview questions of operating system:
Async keyword
MySQL sync could not find first log file name in binary log index file error
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
thinkphp5+数据大屏展示效果