Detailed answers to functions in C language

what is a function

In computer science, a subroutine (English: Subroutine, procedure, function, routine, method, subprogram, callable unit) is a certain part of the code in a large program, consisting of one or more statement blocks. It is responsible for completing a specific task and is relatively independent from other codes. Generally, there will be input parameters and a return value , providing the encapsulation of the process and the hiding of details . These codes are often integrated as software libraries.

Classification of Functions in C Language

  • Library functions: Functions provided inside the C language.
  • User-defined functions: Functions written by oneself.

Library Functions

When we write C language code, we always use some functions frequently:
for example:

  • Print information to the screen in a certain format (printf)
  • In the process of programming, we will frequently do some copying of strings (strcpy), and we also calculate during programming, always calculating operations such as n to the kth power (pow)...

Basic functions like the above are often used when writing programs. Therefore, the basic library of C language provides a series of similar library functions, which is convenient for programmers to develop software.
So how to learn library functions?
The use of library functions does not need to be specially remembered, we can find out how they are used.
A website and an app are recommended here

Through these methods, we can find their information, such as: function name, formal parameters, required header files and return value and other necessary information.
A brief summary, the library functions commonly used in C language are:

  • I/O function
  • String manipulation functions
  • character manipulation functions
  • memory manipulation function
  • time/date functions
  • math function
  • Other library functions

**Note: **
When using library functions , the header files corresponding to #include must be included.

custom function

A custom function is designed by the programmer independently, and has the same function name, return type, and formal parameters as ordinary functions.
The composition of the function:

ret_type fun_name(para1, * )
{
    
    
 statement;//语句项
}
ret_type 返回类型
fun_name 函数名
para1    函数参数

Use a function to find the maximum of two numbers

//使用函数求两个数的最大值
#include<stdio.h>
int get_Max(int x, int y)
{
    
    
	return (x > y ? x : y);
}
int main()
{
    
    
	int a, b;
	scanf("%d %d", &a, &b);
	int num=get_Max(a, b);
	printf("%d", num);
	return 0;
}

function parameters

Arguments

  • The parameters actually passed to the function are called actual parameters.
  • Actual parameters can be: constants, variables, expressions, functions, etc.
  • When the function is called, they must all have definite values ​​in order to pass these values ​​to the formal parameters.

keepsake

  • Formal parameters refer to the variables in parentheses after the function name , because formal parameters are only instantiated (allocated memory units) when the function is called , so they are called formal parameters.
  • The formal parameter is automatically destroyed after the function call is completed, so the formal parameter is only valid in the function.
//交换两个整数
#include<stdio.h>
void exchange(int* pa, int* pb)
{
    
    
	int temp = *pa;
	*pa = *pb;
	*pb = temp;
}

int main()
{
    
    
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	exchange(&a,&b);
	printf("a=%d,b=%d", a, b);
	return 0;
}

After the formal parameter is instantiated, it is actually equivalent to a temporary copy of the actual parameter.

function call

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 .

Nested calls and chained access of functions

Functions and functions can be combined according to actual needs, that is, they call each other.

nested calls

#include<stdio.h>
void new_line()
{
    
    
    printf("haha\n");
}
void th_line()
{
    
    
    int i = 0;
    for (i = 0; i < 3; i++)
    {
    
    
        new_line();
    }
}
int main()
{
    
    
    th_line();
    return 0;
}

Functions can be called nestedly , but definitions cannot be nested .

chain access

Using the return value of one function as the parameter of another function is called chain access.

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	char arr[20] = "hello";
	int ret = strlen(strcat(arr, "tong"));
	printf("%d\n", ret);
	return 0;
}

Function declaration and definition

function declaration

  • Tell the compiler what the name of a function is, what the parameters are, and what the return type is, but whether it exists or not, the function declaration cannot decide.
  • The declaration of a function generally appears before the use of the function. It must be declared before use.
  • Function declarations are generally placed in header files.

function definition

The definition of a function refers to the specific realization of the function, explaining the function realization of the function.

int add(int x,int y)
{
    
    
    return x+y;
}

At work, we will write the Add function as an addition module. Just call the header file of Add in the main function. Writing the code separately will make the logic clear.

function recursion

what is recursion

  • The programming technique in which a program calls itself is called recursion .
  • Recursion is widely used as an algorithm in programming languages.
  • A process or function has a method of calling itself directly or indirectly in its definition or description. It usually converts a large and complex problem layer by layer into a smaller-scale problem similar to the original problem to solve. The recursive strategy is only A small number of programs can be used to describe the multiple repeated calculations required in the problem-solving process, which greatly reduces the amount of code in the program .
  • The main way of thinking about recursion is: to make **big things into small**.

Two necessary conditions for recursion

  • There are constraints, and when this constraint is met, the recursion will not continue.
  • This limit is getting closer and closer after each recursive call.

This blog mainly introduces the definition, use, and concept of recursion of functions. The next blog will write some topics about functions to strengthen the use of functions. Let's see you next time.

Guess you like

Origin blog.csdn.net/weixin_63284756/article/details/130389926