Functions of C language learning notes

Posted by Doom87 on Sat, 15 Jan 2022 01:26:46 +0100

function

  1. Introduction of problems
    Sometimes, we often need to input an array in another program.
    For example:
	int a[10];
	for(i = 0;i < 10;i++)
	{
		scanf("%d",&a[i]);
	}
	
	int b[5];
	for(i = 0;i < 5;i++)
	{
		scanf("%d",&b[i]);
	}
		...
	Functions: reuse code blocks.
  1. function
    What is a function?
    Function function

    A function is an encapsulation of a sequence of instructions that complete a function.

    In C language, except initialization statements, all other statements must be inside the function.

    Function can realize code reuse and modular design.

    Structured programmers advocate dividing a large task into multiple functional functions.

    Function: code block with the same function, reusable, modular design idea.

  2. How to design a function?
    Function is used to implement a function.

    1. What is the function of a clear function? What are the goals to achieve
      Function function
      For example:
      Want to find the maximum value of an array

      You can give the function a name
      There are two points to note when giving function commands:
      1) The naming of function name shall comply with the identifier naming rules of C language
      2) Please try to "see its name and know its meaning" when naming a function.

      find_array_max
      zhao_max
      
    2. Consider what known conditions are required to complete the task / function?
      What parameters need to be input to complete the task / function / function
      For example:
      Parameters to be given to me:
      Give me the array name
      Number of elements in the array

    3. Output results of completing the task / function / function

    4. Algorithm implementation and code debugging.

  3. Grammatical forms of functions in C language
    Function return value type function name (input parameter list)
    {
    C statement; (the part used to implement the code - > determines the specific function of your function)
    return expression;
    }

    "Function return value type":
    The type of the expression value after the return statement.
    Generally, it can be "single value" type, that is, basic type and pointer type.
    Functions can also have no return value.
    The return value type of the function is not specified. If not specified, the default type is int.

    "Function name":
    1) The naming of function name shall comply with the identifier naming rules of C language
    2) Please try to "see its name and know its meaning" when naming a function.

    "Input parameter list":
    Format: data type 1, parameter name 1, data type 2, parameter 2
    void means the function has no parameters

    "return":
    Return means that return can only be inside a function.
    Indicates the end of the function.
    return ;// Indicates the end of the function without a return value
    Return expression// Indicates the end of the function with a return value, which is
    The value of "expression" and the return value type of the function are the type of expression after the return statement.

    be careful:
    Functions cannot be nested, that is, functions cannot be defined inside functions, but only outside functions.

    example:
    Design a function, function function: find the sum of two integers.
    1. Clarify the function of the function and give a name of "see its name and know its meaning" according to the function.
    Sum two integers - > sum
    2. What parameters need to be input to complete the task / function / function
    The input parameters are: two integers
    sum(int a,int b)
    3. Complete the output result of the task / function / function
    int sum(int a,int b) / / function header
    4. Algorithm implementation and code debugging

    		int sum(int a,int b)//Function header
    		{					//Function body
    			int s;
    			s = a + b;
    			return s;
    		}
    
  4. Function call
    Function call: call a written function to execute.

    (1) Function call
    a. Specify the function name
    b. Prepare the parameters required by the function
    Calling function: a function that calls other functions, such as main()
    Other functions called.
    Formal parameter: the parameter of the called function when it is defined
    Actual parameter: the input parameter value passed by the calling function to the called function during the function call
    When calling a function, you need to specify an argument, and the argument needs to correspond to the formal parameter one by one.
    One to one correspondence: the number of formal parameters and arguments must be consistent, and the data type must be consistent.
    In C language, there is only one case for the transfer of function parameters, that is "value transfer": that is, the value of the argument
    Passed to the corresponding formal parameter.

    sum(3,4);
    sum(m,n);
    sum(3+5,4-2);
    sum(sum(3,5),4);
    
    int m = 1,n = 2;
    sum(int m,int n);//error
     When calling a function, you only need to specify the value of the argument, not the type of the argument.
    
    sum(4);//error because the number of real participating formal parameters is inconsistent
    

    (2) Function call procedure
    sum(3,5);

    1.	Assign the value of the argument to the corresponding formal parameter
    2.	Jump to the specified function and execute it until it is encountered return Or return after the function statement is executed
     To function call. return The value of the following expression will be the value of the entire function call expression.
    
  5. Array as an argument to a function
    When the parameter of a function is an array and the array is used as a formal parameter, how to describe it?

    Type of array element array name [number of array elements];

    example:

    (1)	int a[10]
    	
    	//Parameter b receives the array name of an array of type int
    	//Parameter n receives the number of elements in the array
    	xxx(int b[],int n)
    	{
    	
    	}
    	
    	Call:
    		xxx(a,10);
    
    (2)	int a[3][4];
    	
    	//Parameter b receives the array name of an array of type int[4]
    	//Parameter n receives the number of elements in the array			
    	yyy(int b[][4],int n)
    	{
    	
    	}
    
  6. Declaration of function
    "Declaration": a declaration in C language is an existing identifier (object name).

    Why do I need a statement?
    When compiling source files in C language, they are compiled line by line from the first line to the last line.
    If more than one exists in the project c file, is a file to file compilation, sometimes we are in a file
    2. Is required in 1.c Objects (variables, functions,...) defined in C. At compile 1 C, I met
    The name of this object. The compiler doesn't know what this identifier is.

    Convention: we usually put the declaration statement in front of the call.

    Statement:
    Declaration of variables:
    Type and variable name of extern variable;

    Declaration of function:
    Declaration of external function:
    extern the header of the external function;

    	int sum(int a,int b)
    	{
    		return a + b;
    	}
    				
    	Statement: int sum(int ,int )
    
  7. Scope and lifetime of variables
    (1) Scope
    The range in which an object (function, array, variable, etc.) works.

    Global variables:
    	Variables defined outside the function are called global variables.
    	If the global variable is not initialized, it is initialized to 0 by default.
    	Scope of global variables: from customization to the end of the file(It can also be called in other files, but it needs to be called
    				use extern statement). 
    			
    	static If you modify a global variable, the scope of the global variable is limited to this file.
    	static Decorated variables are initialized to 0 by default without initialization.
    	Similarly, if a function is static Modifier, then this function can only be used in this file.
    
    
    Local variables:
    	Variables defined in function bodies or compound statements are called local variables.
    	Scope of local variable: custom function or end of compound statement(That is, the first right curly bracket ends). 
    	
    	Two variables with different scopes must be two independent memory spaces.
    	Even if you have the same name, look up nearby.
    
    Shape does not change to reality:
    	When the calling function calls the called function and the actual parameter is a variable, the value of the formal parameter variable in the called function
    	The change of value will not affect the value of the argument.
    	
    	Reason: the formal parameter is only a local variable in the called function, and the actual parameter variable is two independent memory spaces
    	It's just that the value of the formal parameter variable is assigned to the value of the argument. The two do not affect each other.
    

    (2) Survival period
    It refers to the period (lifetime) from birth to death of an object.

    If a variable passes its lifetime, its memory space will be released by the system.
    
    Global variables:
    	Continuity with process. As soon as your program runs, global variables exist until your process exits.
    	A running program is called a process.
    	
    
    Local variables:
    	1.	Ordinary local variable
    		int a;
    		The lifetime of a common local variable is from the definition to the end of the first right curly bracket.
    		void f()
    		{
    			int a = 7;
    			a++;
    			printf("a = %d\n",a);
    		}
    
    		int main()
    		{
    			f();//8
    			f();//8
    		}
    
    
    	2.	static(static state)local variable
    		static int a;
    		static Survival of local variables: persistence with process.
    			Initialize only once.
    			
    		void f()
    		{
    			static int a = 7;
    			a++;
    			printf("a = %d\n",a);
    		}
    
    		int main()
    		{
    			f();//8
    			f();//9
    		}					
    
    static stay C There are only two functions in language:
    	1.	static Used to modify global variables and functions:
    		Make the scope of modified global variables and functions valid only in this file.
    	2.	static It is used to modify local variables to:
    		The lifetime of the modified local variable varies with the persistence of the process.
    
  8. Recursive function
    Recursive function: directly or indirectly call the function itself.
    Call yourself

    When to use recursion?
    When solving a problem, the solution idea turns into a problem similar to the problem itself.

    Design of C statement recursive function:
    1. The problem model itself should conform to the recursive model (recursive relationship).
    2. First clarify the relationship between the functions to be realized by the function and parameters, regardless of the specific implementation of the function.
    3. When the solution of the problem recurses to a certain level, the answer must be obvious and can end
    Function (not infinite recursion).
    4. The recursive relationship between layer N and layer n-1 should be presented.

    example:
    (1) Write a function to find the age of the nth person.
    The age of the first person is 10 and the age of the second person is 12. Take this as an example.
    //Ordinary Edition

    	int age(int n)
    	{
    		int a[n];
    		a[0] = 10;
    		int i;
    		for(i = 1;i < n;i++)
    		{
    			a[i] = a[i - 1] + 2;
    		}
    		return a[n-1];
    	}
    

    //Recursive version

    	int age(int n)
    	{
    		age(n) = age(n - 1) + 2;
    		...
    		age(5) = age(4) + 2;
    		...
    		age(2) = age(1) + 2;
    		age(1) = 10;
    	}
    	
    	int age(int n)
    	{
    		if(n > 1)
    		{
    			return age(n-1) + 2;
    		}
    		else 
    		{
    			return 10;
    		}
    	}
    
    
    	age(4)
    		--->return age(n-1) + 2;
    					age(3)
    					---> return age(n-1) + 2;
    								 age(2)
    									--->return age(n-1) + 2;
    				
    												10
    

