C Language Details - Functions

This blog is about C language functions
Author: Xiaochen
Welcome to reprint, please indicate the source
Introduction:
What is a function?
We often see the concept of function in mathematics. Here we define a function: a subroutine is a part of code in a large program, consisting of one or more statement blocks. It is responsible for a specific task and has relative independence compared with other codes.
Generally, there are input parameters and return values, which provide encapsulation of the process and hide the details. These codes are usually integrated into software libraries.

function

1. Library function
In the process of development, every programmer may use it. In order to support portability and improve the efficiency of the program, the basic library of C language provides a series of similar library functions for programmers to develop software.
Note: Using library functions, you must include header files corresponding to # include.
2. Custom function
Custom functions, like library functions, have function names, return value types and function parameters. But the difference is that we design them all by ourselves. This gives programmers a lot of room to play.

int sum(int x)//parameter x
{
	Statement item;
	Return (return value);
}

Example: Use function call method to maximize

#include <stdio.h>
//Design of get_max function
int get_max(int x, int y)
{
return (x>y)?(x):(y);
}
int main()
{
int num1 = 10;
int num2 = 20;
int max = get_max(num1, num2);
printf("max = %d\n", max);
return 0;
}

3. PARAMETERS OF FUNCTIONS
Actual parameters;
The real parameters passed to the function are called arguments. Arguments can be constants, variables, expressions, functions, etc. Regardless of the type of arguments, they must have definite values when making function calls in order to pass these values to the parameters.
Formal parameters:
Formal parameters refer to the variables in parentheses after the function name, because formal parameters are instantiated only in the process of the function being called (allocating memory units), so they are called formal parameters. Formal parameters are automatically destroyed when the function call is completed. So formal parameters are only valid in functions.
Note: The parameter does not change the value of the parameter.
Note here that if the parameter is an array name and the subscript is used to refer to the array's parameters in the function, the modification of the array elements in the function is actually the modification of the array elements in the calling program, and the function will access the array elements of the calling program. In fact, there is no contradiction here. The value of array name is actually a pointer, which is the copy of the pointer passed to the function. Subscript reference is actually another form of indirect access. It can indirectly access the pointer and access the contents of the memory location pointed by the pointer. Blog posts on subsequent arrays will also be mentioned.
4. Call of Function
Function calls:
(1) Value transfer call
Function parameters and parameters occupy different memory blocks, and the modification of parameters will not affect the parameters.
(2) Address Call
Address call is a way to call a function by passing the memory address of the variable created outside the function to the function parameter.
This method of parameter transfer can establish a positive and true relationship between the function and the variables outside the function, that is, the variables outside the function can be directly manipulated inside the function.

There are not too many difficulties with function calls. Here are a few examples.
1. Write a program to realize the output multiplication formula table, requiring users to choose (n*n)

#include <stdio.h>
void Prf_Table(int x)      //Subfunctions of Printing Multiplication Tables
{
	int i=1,j;
	for (; i <= x; i++)
	{
		for (j = i; j <= x; j++)
		{
			printf("%d*%d=%-4d", i, j, i*j);
		}
		printf("\n");
	}
}
int main()
{
	printf("Please enter the number of multiplication tables you want to print\n");
	int i;
	scanf("%d", &i);
	Prf_Table(i);   //j takes the value entered by the user as a parameter and calls the function at the same time
	return 0;
}

2. Write programs to initialize, empty and invert arrays

#include <stdio.h>
void manu()//Print menu functions
{
	printf("****************1.Clean up arrays*************\n");
	printf("****************2.Inverse array*************\n");
}

void init(int arr1[], int n)//Functions that initialize arrays
{
	int i = 0;
	printf("Initialize arrays arr[6]:\n");
	for (i = 0; i<n; i++)
	{
		scanf("%d", &arr1[i]);
	}
	printf("Initialization arr[6]after\n");
	for (i = 0; i<n; i++)
	{
		printf("arr[%d]=%d ", i, arr1[i]);
	}
	printf("\n");
}
void empty(int arr1[], int n)//Functions of empty arrays
{
	int i = 0;
	printf("empty arr[6]after\n");
	for (i = 0; i<n; i++)
	{
		arr1[i]=0;
		printf("arr[%d]=%d ",i, arr1[i]);
	}
	printf("\n");
}

void reverse(int arr1[],int n)//Functions of array inversion
{
	int i=0;
	int tmp=0;
	for (i = 0; i<(n / 2); i++)
	{
		tmp = arr1[i];
		arr1[i] = arr1[6 - i - 1];
		arr1[6 - i - 1] = tmp;
	}
	printf("Inverse array arr[6]after\n");
	for (i = 0; i<n; i++)
	{
		printf("arr[%d]=%d ", i, arr1[i]);
	}
	printf("\n");
}
int main()
{
	int input = 0;
	int arr[6];
	int i = sizeof(arr) / sizeof(arr[0]);//Get the array size
	init(arr, i);//Initialization function call
	do
	{
		manu();  //Print menu
		printf("Please choose:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1: empty(arr, i); break;
		case 2: reverse(arr, i); break;
		default:printf("Please re-enter\n"); break;
		}
		printf("End please enter:0,Continue please enter:1\n");//End the cycle
		scanf("%d", &input);
	} while (input);
	return 0;
}

This is the end of this blog, and the next section will describe the difficulty of a function part: recursion.
Thank you for reading.

Keywords: C

Added by deltatango5 on Fri, 26 Jul 2019 11:00:51 +0300