[C language] Summary of function knowledge (1)

introduction

The modular programming method usually decomposes a relatively complex problem into several relatively simple sub-problems and then solves them separately to reduce the complexity of solving the problem. If you already have code that solves a certain subproblem, you can use the code directly, which is a function. A C language program consists of one or more program modules, and each program module consists of one or more functions. Therefore, functions are an indispensable and important mechanism for realizing modular programming.

1. Function overview


  • The essence of a function: a program segment that can be called repeatedly and has relatively independent functions
  • The purpose of introducing functions:
    1. To facilitate structured and modular programming
    2. To solve the duplication of code

function call
When a C language program is large in scale, it can be composed of multiple source files, but only one source file contains the main function main(), and other source files cannot contain the main function. The main function can call other functions, but not the other way around. Other functions can call each other, and the same function can be called any number of times by one or more functions. When a function calls another function, the former is called the calling function and the latter is called the called function.
  • Function classification:
    Function classification

2. Function definition

1. Definition of parameterless function

Type name function name ()
{ function body } or type name function name (void) { function body }







  • The type name specifies the type of function return value. When omitted, the default function return value type is int, and void means that the function has no parameters.
  • The function body contains a declaration part and a statement part (it can be only part of it or none of it)
  • The declaration part is mainly the declaration of variables or the declaration of the function being called.
  • The execution part consists of execution statements, and the functions of the function are implemented by these statements

2. Definition of parameterized functions

Type name function name (formal parameter list)
{ function body } formal parameter list format: type 1 formal parameter 1, type 2 formal parameter 2,..., type n formal parameter n



#include<stdio.h>
#include<math.h>
double fact(int n);//函数声明
int main()
{
    
    
	int i = 1;
	double x, item, s = 0;
	printf("输入x的值:");
	scanf("%lf", &x);//由键盘读入浮点数x的值
	item = x;
	while (fabs(item) >= 0.00001)
	{
    
    
		s = s + item;//累加一项
		i++;//累计次数
		item = pow(x, i) / fact(i);//调用函数,fact()函数中i为实参
	}
	printf("和是:%lf\n", s);
	return 0;
}
double fact(int n)//函数首部 n为形参
{
    
    
	int i;
	double jc = 1;//阶乘变量赋初值
	for (i = 1; i < n; i++)//计算阶乘
		jc = jc * i;
	return jc;//返回阶乘
}

3.Definition of empty function

type name function name ()
{ }


  • Used to declare a function, which can be a function that needs to be improved in the future.
  • The purpose is to have a clear program structure, good readability, and easy expansion of functions in the future.

3. Function call


The purpose of defining a function is to reuse it, so the function of the function can only be realized by calling the function in the program.


1. The form and process of function calling

(1) General form of function call

Function name (actual parameter list column)

  • The actual parameter list is the actual parameter, or actual parameter for short. Can be a constant, variable, expression
#include <stdio.h>

// 定义一个函数,它有三个形式参数
void print_values(int a, float b, char c) {
    
    
    printf("The values are: %d, %f, %c\n", a, b, c);
}

int main() {
    
    
    int x = 3;
    float y = 3.14;
    char z = 'A';

    // 调用函数print_values(),传递常数、变量和表达式作为实参
    print_values(1, y, 'B');
    print_values(x, 2.5, z);
    print_values(x + 1, y * 2, z + 1);

    return 0;
}

  • Separate the actual parameters with commas
  • The number and type of actual parameters should be consistent with the number and type of function parameters.
  • If it is a parameterless function, there is no actual parameter list, but the parentheses cannot be omitted.

(2) How to call functions

Ⅰ.Function statement
把函数调用作为一条语句
一般形式:函数名(实参表列);
这种方式常用于调用一个没有返回值的函数,只要求函数完成一定的操作
Ⅱ.Function expression
函数调用作为表达式中的一部分出现在表达式中,
以函数返回值参与表达式的运算
Ⅲ. Use of function nesting

The C language program starts execution from the main function, and the execution of the custom function is achieved by calling the custom function. When the custom function ends, return from the end of the custom function to the calling point in the main function and continue execution until the main function ends.

Nested function calls

