Functions of C language notes

Posted by Hellomonkey on Wed, 10 Nov 2021 12:22:40 +0100

function

1, Concept of function

  1. A function is an independent program module that does a specific job and is packaged. The types of functions include library functions and self created functions.
  2. Library functions include scanf(),printf(), etc. These two functions are contained in the stdio.h function library.
  3. Self created function is the function designed according to the requirements of the topic, and then encapsulate the function and call it directly when in use. We mainly study the structure and usage of self generated functions.

2, Definition and application of self generated function

1. The general form of user-defined function definition is:

Function type function name (formal parameter)    \\Function header\\
{
    Function implementation process               \\Function body\\
    return (Value to return);
}
  1. The function type can be any data type in c language, such as double, char, int, etc.
  2. Function name is a kind of identifier, and the naming rule is the same as that of identifier. The () after the function name cannot be less.
  3. The function implementation process is the code that the function needs to execute. It is the theme of the function and needs to be surrounded by {}.
  4. If there is a return value, it is returned through the return statement in the function body. And the return value should be consistent with the type of the function you define.

For example, you need to calculate from 1+ 2!+ 3!+…+ n!

If the function method is not used in this problem, it is very simple. However, if it is necessary to calculate the sum of the multipliers of two different values in a program, it is necessary to input two for loops to calculate the multipliers of the two numbers respectively, and then use a variable to store them, and finally add them to get the result.

If the function method is used, a function can be designed to calculate the multiplication order, and finally the function can be referenced for calculation. Then reference the function for calculation.

Function:

   int fact(int n)
   {
     int a=1;
     for(int i=1;i<=n;i++)
     {
       a=a*i;
     }
        return a;
    }

The final result is saved in variable a and returned through the return statement. A is of type int, and the return value is also of type int. they correspond to each other one by one.

Return is a keyword in C language. It can only be used in functions to return processing results.

This function is used to calculate the multiplication order of n, which is a self created function. However, it cannot be used directly, but it needs to be referenced in the main function.

Of course, functions also have functions without return value. Different from int or other data type functions, the prefix of such functions is viod, that is, they have no return type.

Function:

   void fact(int n)
   {
     int a=1;
     for(int i;i<=n;i++)
       a=a*i;
    }
    }

void is a keyword of c language, which indicates null type or no type. In most cases, there is no return value.

3, How to call self created functions

1. When the self created function and the main function are in the same cpp file, and the self created function is behind the main function, it is necessary to declare the function in the same form as the declared variable.

#include <stdio.h>     
int sum(int n);         //Call function//
int main()
{
	intc x,y;
	scanf("%d %d",&x,&y);
	int result1=sum(x);     //Factorial when n=x//
	int result2=sum(y);     //Factorial when n=y//
	int result3=result1+result2;
	printf("%d",result3);
}
int sum(int n){       //Function part//
	int i,a=1;
	for(i=1; i<=n; i++){
    	a=a*i;
	}
	return a;
}

2. When the self created function and the main function are in the same cpp file and the self created function is before the main function, it is not necessary to declare the function.

#include <stdio.h>  
int sum(int n){       
	int i,a=1;
	for(i=1; i<=n; i++){
    	a=a*i;
	}
	return a;
}
int main()
{
	int x,y;
	scanf("%d %d",&x,&y);
	int result1=sum(x);     //Factorial when n=x//
	int result2=sum(y);     //Factorial when n=y//
	int result3=result1+result2;
	printf("%d",result3);
}

3. When the self created function and the main function are not in the same cpp file, you need to place the two functions in the same folder and declare the user-defined function with #include "function name. CPP". The function name is consistent with the format on CPP.

#include <stdio.h>
#include "sum.cpp>     
int sum(int n);         //Call function//
int main()
{
	int x,y;
	scanf("%d %d",&x,&y);
	int result1=sum(x);     //Factorial when n=x//
	int result2=sum(y);     //Factorial when n=y//
	int result3=result1+result2;
	printf("%d",result3);
}

Since the self created function does not have a return type, you can also refer to a function with void as the type.

  • For example, find 1+ 2!+ 3!+…+ n!

Function:

#include <stdio.h>     
void sum(int n);         //Call function//
int main()
{
	int x;
	scanf("%d",&x);
    sum(x);
	
}
void sum(int n){       //Function part//
	int i,a=1;
	for(i=1; i<=n; i++){
    a=i*a;
	}	
	printf("%d",a);
}

When the function has no return type, it will not use return to return the value of a, so you need to implement the next things in the self created function
At this time, the sum function already contains the value, so you can get the value directly by using sum in the function. Once the return value type of a function is defined as void, it can no longer receive its value. So you can use this function directly.
##4, Actual and formal parameters, global and local variables.

###1, Actual parameters and formal parameters

1. Formal parameters are parameters named when defining functions or procedures. Generally speaking, it is a sign. The actual parameters are the parameters passed to the function or procedure during execution and when the function or procedure is called. Generally speaking, it is the actual value. A parameter is a variable, which changes according to different users.

For example:

