当前位置:网站首页>C language ---18 function (user-defined function)
C language ---18 function (user-defined function)
2022-06-24 14:17:00 【Try!】
Before this record , First of all, let's make a point , That's it The main function is also a function .
1、 Composition of custom functions
I learned about library functions before , I learned that Library functions are the return types of containing functions 、 Function name and function parameter . for instance ( Let's use the previous example ), Copy of the contents of the array , The code is as follows :
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = {
0 };
char arr2[] = "abc";
strcpy(arr1,arr2);
printf("%s\n",arr1);
return 0;
}
This code is to pass the library function strcpy Implement the array 2 Copy the contents of to the array 1, Before using library functions , You need to query the header files required for the use of library functions .
strcpy Format of library functions :char *strcpy(char *strDestination,const char *strSource)
among ,strcpy Is the function name ;(char *strDestination,const char *strSource) It's a function parameter ;char * Is the return type of the function . Custom functions are the same as library functions , There's a function name 、 Return value type and function parameters . But the difference is that these are designed by ourselves , It will give us a lot of room to play , As the saying goes “ There are a thousand Hamlets in a thousand people's eyes ”, Everyone has their own style of writing code .
Composition of custom functions :
ret_type fun_name(para1, *)
{
statement;// Statement item
}
ret_type Return type
fun_name Function name
para1 Function parameter
{
} Inside is the structure
2、 Examples of custom functions
Example 1 : Write a function to find the maximum value of two integers
Definition of function
int get_max(int x, int y)
{
int z = 0;
if (x > y)
z = x;
else
z = y;
return z;// return z-- Returns the larger of the two ,z The type of is integer , therefore get_max The type of is also an integer
}
int main()
{
int a = 10;
int b = 20;
// Function call
int max = get_max(a,b);
printf("max = %d\n",max);
return 0;
}
The operation results are as follows :
max = 20
Example 2 : Write a function that can exchange the contents of two integer variables
The code is as follows :
void Swap(int x, int y)
// Only need to a as well as b Just swap the values of , There is no need to return a value
// Some functions when things are done , Nothing needs to be returned , Write the return type of the function as void
// The return type of the function is written as void, Indicates that this function does not return any value , There's no need to go back
{
int tmp = 0;
tmp = x;
x = y;
y = tmp;
}
int main()
{
int a = 10;
int b = 20;
// Write a function , Exchange the values of two integer variables
printf(" Exchange before :a=%d b=%d\n", a, b);
Swap(a,b);
printf(" After exchanging :a=%d b=%d\n", a, b);
return 0;
}
Run it to see the results :
Exchange before :a=10 b=20
After exchanging :a=10 b=20
Obviously , There's a problem with the program , Did not a and b The value of , Let's analyze the code .




Through the monitoring window, you can know a and b These two spaces and x and y These two spaces are completely independent , So for x and y The change will not affect a and b Of . The place where the function is called has nothing to do with the two variables inside the function , So it won't affect each other .
That's when it's used “ The pointer ” Knowledge points of , Any variable needs space in memory . Let's review our knowledge of pointers .
int main()
{
int a = 10;// At this point, it is equivalent to opening up four bytes of space in the memory
// The four byte space has an address ,&a You can get its address ; take a Save your address , Put it in a variable pa Inside
//pa The type of int*, Inside pa It's a pointer variable .
int* pa = &a;//pa It's a pointer variable
// hold a The address of is entrusted to pa,pa And a There is a connection between , You can go through *pa To find the a To operate
//*pa;// adopt pa Address inside , To find the pa Object referred to (*pa Is refers to a)
*pa = 20;// This is the time a The value of becomes 20 Add a sentence “printf("%d\n",a)” To verify the results
printf("%d\n", a);
return 0;
}
The operation results are as follows : You can see a The value of is changed to 20 了 .
20
//*pa;// adopt pa Address inside , To find the pa Object referred to (*pa Is refers to a) According to the nature of the pointer , Correct the error code just now , The code is as follows :
void Swap(int* pa, int* pb)
//a After you give me your address , The address is an integer address , So it's better to take an integer pointer to receive the parameter
{
//*pa It is the following that is accessed a, *pb It is the following that is accessed b
int z = 0;
z = *pa;// here z What's in it is a Value
*pa = *pb;//a It's in there b Value
*pb = z;//b What's in it is a
}
int main()
{
int a = 10;
int b = 20;
// Write a function , Exchange the values of two integer variables
printf(" Exchange before :a=%d b=%d\n", a, b);
Swap(&a,&b);// take a and b Give this function the address of
printf(" After exchanging :a=%d b=%d\n", a, b);
return 0;
}
The operation results are as follows :
Exchange before :a=10 b=20
After exchanging :a=20 b=10
Successfully a and b The values of are exchanged .
3、 Function parameter
The parameters of a function are divided into formal parameters and actual parameters .
The actual parameter ( Actual parameters ): The parameters actually passed to the function are called arguments . The argument can be : Constant 、 Variable 、 expression 、 Functions, 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 .

Formal parameters ( Shape parameter ): Formal parameters refer to the variables in brackets after the function name , Because formal parameters are instantiated only 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 ( namely : Its life cycle is in function calls ), So formal parameters are only valid in functions .
In example 2 ,x、y、pa、pb Are formal parameters ; stay main Function passed to Swap Medium a、b as well as &a、&b Is the actual parameter . stay Swap When a function is called , The actual parameter is passed to the formal parameter . In fact, a formal parameter is a temporary copy of an argument . That is to say, in the wrong part of the code ,x and y It's just a copy a and b Value , Create an independent space for it , Their addresses are different . So changing the formal parameter will not affect the argument , Naturally in that code a and b The value of is not exchanged .
边栏推荐
- 第八章 操作位和位串(四)
- Method of establishing unity thermodynamic diagram
- v-if 和 v-show 的区别
- 【深度学习】NCHW、NHWC和CHWN格式数据的存储形式
- Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
- 10_那些格调很高的个性签名
- HarmonyOS.2
- Jerry's serial port receiving IO needs to set the digital function [chapter]
- 二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
- PgSQL queries the largest or smallest data of a field in a group
猜你喜欢

数字臧品系统开发 NFT数字臧品系统异常处理源码分享

One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?

box-sizing

v-for 中 key的作用和原理

Overview of SAP marketing cloud functions (III)

Keras深度学习实战(11)——可视化神经网络中间层输出
![Maximum path sum in binary tree [handle any subtree, then handle the whole tree]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]
![根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]](/img/f7/8d026c0e4435fc8fd7a63616b4554d.png)
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]

【无标题】

Solution of channel management system for food and beverage industry: realize channel digital marketing layout
随机推荐
10_那些格调很高的个性签名
Convolution kernel and characteristic graph visualization
数字臧品系统开发 NFT数字臧品系统异常处理源码分享
Idea connection MySQL custom generated entity class code
Jerry's seamless looping [chapter]
Harmony os. (2)
Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]
**Puzzling little problem in unity - light and sky box
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
[deep learning] storage form of nchw, nhwc and chwn format data
文本对比学习综述
leetcode:1504. 统计全 1 子矩形的个数
2022 coal mine gas drainage operation certificate examination questions and simulation examination
Operation of simulated examination platform for examination questions of coal production and operation units (safety production management personnel) in 2022
CONDA and pip commands
Kunpeng arm server compilation and installation paddlepaddle
Can a team do both projects and products?
Jerry's infrared filtering [chapter]
Jupiter notebook operation