当前位置:网站首页>Introduction to C language function parameter passing mode
Introduction to C language function parameter passing mode
2022-08-02 15:34:00 【Yang Lao head soft work】
一、引言
函数是C源程序的基本模块,Specific functions can be implemented by calling function modules.在定义函数的时候,Function parameters are almost mandatory options.To be proficient in using function calls,The passing mode of function parameters must be clear.Functions can be divided into calling functions and called functions,The main calling function refers to calling other functions inside the function,and by other functions(包括主函数)The called function is called the called function.It is because of the calling function and the called function,There is also a classification of function parameters——形参和实参.当然了,Functions can be further divided into functions with parameters and functions without parameters.The following will describe the function parameter classification and parameter transmission methods respectively.
二、函数参数的分类
函数的参数可以分为实参(实际参数)和形参(形式参数).
A formal parameter is a function definition(声明)parameters that appear in parentheses after the function name.
Parameters that appear in parentheses after the function name when the function is called are called actual parameters.
需要注意的是,Function parameters are not required.
Form parameter example:
For example, there is the following function declaration:
int max( int a[], int n );
此时,max称为函数名(Also known as an interface),inside parenthesesa[]和n称为形参,因为此时数组a和变量nThere is no actual data,Hence it is called a formal parameter.
Practical example:
For example, there is the following code:
int fun( )
{
int a[] = {
1, 2, 3, 4, 9, 5 };
int n = 6;
int var = max( a, n );
......
}
此时函数funcalled the calling function,函数maxcalled the called function.函数maxarray in parenthesesa和变量n称为实参,Because the parameters at this time have already been assigned.
三、The passing mode of function parameters
Actual parameters to formal parameters“单向传递”:实参—>形参;
“值”传递.That is, when calling a function,Pass the value of the actual parameter to the formal parameter one-way.This is also done to protect the actual parameters,Avoid the actual parameter being taken away by the formal parameter.
Demo 1:Find the maximum value of an array of integers.
参考代码:
#include"stdio.h"
int max( int a[], int n );
int main()
{
int a[] = {
1, 2, 3, 4, 9, 5 };
int n = 6;
int maxV = max( a, n );//a和n为实参
printf( " max Value = %d\n", maxV );//Output an array to the screena的最大值
printf( " n = %d\n", n );//Output arguments to the screenn的值
return 0;
}
//Find an array of integersa中的最大值,a和n均是形参
int max( int a[], int n ){
int maxV, i;
maxV = a[0];
for( i = 1; i < n; i++ ){
if( a[i] > maxV ){
maxV = a[i];
}
}
n = 10; //Attempt to change parametersn的值
return maxV;
}
输出结果:
The first of this example21行n = 10;是在函数maxInternally changes the value of the formal parameter,But through no9The line statement gets the output but it isn=6,This shows that the parameters of the function are passed in one way.
Such a function parameter passing mechanism,There are troubles when it comes to actual programming,For example, if you want to get the maximum and minimum values of an integer array at the same time,Returning two values at the same time through the pattern of function return values is not acceptable.解决的方法有很多,其中常用的有:
① 返回数组,The maximum and minimum values are stored in the array;
② 返回结构体变量,The members are the maximum and minimum values;
③ Add pointer-type function parameters.CThe language adds a two-way pass-by-value mechanism for function parameters,That is to achieve bidirectional transfer of data through pointer variables:实参<—>形参.此时,The function parameter passed from the actual parameter to the actual parameter is the address of the pointer variable(Because at this time the value of the pointer is the address,仍旧是“值”传递),As long as the address of the pointer is not changed inside the function,You can change anything else,其实,You can also change the data value in the address.(This article does not discuss the method of changing the pointer address,In fact, it is a double pointer problem,依然是“值”传递).
Demo 2:Find the maximum and minimum values of an array of integers.
方法一:返回数组
参考代码:
#include"stdio.h"
#include"malloc.h"
int *max( int a[], int n );
int main()
{
int a[] = {
1, 2, 3, 4, 9, 5 };
int n = 6;
int *mV = max( a, n );//a和n为实参
printf( " max Value = %d\n min Value = %d\n", mV[0], mV[1] );
return 0;
}
//Find an array of integersa中的最大值,a和n均是形参
int *max( int a[], int n ){
int maxV, minV, i;
maxV = a[0];
minV = a[0];
for( i = 1; i < n; i++ ){
if( a[i] > maxV ){
maxV = a[i];
}
if( a[i] < minV ){
minV = a[i];
}
}
//动态分配空间,存储最大值和最小值
int *mv = ( int * )malloc( 2 * sizeof( int ) ) ;
mv[0] = maxV;
mv[1] = minV;
return mv;
}
方法二:返回结构体变量
参考代码:
#include"stdio.h"
typedef struct
{
int maxV;
int minV;
}MValue;
MValue max( int a[], int n );
int main()
{
int a[] = {
1, 2, 3, 4, 9, 5 };
int n = 6;
MValue mV = max( a, n );//a和n为实参
printf( " max Value = %d\n min Value = %d\n", mV.maxV, mV.minV );
return 0;
}
//Find an array of integersa中的最大值,a和n均是形参
MValue max( int a[], int n ){
int maxV, minV, i;
maxV = a[0];
minV = a[0];
for( i = 1; i < n; i++ ){
if( a[i] > maxV ){
maxV = a[i];
}
if( a[i] < minV ){
minV = a[i];
}
}
MValue mv;
mv.maxV = maxV;
mv.minV = minV;
return mv;
}
方法三:Add pointer-type function parameters
参考代码:
#include"stdio.h"
#include"malloc.h"
void max( int a[], int n, int *maxV, int *minV );
int main()
{
int a[] = {
1, 2, 3, 4, 9, 5 };
int n = 6;
int *maxV = ( int * )malloc( sizeof( int ) );
int *minV = ( int * )malloc( sizeof( int ) );
max( a, n, maxV, minV );//a、n、maxV和minV为实参
printf( " max Value = %d\n min Value = %d\n", *maxV, *minV );
free( maxV );
free( minV );
return 0;
}
/*Find an array of integersa中的最大值 a、n、maxV和minV均是形参 maxV和minVUsed to implement bidirectional value transfer */
void max( int a[], int n, int *maxV, int *minV ){
int i;
*maxV = a[0];
*minV = a[0];
for( i = 1; i < n; i++ ){
if( a[i] > *maxV ){
*maxV = a[i];
}
if( a[i] < *minV ){
*minV = a[i];
}
}
}
补充说明:
① 上述三个方法中,Using structs is relatively simple;
② 在上述三个方法中,函数max的参数a是数组名,Therefore, bidirectional value transfer can also be achieved,例如Demo 3.
Demo 3:Sort an array of integers from smallest to largest.
参考代码:
#include"stdio.h"
void sort( int a[], int n);
int main()
{
int a[] = {
1, 2, 3, 4, 9, 5 };
int n = 6;
sort( a, n );
for( int i = 0; i < n; i++ )
printf( "%5d", a[i] );
return 0;
}
//对数组aSort using selection sort
//参数a在函数sort被调用时,The elements passed in are the elements of the array,
//当函数sort调用结束后,aStored in is the sorted result
void sort( int a[], int n){
int i, j, t;
for( i = 0; i < n - 1; i++ ){
for( j = i + 1; j < n; j++ )
if( a[j] < a[i] ){
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
边栏推荐
猜你喜欢
轻量化AlphaPose
General syntax and usage instructions of SQL (picture and text)
Win10无法连接打印机怎么办?不能使用打印机的解决方法
4. Publish Posts, Comment on Posts
为vscode配置clangd
Based on the matrix calculation in the linear regression equation of the coefficient estimates
Introduction to MATLAB drawing functions ezplot explanation
推开机电的大门《电路》(二):功率计算与判断
5. Transaction management
IPV4和IPV6是什么?
随机推荐
Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
STM32LL library - USART interrupt to receive variable length information
Redis的线程模型
Codeforces Round #624 (Div. 3)
Mysql的锁
win11一直弹出用户账户控制怎么解决
Please make sure you have the correct access rights and the repository exists.问题解决
LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
第二十五章:一文掌握while循环
Fast advanced TypeScript
4.发布帖子,评论帖子
Lightweight AlphaPose
二叉排序树与 set、map
模板系列-并查集
Masters and Masters
1.开发社区首页,注册
使用 腾讯云搭建一个个人博客
General code for pytorch model to libtorch and onnx format
MATLAB制作简易小动画入门详解
Installation and configuration of Spark and related ecological components - quick recall