I learned the sin(x) function in middle school, where x is the formal parameter. When you need to find the sine of 1, you will use sin(1), where 1 is the actual parameter. 4 Relationship between formal parameters and arguments: the two are combined at the time of call. Usually, the argument will pass the value to the formal parameter. After the formal parameter is removed, the function process operation may be carried out, and then some values may be returned to the caller through parameters or function symbols.

#include <stdio.h>  
int sum(int n){   //int n here is the formal parameter//      
	int i,a=1;
	for(i=1; i<=n; i++){
    	a=a*i;
	}
	return a;
}
int main()
{
	int x,y;
	scanf("%d %d",&x,&y);
	int result1=sum(x);     //Factorial when n=x / / / / here x is the actual parameter//
	int result2=sum(y);     //Factorial when n=y//
	int result3=result1+result2;
	printf("%d",result3);
}

2, Local and global variables

1. Local variables

Variables defined inside a function are called local variables. Its scope is limited to the inside of the function and is invalid after leaving the function.

int f1(int a){
	int b,c;  //a. B, C are valid only in function f1()
	return a+b+c;
}
int main(){
	int m,n;  //m. N is valid only within the function main()
	return 0;
}
  • Notes:
    1. The variables defined in the main function are also local variables and can only be used in the main function; At the same time, variables defined in other functions cannot be used in the main function. The main function is also a function and has equal status with other functions.
  1. Formal parameter variables and variables defined in the function body are local variables. The process of transferring values from arguments to formal parameters is also the process of assigning values to local variables.

  2. You can use the same variable name in different functions. They represent different data, allocate different memory, do not interfere with each other, and will not be confused.

  3. A variable can also be defined in a statement block, and its scope is limited to the current statement block.

2. Global variables

1. Variables defined outside all functions are called global variables. By default, its scope is the whole program, that is, all source files, including. c and. h files.

int a, b;  //global variable
void func1(){
	//TODO:
}
float x,y;  //global variable
int func2(){
	//TODO:
}
int main(){
	//TODO:
	return 0;
}
  • Notes:

a. b, x and y are global variables defined outside the function. C language code is executed from front to back. Since x and y are defined after func1(), they are invalid in func1(); A and b are defined at the beginning of the source program, so they are valid in func1(), func2() and main().

Comprehensive examples of local and global variables:

#include <stdio.h>
int n = 10;  //global variable
void func1(){
	int n = 20;  //local variable
	printf("func1 n: %d\n", n);
}
void func2(int n){
	printf("func2 n: %d\n", n);
}
void func3(){
	printf("func3 n: %d\n", n);
}
int main(){
	int n = 30;  //local variable
	func1();
	func2(n);
	func3();
	//The code block is surrounded by {}
	{
    int n = 40;  //local variable
    printf("block n: %d\n", n);
	}
	printf("main n: %d\n", n);
	return 0;
}

5, Storage category of variable

In c language, variables have two storage categories: automatic class and static class.

1. Storage category of local variables:

(1) auto variable

int a; Equivalent to auto int a;
The storage category of auto variables is dynamic. In general, we rarely show that the declared variable is of type auto in the program, because the variable in the code block is of this type by default.

(2) static variable

Those declared with static are called static local variables. This type can be applied to global variables or local variables. The assignment of static local variables is given at compile time. There is an initial value when the program runs. In the future, each function call is no longer re assigned, but the value at the end of the last function call is retained. If the static function is not assigned an initial value, it will be automatically assigned a value of 0, while if the dynamic local variable is assigned a value, the system will automatically assign an uncertain value.

example:

Dynamic local variables:

#include <stdio.h>

void test()
{
	int num = 0;
	num++;
	printf("%d ", num);
}
	int main()
{
	int i = 0;
	for (i = 0; i < 10; i++)
	{
    	test();
	}	
	return 0;
}

The output is 1 1 1 1 1 1 1 1 1

Because the dynamic variable will be re assigned num to 0 every time the function is called, because num is equal to 0 every time the function is called.

#include <stdio.h>

void test()
{
	static int num = 0;
	num++;
	printf("%d ", num);
}
int main()
{
	int i = 0;
	for (i = 0; i < 10; i++)
	{
	    test();
	}
	return 0;
}

The output result is 1 2 3 4 5 6 7 8 9 10

Because the static variable is re assigned every time the function is called, but retains the value at the end of the last function call, the value of each function is the value of the last output plus one.

recursion

1. What is recursion?

Recursion is a function called in its function body. It is called recursive call. This function is called recursive function.

In short: recursion is a function that calls itself directly or indirectly.

For example:

include <stdio.h> 
void fact(int n);
int main()
{
	fact(7);
	return 0;
}
void fact(int n)
{
	if(n==1){
		printf("Recursion is me calling myself\n"); 
	}
	else{
		printf("%d\n",n);
		fact(n-1);
	}
}

The running result is 7 6 5 4 3 2 recursion. I call myself

After each call, the function will be called again until n=1 ends the program. Is the simplest concept and usage of recursion, that is, calling myself for myself.

Editor in chief: Qin Congming
Producer: Mantianxing

Topics: C