当前位置:网站首页>Some considerations of C language custom function
Some considerations of C language custom function
2022-06-22 05:08:00 【Jiangxueqian】
The parameters of the function
In the function declaration , The formal parameter name can be omitted , And write only the type of formal parameter . Because the compiling system only cares about and checks the number and type of parameters , Without checking the parameter name , When calling a function, you only need to ensure that the actual parameter type is consistent with the formal parameter type , Regardless of what the parameter name is .
The argument of a function can be a constant 、 Variable or expression , But they are required to have definite values . for example max(3, a+b); Assign the value of an argument to a formal parameter when called ( Value passed , And it is one-way transmission , Arguments and formal parameters occupy different storage units in memory ), Arguments and formal parameters data type Should be the same or Assignment compatibility .
When using Array name When doing function arguments , Instead of passing the value of an array element to a formal parameter , Instead, the arguments The address of the first element of the array Pass to parameter array , So the two arrays are Share in The same memory unit . In this case, if the value of the element in the formal parameter array changes , It will also change the values of real parameter group elements at the same time .
When using multidimensional array names as function parameters , Function can specify the size of each dimension , The size description of the first dimension may also be omitted , but The size description of the second dimension and other high dimensions cannot be omitted .
void(int array[2][2]){
} // legal
void(int array[][2]){
} // legal , And is equivalent to the first
void(int array[][]){
} // illegal
Functions and global variables
You can use local variables in a function , You can also use valid global variables .
Due to... In the same file All functions Can reference the value of a global variable , So if you change the value of a global variable in a function , will influence To other functions that use this global variable . Also changes the value of the global variable . Because the function call can only bring back one function return value , Therefore, sometimes global variables can be used to increase the connection channel between functions , More than one value can be obtained through function call .
#include <stdio.h>
float Max = 0, Min = 0; // Global variables
float average(float array[], int n)
{
float aver;
float sum = array[0];
Max = Min = array[0];
for (int i = 0; i < n; i++)
{
if (array[i] > Max)
{
Max = array[i]; // Modify global variables
}
if (array[i] < Min)
{
Min = array[i]; // Modify global variables
}
sum = sum + array[i];
}
aver = sum / n;
return aver;
}
int main(void)
{
float score[10] = {
88,87,89,91,93,86,90,81,83,85 };
float ave;
ave = average(score, 10);
printf("max = %4.2f min = %4.2f average = %4.2f", Max, Min, ave);
}
If it's in the same source file , Global variables have the same name as local variables , In this case, it is within the scope of the local variable , Local variable valid , Global variables are “ shielding ”, It doesn't work .
The principle of program design requires that each module has “ Strong cohesion ”, And with other modules “ Low coupling ”. Global variables do not conform to this rule , So you should avoid using global variables .
Internal and external functions
Functions are global in nature , We define a function to call it in other functions . If not stated , A function in a source file can be This document Other function calls in , Can also be Other documents Function call in . however , It's fine too Appoint Some functions cannot be called by other files .
Depending on whether the function can be called by other source files , Distinguish functions into Internal function and External function .
External function
If in Defined function when , Add the keyword at the leftmost end of the function header extern, Then this function is an external function , It can be called by other files .
But we know that , Functions can be called by external files , Because when we define a function , If Don't specify Is a function an internal function or an external function , System Auto default Function is External function .
//file1.c file
#include <stdio.h>
void fun()
{
printf(" I am a fun function , come from file1.c\n");
}
//main.c file
#include <stdio.h>
extern void fun(); // Declare functions , Equivalent to void fun();
int main()
{
fun(); // I am a fun function , come from file1.c
return 0;
}
According to the above example, we can see , Use extern Declaration can call functions defined in other files in this file , Or put the function's Scope Extended to this document .
Because the function itself is external by default , So you can omit... When declaring a function extern.
Internal function
If a function Only by this document Other function calls in , It's called Internal function . When defining internal functions , Add... Before the function name and function type static keyword , namely :
static void fun()
{
}
Internal functions are also called Static functions , Of static functions Scope Only limited to the file , And priority Higher than the external function with the same name . such , Even if there are internal functions with the same name in different source files of the program , They don't interfere with each other .
example :
//file1.c file
#include <stdio.h>
void fun()
{
printf(" I am a fun function , come from file1.c\n");
}
//main.c file
#include <stdio.h>
static void fun()
{
printf(" I am the Lord ");
}
extern void fun(); // Declared an external function , But it didn't work , It can be said that static Shield it .
int main()
{
fun(); // I am the Lord
return 0;
}
The purpose of function declaration
The function declaration can be understood from the external and internal functions .
Use Function declaration Be able to put the Scope Extend to the full range of the current file ( That is, the definition of the called function is located under the function that calls it ), Or extend it beyond the file that defines the function , Just include the declaration of the function in every file that uses the function .
The function declaration informs the compiling system : This function is defined later in this document , Or defined in another file .
边栏推荐
- 6. Local - custom filter factory
- Solve the shortage of developers. Maybe it can help you
- Flink deployment mode (I) - standalone and Application
- QT save qtextedit memory to Txt file
- Great! Huaibei and Huaibei enterprises are approved to use special marks for geographical indication products
- go学习(二、内建容器)
- Web page design and production final assignment report - College Students' online flower shop
- 并发编程——线程池
- 招贤纳士-第23期
- Concurrent programming - thread pool
猜你喜欢

When idea creates a method, it uses annotations to prompt method parameters (param), return value (return), and method function (description)

VirtualBox 6.1.34 release

Understanding of service container, service provider and facade of laravel

Detailed explanation of deep learning technology for building an image search engine that can find similar images

Progress warning and problem management of progress control in Zhiyuan project management SPM system

8. Gateway request logging

Postman uses (reads) JSON files for batch testing

Remote Dictionary Server(Redis)——基于 KV 结构的作为 Cache 使用的 NoSQL 数据库管理系统
![[fault diagnosis] stitch Py script failure](/img/5c/e5df21674b5d0677484b49a608f8ae.png)
[fault diagnosis] stitch Py script failure

Accelerate the promotion of industrial Internet, and map out a new blueprint for development
随机推荐
What is the difference between TOC and Tob
Golang concise architecture practice
【chrome】 谷歌小技巧 谷歌浏览器 自带 滚动截图(全屏截图)
NFC authentication makes the ultimate KYC experience
【故障诊断】cv2.imwrite无法写入图片,但程序就是不报错
Exness: ECB president Christine Lagarde reiterated her plan to raise interest rates at the July meeting
Web page design and production final assignment report - College Students' online flower shop
Arrangement of soft test subjects in the second half of 2022
Detailed explanation of deep learning technology for building an image search engine that can find similar images
MySQL day01 class notes
Target detection algorithm based on deep learning interview essential (rcnn~yolov5)
MySQL day02 class notes
Idea创建方法时,使用注解提示方法参数(param)、返回值(return)、方法作用(Description)
使用选择法对十个整数进行排序
Remote Dictionary Server(Redis)——基于 KV 结构的作为 Cache 使用的 NoSQL 数据库管理系统
Data backup and recovery
89--- Dirac delta function
QT save qtextedit memory to Txt file
go学习(一、基础语法)
flink部署模式总结