当前位置:网站首页>C language explanation series -- understanding of functions (3) formal parameters, arguments, nested calls and chain access
C language explanation series -- understanding of functions (3) formal parameters, arguments, nested calls and chain access
2022-07-26 05:22:00 【Sad pig, little pig】
Review knowledge
In the previous function chapter, we shared with you two cases of using functions to solve , One is to obtain the maximum value of two integer variables through a function
#include<stdio.h>
int get_max(int x, int y)
{
return (x > y ? x : y);
}
int main()
{
int a = 10;
int b = 20;
int max = get_max(a, b);
printf("%d", max);
return 0;
}
The other is to exchange the values of two integer variables through a function
void exchange_num(int* pa, int* pb)
{
int tmp = *pa;
*pa = *pb;
*pb = tmp;
}
int main()
{
int a = 10;
int b = 20;
printf(" Exchange before : a = %d,b = %d\n", a, b);
exchange_num(&a,&b);
printf(" After exchanging :a = %d,b = %d", a, b);
}
Through these two codes, we learned what is Value transfer call What is? Address call , When we need to make certain connections inside and outside the function, we need to use Address call .
Value transfer call
The formal and actual parameters of a function occupy different memory blocks , Modification of a parameter does not affect the argument
Address call
Address calling is a way to call a function by passing the memory address of the variable created outside the function to the function parameters .
This method of parameter transfer can establish a relationship between the function and the variables outside the function , In other words, the variables outside the function can be directly manipulated inside the function
From the above knowledge, we can sum up a sentence : When a function is called , When an argument is passed to a formal parameter , The formal parameter will be a temporary copy of the argument , Therefore, the modification of formal parameters does not affect the arguments . This is the knowledge we shared with you before , Here is a simple review , It may be difficult for us to understand the words on the yellow background , What is formal parameter ? What is real parameter ? How to define ? Listen to me .
Formal parameter and actual parameter
The actual parameter is the actual parameter
The parameters actually passed to the function are called arguments
The argument can be : Constant 、 Variable 、 expression 、 Functions, etc , But no matter what type of quantity the argument is , When you make a function call , They all have to have a certain value , To pass these values to the formal parameters
Formal parameters are formal parameters
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 .
After the formal parameters are called, they are destroyed by themselves , So formal parameters are only valid in functions .
for instance
void exchange_num(int* pa, int* pb)
{
int tmp = *pa;
*pa = *pb;
*pb = tmp;
}
int main()
{
int a = 10;
int b = 20;
printf(" Exchange before : a = %d,b = %d\n", a, b);
exchange_num(&a,&b);
printf(" After exchanging :a = %d,b = %d", a, b);
}
Or this code , The actual parameter &a,&b, The formal parameter is pa,pb.
Nested calls and chained access to functions
Nested calls
As the name suggests, nested calls to functions are , Functions can be combined arbitrarily , You can call each other according to your own needs . Let's give you an example
#include<stdio.h>
int main()
{
int len = strlen("abcdef");
printf("%d\n", len);
}
As shown in the figure, we want to print the string "abcdef" The length of , We use strlen() Library function to find the length of the string, and then use len Variable reception , Finally using printf() Library functions to print , Is it too much trouble to write like this , We can use nested calls of functions , Direct will strlen() Print the value of the library function .
#include<stdio.h>
int main()
{
printf("%d\n", strlen("abcdef"));
}
As shown in the figure, the two printing results are the same , This is what we call nested function calls , Be careful Functions can be called nested , But you can't nest definitions .
Chained access
Chain access is Take the return value of one function as an argument to another function , Let's use examples to let you understand what chain access is
int main()
{
printf("%d", printf("%d", printf("%d", 43)));
return 0;
}
Let's analyze what the code outputs , We found that , We'll be back printf() The return value of the library function is the previous printf() Arguments to library functions , Then we need to know what his return value is , Last time we shared how to learn library functions , Now we can apply what we have learned
We're on the website www.cplusplus.com Inquire about printf() This library function , See the description of his return value type , He returns the number of characters printed on the screen . So we output for the first time 43, The second output is the number of characters printed on the screen 2, The third output 1, So the final result is
I don't know. After our explanation, everyone is right Nested calls and Chained access Whether our understanding has deepened .
边栏推荐
- Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
- JVM Lecture 6: how to solve the frequent FGC in online environment?
- No background, no education? Is it really hopeless for specialist testers to enter Internet factories?
- mysql如果计算本月变动/本月增幅/同比变动/同比增幅?
- Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
- Circular structure practice
- Webassembly 01 basic information
- 35. 搜索插入位置
- FPGA question brushing sequence detection
- Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)
猜你喜欢
随机推荐
Computable general equilibrium (CGE) model practice technology in resource environment under the goal of "double carbon"
没背景、没学历?专科测试员进入互联网大厂是不是真的没希望?
Test of countlaunch demo
Development to testing: a six-year road to automation from scratch
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
SQL injection
Bash shortcut key to improve command line efficiency [Full Version]
Week 6 Learning Representation: Word Embedding (symbolic →numeric)
LNMP架构
OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation
SSTI payload and various bypass methods
Excel VBA: summarize calculation output results by date (SUMIF)
pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0
DOM操作--操作节点
YOLOV3预备工作
An online accident, I suddenly realized the essence of asynchrony
Embedded sharing collection 21
kubernetes install completed
C语言力扣第42题之接雨水。四种方法——暴力、动态规划、栈、双指针
CMD operation command








