"C Language Elementary" Chapter 4 - Function

foreword

Dear friends, after many days, Xiaoyang is here to learn new knowledge of C language with you today: function, okay, let’s not talk nonsense, let’s go directly to the topic!


function definition

Usually when you first see a function, you will think of various functions in mathematics, but what Xiaoyang shared today is the function in C language. We know that often a big problem needs to be decomposed into many small problems to solve one by one. The same is true in C language. A certain part of the code in a large program consists of one or more statement blocks, and this certain part of the code is for 完成某种特定的功能Designed for " ", and compared with other codes, it has 一定的独立性, so it is called " 子程序(函数)"


Classification of functions

Library Functions

First of all, we have to understand what the library function is? How to use library functions?
Since we learned C language, there are many functions that we have used frequently many times:

  1. Print some kind of information on the screen (printf function)
  2. Calculate n to the kth power (pow function)
  3. Calculate the length of a string (strlen function)

The above library functions can be used by every programmer in the development process. In order to improve the portability of the program and improve the efficiency of the program, the C language provides a series of similar library functions in its basic library. It is convenient for programmers to carry out software development.

Due to the large number of library functions, it is impossible to list them all, and they can only be classified briefly:

  • IO函数
  • 字符串操作函数
  • 字符操作函数
  • 内存操作函数
  • 时间/日期函数
  • 数学函数
  • 其他库函数

share site

Knowing about library functions, what tools should we use when we want to query library functions? Here Xiaoyang gives you a website for querying library functions:
cplusplus
Simple and lightweight

usage example

#include<stdio.h>//printf函数需要的头文件
#include<math.h>//pow函数需要的头文件
int main()
{
    
    
	int a1 = 1, b1 = 2, c1 = 0;
	c1 = a1 + b1;
	printf("%d\n", c1);
	double a2 = 2.0, b2 = 4.0, c2 = 0.0;
	c2 = pow(a2, b2);//计算a2的b2次方
	printf("%lf", c2);
	return 0;
}

insert image description here
Note:
The header file corresponding to #include must be included when using the library function.


custom function

Library functions are not omnipotent. There are still many functions that we need to complete by ourselves. Otherwise, what else do programmers need to do? At this time, we can design some functions that better meet our needs to help us write programs, that's it 自定义函数.
Custom functions are the same as library functions, there are 函数名, 返回值and 函数参数.

The composition of the function

函数返回值类型 函数名 (参数1,参数2,参数3……)
{
    
    
	//...函数体
}

usage example

Find the greater of two numbers

#include<stdio.h>
int Max(int x, int y)
{
    
    
	int z = 0;
	z = x > y ? x : y;
	return z;
}
int main()
{
    
    
	int a = 0, b = 0, max = 0;
	scanf("%d%d", &a, &b);
	max = Max(a, b);
	printf("这两个数中较大值是%d", max);
	return 0;
}

Note :
In the above function max, max represents the function name, and a and b are actual parameters (actual parameters).
The name of the function must be meaningful, otherwise you will not know what it means when you look back after writing.


function parameters

The parameters of functions in C language are generally divided into two types:

  • 实参(实际参数)
    The actual parameter passed to the function is called 实参.
    实参可以是:常量,变量,表达式,函数等.
    No matter what type of quantity the actual parameters are, they must have definite values ​​when the function is called, so that these values ​​can be transferred to the formal parameters.
  • 形参(形式参数)
    Formal parameters refer to variables in parentheses after the function name, because 形式参数只有在函数被调用的过程中才实例化(分配内存单元), so called 形式参数. 形式参数当函数调用完成之后就自动销毁了.
    Therefore formal parameters are only valid within the function.

Notice:

  1. Formal parameters (formal parameters) and actual parameters (actual parameters) do not use the same space. (the address is different)
  2. After the formal parameter is instantiated, it is equivalent to a temporary copy of the actual parameter.

function call

There are two types of function calls:

  • Call by value:
    The formal parameters and actual parameters of the function occupy different memory blocks, and the modification of the formal parameters will not affect the actual parameters.
  • Call by address:
    Call by address is a way to call a function by passing the memory address of the variable created outside the function to the function parameter. This way of passing parameters can establish a real connection between the function and the variables outside the function, that is, the inside of the function can directly manipulate the variables outside the function.

Usage example:

#include<stdio.h>
void Swap1(int x, int y)
{
    
    
	int tmp = 0;
	tmp = x;
	x = y;
	y = tmp;
}

void Swap2(int* x, int* y)
{
    
    
	int tmp = 0;
	tmp = *x;
	*x = *y;
	*y = tmp;
}
int main()
{
    
    
	int a = 1, b = 2;
	Swap1(a, b);
	printf("a=%d,b=%d\n", a, b);
	Swap2(&a, &b);
	printf("a=%d,b=%d", a, b);
	return 0;
}

insert image description here
Notice:
传值调用对形参的修改不会影响实参!


Nested calls and chained access of functions

nested calls

Definition: A function calls another function

usage example

#include<stdio.h>
int Max(int x, int y)//函数1
{
    
    
	return (x > y) ? x : y;
}
void Print(int x, int y)//函数2
{
    
    
	int i = 0;
	int c = Max(x, y);
	for (i = 0; i < c; i++)
		printf("努力学习+1\n");
}
int main()
{
    
    
	int a = 0, b = 0;
	scanf("%d%d", &a, &b);
	Print(a, b);
	return 0;
}

chain access

把一个函数的返回值作为另外一个函数的参数

usage example

#include<stdio.h>
int main()
{
    
    
	printf("%d", printf("%d", printf("%d", 43)));
	return 0;
}

Supplementary Knowledge:
printf函数的返回值是打印在屏幕上字符的个数。
Analysis:

  1. printf(“%d”, 43) first prints 43, and then since the number of characters printed on the screen is 2, the return value is 2.
  2. printf("%d", printf("%d", 43) is equivalent to printf("%d", 2) → after printing 2, since the number of characters printed on the screen is 1, the return value is 1;
  3. printf("%d", printf("%d", printf("%d", 43))) so the final result is to print 4321;

Function declaration and definition

function declaration

  1. Before seeing the definition of the function, you must tell the compiler in advance what the function is called, what the parameters are, and what the return type is. Declaring a function just means that there is this function, but it is impossible to know whether there is a specific structure
  2. The declaration of the function generally appears before the use of the function, and it must satisfy先声明后使用
  3. Function declarations are generally placed 头文件in

function definition

函数的定义是指函数的具体实现,交代函数的功能实现


function recursion

程序调用自身的编程技巧称为递归

necessary conditions

  • There are restrictions, when the restrictions are met, the recursion will not continue
  • Getting closer and closer to this limit after each recursive call

usage example

Accepts an integer value (unsigned), and prints each bit of it in order (for example: input: 1234, output 1 2 3 4).

#include<stdio.h>
void Print(int x)
{
    
    
	if (x > 9)
		Print(x / 10);
	printf("%d ", x % 10);
}
int main()
{
    
    
	int a = 1234;
	Print(a);
	return 0;
}

Click on the personal business card below to add the blogger’s personal WeChat, which will make communication more convenient~

Guess you like

Origin blog.csdn.net/hsjsiwkwm/article/details/131498203