C语言的函数定义是互相平行、独立的,也就是说,在定义函数时,
一个函数内不能再定义另一个函数,也就是不能嵌套定义,
但可以嵌套调用函数

(3) The process of function calling

  • 1. Allocate memory for all formal parameters of the called function, then calculate the values ​​of the actual parameters, and assign them to the corresponding formal parameters one by one (for parameterless functions, this work is not done)
  • 2. Allocate storage space for the variables defined in the function description part, and then execute the executable statements of the function in sequence. When the "return (expression);" statement is executed, the return value is calculated (if it is a function without a return value, this work will not be done).
  • 3. Release the storage space occupied by the variables defined in this function (for static type variables, the space will not be released), return to the main calling function to continue execution

2.Parameter passing


  • When calling a parameterized function, there is a data transfer relationship between the calling function and the called function.
  • The calling function transfers data to the called function mainly through the parameters of the function.
  • The called function transfers data to the calling function generally through the return statement.
  • Formal parameters are variables in parentheses after the function name when the function is defined.
  • Actual parameters refer to the constants, variables, and expressions in parentheses after the function name when calling the function.
  • When calling a function, pass the value of the actual parameter to the formal parameter so that the formal parameter is numerically the same as the actual parameter.
  • Two ways to pass parameter data: passing by value and passing by address

(1) Pass by value


  • When a function is called, the calling function passes the value of the actual parameter to the formal parameter of the called function. Changes in the formal parameter value will not affect the value of the actual parameter. This is a one-way data transfer method.
  • When the actual parameter is a variable, constant, expression, array element or function, the function uses the formula to pass the data by value.

#include <stdio.h>
void swap(int x, int y)
{
    
    
    int t;
    t = x; x = y; y = t;
    printf("x=%d,y=%d\n", x, y);
}
int main() 
{
    
    
    int a, b;
    scanf("%d,%d", &a, &b);
    swap(a, b);
    printf("a=%d,b=%d\n", a, b);
    return 0;
}

result:
Insert image description here


  • The number and data type of actual parameters should be consistent with the formal parameters, otherwise a compilation error will occur.
  • When a function is defined, the system does not allocate storage units to the formal parameters. The system only allocates storage units to the formal parameters when the function is called. After the call is completed, the storage unit occupied by the formal parameter is released.
  • Even if the actual and formal parameters have the same name, different storage units will be allocated.
  • When parameter transfer is "value transfer", it is a one-way transfer, that is, the actual parameter passes the value to the formal parameter, but the value of the formal parameter cannot be passed to the actual parameter, which means that the modification of the formal parameter will not affect the corresponding actual parameter. This is because in memory, the storage unit of the actual and formal parameters is released. After the function is executed, the storage unit of the formal parameters is released.

(2) Deliver by address


When the formal parameters of a function are of array or pointer type, parameter passing in function calls is called passing by address. Since the address is passed, the formal and actual parameters share the same storage unit. In this way, the data in the address can be directly referenced or processed through the formal parameters to achieve the purpose of changing the actual parameter value.


3. Return value of function

1. The value of a function can only be returned to the calling function through the return statement.

return 表达式;
return (表达式);//如 a>b?:a:b

Multiple return statements are allowed in a function, but logically only one return statement can be executed per call.
2. If there is no need to return a function value from the called function, the return statement can be omitted. At this time, after executing the last statement in the function body, the called function will automatically jump out and return an uncertain value. 3. The value
type of the expression in the return statement should be consistent with the type of the function in the function definition.

4. Function declaration

1. Declare first and then use
(1) When calling a library function, you generally need to use the "#include" command at the beginning of the program
(2) When calling a user-defined function, the function declaration format is:

类型名 函数名(参数类型1,参数类型2...);
类型名 函数名(参数类型1 形参1,参数类型2 形参2,...);

2. When one of the following two conditions is met, the called function does not need to be declared in the main calling function

  • When the called function is defined before the calling function
  • When the return value type of the called function is integer or character

5. Nested calls of functions

The function definitions in C language are all parallel and independent of each other. That is, when defining a function, one function cannot contain the definition of another function. If another function is called within a function, it is called a nested call of functions. In other words, during the process of the called function, another function can be called.

Guess you like

Origin blog.csdn.net/m0_74102736/article/details/130446639