当前位置:网站首页>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 .

原网站

版权声明
本文为[Jiangxueqian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220503534008.html