(2) Write a recursive function to realize n!

			int jiechen(int n)
			{
				if(n == 0 || n == 1)
				{
					return 1;
				}
				else
				{
					return jiechen(n-1)*n;
				}
			}
	(3)	Hanio Pagoda	
		hold n A plate from A Move to C,Intermediate can be used B. 
		Print out the moving steps and the number of moving steps.
		
		2
		A->B
		A->C
		B->C
		3steps
		int step;
		//Move n plates from A to C, and B can be used in the middle.
		void hanio(int n,char A,char B,char C)
		{
			//When there are only two plates, the answer is obvious.
			if(n == 2)
			{
				printf("%c-->%c\n",A,B);
				step++;
				printf("%c-->%c\n",A,C);
				step++;
				printf("%c-->%c\n",B,C);
				step++;
				return ;
			}
			else
			{
				//Find out the relationship between layer N and layer n-1
				//step1: place the plates on the nth-1st floor and above of position A to B, and C can be used
				hanio(n-1,A,C,B);

				//Step 2: move the last large plate at position A directly to position C
				printf("%c-->%c\n",A,C);
				step++;
				
				//step3: place the plates on the nth-1st floor and above of position B to C, and use A
				hanio(n-1,B,A,C);
			}
		}
		
		hanio(3,'A','B','C')
		{
			hanio(2,'A','C','B');
			{			
				if(n == 2)
				{
					printf("%c-->%c\n",A,B);//A-->C
					step++;
					printf("%c-->%c\n",A,C);//A-->B
					step++;
					printf("%c-->%c\n",B,C);//C-->B
					step++
					return ;
				}	
			}
			
			printf("%c-->%c\n",A,C);//A-->C
			
			hanio(2,'B','A','C');
			{
				if(n == 2)
				{
					printf("%c-->%c\n",A,B);//B-->A
					step++;
					printf("%c-->%c\n",A,C);//B-->C
					step++;
					printf("%c-->%c\n",B,C);//A-->C
					step++
					return ;
				}					
			}
		}

Topics: C