当前位置:网站首页>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 .
边栏推荐
- IVR在voip电话系统的应用与价值
- 测试用例评审如何开展
- 87. 扰乱字符串
- Yuancosmos provides a digital social platform for fashion design display
- Teach you how to use code to realize SSO single sign on
- STL常用模板库
- 动态内存管理及柔性数组
- DOM event flow event bubble event capture event delegate
- Why is the value represented by a negative number greater than an integer by 1?
- ThreadLocal transfer between parent and child threads in asynchronous
猜你喜欢

Ansible中常用的模块

The first positive number missing in question 41 of C language. Two methods, preprocessing, fast sorting and in situ hashing

C语言-指针进阶

SSTI payload and various bypass methods

pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0

ALV program collection

no networks found in /etc/cni/net.d

攻防世界--easy_web

Compilation method of flood control evaluation report and flood modeling under the new guidelines

Week 6 Learning Representation: Word Embedding (symbolic →numeric)
随机推荐
Uniapp applet framework - a set of code, multi segment coverage
517. 超级洗衣机
C language function
Excel VBA: realize automatic drop-down filling formula to the last line
List converted to tree real use of the project
DOM事件流 事件冒泡-事件捕获-事件委托
动态内存管理及柔性数组
Earth system model (cesm) practical technology
Chinese character style transfer --- learn the conversion and generation of one to many programmed Chinese characters through generation confrontation network
Hack The Box -SQL Injection Fundamentals Module详细讲解中文教程
TZC 1283: simple sort Bubble Sort
SSTI-payload和各种绕过方法
如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation
C语言函数
No background, no education? Is it really hopeless for specialist testers to enter Internet factories?
第二讲 初识SLAM
FPGA刷题——序列检测
nacos注册中心
Week 6 Learning Representation: Word Embedding (symbolic →numeric)