当前位置:网站首页>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;
}
}
}
边栏推荐
- Win10无法连接打印机怎么办?不能使用打印机的解决方法
- Win7怎么干净启动?如何只加载基本服务启动Win7系统
- Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
- Software Testing Basics (Back)
- Win11怎么在右键菜单添加一键关机选项
- STM32LL库使用——SPI通信
- 实战美团Nuxt +Vue全家桶,服务端渲染,邮箱验证,passport鉴权服务,地图API引用,mongodb,redis等技术点
- Happy, 9/28 scene collection
- Win11 keeps popping up User Account Control how to fix it
- Use tencent cloud builds a personal blog
猜你喜欢

Win10 cannot directly use photo viewer to open the picture

Win7 encounters an error and cannot boot into the desktop normally, how to solve it?

Installation and configuration of Spark and related ecological components - quick recall

Use tencent cloud builds a personal blog

A clean start Windows 7?How to load only the basic service start Windows 7 system

二叉排序树与 set、map

基于矩阵计算的线性回归分析方程中系数的估计

推开机电的大门《电路》(三):说说不一样的电阻与电导

4. Publish Posts, Comment on Posts

Win10电脑不能读取U盘怎么办?不识别U盘怎么解决?
随机推荐
1.开发社区首页,注册
3. User upload avatar
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
win11一直弹出用户账户控制怎么解决
利用plot_surface命令绘制复杂曲面入门详解
jest test, component test
基于最小二乘法的线性回归分析方程中系数的估计
Detailed introduction to drawing complex surfaces using the plot_surface command
将SSE指令转换为ARM NEON指令
[System Design and Implementation] Flink-based distracted driving prediction and data analysis system
Masters and Masters
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
Win10电脑不能读取U盘怎么办?不识别U盘怎么解决?
用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
MATLAB绘图函数plot详解
Installation and configuration of Spark and related ecological components - quick recall
What are IPV4 and IPV6?
Fast advanced TypeScript
jest测试,组件测试
Software Testing Basics (Back)