C language -- function

Posted by myanavrin on Sat, 08 Jan 2022 17:32:20 +0100

catalogue

Basic use of functions

Parameters of function

 Function call

Nested calls and chained access to functions

Declaration and use of functions

Recursion of function

Basic use and recursion of functions

1, What is a function

Baidu Encyclopedia's definition of function:

https://baike.baidu.com/item/%E5%87%BD%E6%95%B0/301912https://baike.baidu.com/item/%E5%87%BD%E6%95%B0/301912

Classification of functions in C language:

1. Library function

2. Custom function

  • Library functions. For some basic functions required in the programming process, every programmer may use them in the development process. In order to support portability and improve program efficiency, the basic library of C language provides us with a series of library functions that can directly realize some functions, such as string copy, string link, etc, It is convenient for programmers to develop software.

Common library functions in C language include:

  • IO function
  • String operation function
  • Character manipulation function
  • Memory operation function
  • Time / date function
  • Mathematical function
  • Other library functions

When using library functions, we only need to use #include at the beginning of the program to introduce the corresponding header file.

  • Custom function

Library functions are specific functions that can only realize some specific functions and cannot be changed. Then when we have our own needs and the library functions no longer meet our needs, we need to design the functions ourselves.

Like library functions, custom functions should have function names, return value types, and function parameters. These are all set by ourselves.

For example, chestnuts:

Write a function to exchange the contents of two integers

#include <stdio.h>

#include <math.h>

void Swap(int *m, int *n)//Custom function -- contains two integer parameters with null return value type

{

	int tem = 0;

	tem = *m;

	*m = *n;

	*n = tem;

}

int main()

{ 

	int m, n = 0;

	printf("Please enter two integers\n");

	scanf("%d %d", &m, &n);

	Swap(&m, &n);//Pass two parameters into the custom function in the form of address

	printf("%d %d", m, n);

}

Parameters of function

Actual parameter (actual parameter):

The parameters really passed to the function are called arguments. Arguments can be constants, variables, expressions, functions, etc. No matter what type of arguments are, they must have definite values when making a function call in order to pass these values to the formal parameters.

Formal parameter (formal parameter):

Formal parameters refer to the variables in parentheses of the function name. They are called formal parameters because they are instantiated (memory units are allocated) only in the process of being called by the function. Formal parameters are destroyed when the function call is completed. Therefore, formal parameters are only valid in functions.

Function call:

Value passing call

The formal parameters and arguments of the function occupy different memory blocks respectively, and the modification of the formal parameters will not affect the arguments.

Address call

  • Addressing call is a way to call a function by passing the memory address of the variable created outside the function to the function parameters.
  • This parameter transfer method can establish a real relationship between the function and the variables outside the function, that is, the variables outside the function can be directly operated inside the function.

Nested calls and chained access to functions

Nested call: functions are organically combined with each other

Here, nested calls are made. I give an example of how to calculate the minimum step size of a non Fibonacci number nearest to it (implemented in C language):

#include <stdio.h>
#include <math.h>
//Calculate the x Fibonacci number
int Fib(int x)
{
	if (x == 1)
		return 0;
	else if (x == 2)
		return 1;
	else
	{
		int lleftnum = 0;
		int leftnum = 1;
		int m = 0;
		for (int i = x - 2; i > 0; i--)
		{
			m = lleftnum + leftnum;
			lleftnum = leftnum;
			leftnum = m;
		}
		return m;
	}
}
//Judge whether the number you want to see is a Fibonacci number. If yes, it returns 1 and if not, it returns 0
int IsFib(int x)
{
	for (int i = 1; i <=x; i++)
		if ((Fib(i) == x)||x==1,2,3,5)
			return 1;
	    return 0;
}
int main()
{
	int n = 0;
	printf("Please enter the number you want to judge:>\n");
	scanf("%d", &n);
	int m = n;
	int count1 = 0;
	int count2 = 0;//count1 and count2 are the steps to move left and right respectively if the current number is not a Fibonacci number
	if (n != 0&&IsFib(n)!=1)//The step size is not calculated on the premise that the input number is already Fibonacci number
	{
		while (IsFib(m) != 1)
		{
			m++;
			count1++;
		}
		m = n;
		while (IsFib(m) != 1)
		{
			m--;
			count2++;
		}
		int steps = (count1 > count2 ? count2 : count1);//Returns the smallest left or right offset step
		printf("Currently, the number you have entered is the least  %d  Step can be changed to Fibonacci number", steps);
		return 0;
	}
	else
		printf("Fibonacci number does not make judgment. Please re-enter a number\n");
}

Chained access: take the return value of one function as the parameter of another function.

#include <stdio.h>
int main()
{
	printf("%d", printf("%d", printf("%d", 43)));
//The return value is 4321. If you don't understand why, you can see the definition of printf
	return 0;
}

Declaration and use of functions:

Function declaration:

  • Tell the compiler what a function is called, what the parameters are and what the return type is. But it doesn't matter whether it exists or not.
  • The declaration of a function usually appears before the use of the function. To meet the requirements of declaration before use.
  • The declaration of the function is usually placed in the header file.

Function definition:

  • The definition of function refers to the specific implementation of function and explains the function implementation of function.  

Function recursion

Recursion -- a method in which a procedure or function directly or indirectly calls itself in its definition or description.  

Two necessary conditions for recursion:

  • There is a constraint. When this constraint is met, recursion will not continue.
  • After each recursive call, it gets closer and closer to this limit.
//Accept an integer value and print each bit in order. For example, input 1234, output 1 2 3 4
#include <stdio.h>
void print(int n)
{
	if (n > 9)
	{
		print(n / 10);//Recursive call function print
	}
	printf("%d ", n % 10);
}
int main()
{
	int num = 1234;
	print(num);
	return 0;
}

//Calculate the factorial of a number
#include <stdio.h>
int factorial(int a)
{
	if (a <= 1)
		return 1;
	else
		return a * factorial(a - 1);
}
int main()
{
	int n = 0;
	printf("Please enter the number you want to calculate:");
	scanf("%d", &n);
	printf("%d", factorial(n));
}

Topics: C Back-end