C language programming (Part V) (function)

Posted by sdaniels on Thu, 20 Jan 2022 06:23:05 +0100

  • Functions are functions, and each function is used to implement a specific function. When designing a large program, it is often divided into several program modules. Each module contains one or more functions, and each function realizes a specific function. Using function to realize modular program design is convenient for writing and compiling respectively, and improves debugging efficiency.
  • A C program can be composed of a main function and several other functions. The main function calls other functions, and other functions can also call each other (but cannot call the main function). The same function can be called any number of times by one or more functions.
  • A source program file is a compilation unit. When the program is compiled, it is compiled in the unit of source program file, not in the unit of function.
  • All functions are parallel, that is, they are defined separately and independent of each other. A function is not subordinate to another function, that is, functions cannot be nested (but can call each other).
  • All functions used in the program must be "defined first and then used".

The definition function shall include:

  1. The name of the function. (it should reflect the function it represents)
  2. The type of the function. (i.e. type of function value)
  3. The name and type of the argument to the function. (this is not required for parameterless functions)
  4. What should the function do. (that is, the function of the function)

From the perspective of user use, there are two functions:

  1. Library functions: they are provided by the compiling system. Users can use them directly without defining them themselves (such as sqrt, fabs, sin and cos). However, at this time, it is necessary to write: #include < math. At the beginning of the module in this file h>
  2. User defined function: it is designed by users according to their actual needs to realize the functions specified by users.

From the form of functions, functions can be divided into two categories:

  1. Parameterless function: it is generally used to perform a group of simple operations. When calling a parameterless function, there is no data transferred between the calling function and the calling function.

The general form of defining a parameterless function is:

Type name function name()
{
	Function body
}

Example:

void print_message() //Define print_message
{
	printf("Today is Saturday");
}
//The function is of void type, which means that the return value does not need to be brought back
  • The function body includes the declaration part and the execution statement part. When defining a function, use "type name" to specify the type of function value, that is, the type of value brought back by the function.
  • The declaration part includes the definition of variables used in functions and declaration of functions to be invoked in this function.
  • The "definition" and "Declaration" of a function are not the same thing.
  • The definition of function refers to the establishment of function function, including specifying function name, function value type, formal parameter type and function body. It is a complete and independent function unit
  • The function of "Declaration" is to inform the compiling system of the name of the function, the type of function value and the type, number and order of formal parameters, so that the system can check according to this when calling the function (for example, whether the function name is correct and whether the type and number of actual parameters and formal parameters are consistent).! It does not contain the function body.
  1. Parametric function: when calling a function, the calling function passes parameters to the called function when calling the called function. Generally, when the called function is executed, a function value will be obtained for the main calling function.

The calling form of parameterless function is: function name ()
Example: print_message();

The general form of defining parametric functions is:

Type name function name(Formal parameter table column)
{
	Function body
}

Example:

int max(int x,int y)
{
	int z; //Declaration part in function body
	z=x>y ? x : y;
	return (z); 
}
  • return(z) is used to bring the value of Z back to the main function as the value of the function. It is also called the function return value. The parentheses on both sides of the return value after return can be omitted and can be simplified to "return z;".

The calling form of parametric function is: function name (argument table column)
Example: max(a,b);

  • If the argument table column contains multiple arguments, the parameters are separated by commas.
  • The number of arguments and formal parameters should be equal, and the types should match.
  • The argument corresponds to the formal parameter in order and passes data to the formal parameter.
  • When defining a function, the variable name in parentheses after the function name is a formal parameter (formal parameter); When calling a function in the keynote function, the parentheses in the function name (which can be an expression) are called actual parameters.
  • The formal parameters specified in the definition function do not occupy the storage unit in memory when there is no function call. When a function call occurs, the formal parameters in the function are allocated to memory units.
  • The call ends and the parameter is released.! The argument unit remains and remains unchanged. If the value of the formal parameter changes during the execution of a called function, the value of the argument of the calling function will not be changed.
  • In C language, the data transfer from an argument to a formal parameter is "value transfer", which is one-way transfer. Only the argument is passed to the formal parameter, but not from the formal parameter to the argument.

Example: enter 4 integers and find the largest number (implemented by function)

#include <stdio.h>
int main()
{
	int max_4(int a, int b, int c, int d); //max_4 declaration of function
	int a, b, c, d, max;
	printf("Please enter 4 integer numbers:");
	scanf("%d %d %d %d",&a,&b,&c,&d);
	max = max_4(a, b, c, d); //Call max_4 function, get the largest of the four numbers, and assign it to the variable max
	printf("max=%d \n",max);
	return 0;
}

int max_4(int a, int b, int c, int d) //Define max_4 function
{
	int max(int x, int y); //Declaration of max function
	int m;
	m = max(a,b); //Call the max function to find the largest of a and B
	m= max(m, b); //Call the max function to find the largest of a, B and C
	m= max(m, d); //Call the max function to find the largest of a, B, C and D
	return m; //The return value of the function is the largest of the number
}

int max(int x, int y) //Define max function
{
	return(x > y ? x : y); //The return value of the function is the largest of X and y
}
  • It can be seen from the program that the declaration of the function is basically the same as the function header in the function definition. Therefore, you can simply write the first part of the defined function and add a semicolon to become the "Declaration" of the function. You can also write only the type of the formal parameter instead of the formal parameter name in the function declaration. In C language, function declaration is called function prototype. Using function prototypes is an important feature of ANSI C. Its main function is to use it to comprehensively check the legitimacy of the calling function in the compilation stage of the program.
  • Recursive method n ! n! n!
#include <stdio.h>
int main()
{
	long fac(int n); //Declare the fac function
	int n;
	long fact; //The variable fact is used to store n! Value of
	printf("Please enter a integer number: \n"); //Output a line of information, please enter n
	scanf("%d", &n); //Enter n
	if (n < 0) //When the input is negative, it is judged as abnormal data
		printf("n<0,data error!");
	else //When n is 0 or an integer, the function is called
	{ 
		fact = fac(n);
		printf("%d!=%ld\n",n,fact); //Output n!
	}
	return 0;
}

long fac(int n) //Define fac function
{
	int i;
	long fac = 1;
	for (i = 1; i <= n; i++) //Calculate n!
		fac *= i; 
	return fac; //Return results
}
  • Recursive method n ! n! n!
#include <stdio.h>
int main()
{
	long fac(int n);
	int n;
	long fact;
	printf("Please enter a interger number: \n");
	scanf("%d", &n);
	if (n < 0)
		printf("n<0,data error!");
	else
	{ 
		fact = fac(n);
		printf("%d!=%ld\n",n,fact); 
	}
	return 0;
}

long fac(int n)
{	
	long fact;
	if (n == 0 || n == 1) //0 And 1! Equal to 1
		fact = 1;
	else
		fact = n*fac(n-1); //Call fac function recursively
	return fact;
}

Topics: C Algorithm