当前位置:网站首页>010 C language foundation: C function

010 C language foundation: C function

2022-06-27 04:24:00 Prison plan progress 50%

One : summary

A function is a set of statements that perform a task together . Every C Every program has at least one function , The main function main() , All simple programs can define additional functions .

You can divide the code into different functions . It's up to you to divide the code into different functions , But logically , Partitioning is usually based on each function performing a specific task .

The function declaration tells the compiler the name of the function 、 Return types and parameters . The function definition provides the actual body of the function .

C The standard library provides a large number of built-in functions that can be called by programs . for example , function strcat() Used to connect two strings , function memcpy() Used to copy memory to another location .

There are many other ways to call functions , Such as method 、 A subroutine or program , wait .

 

Two : Defined function

return_type function_name(parameter list){
    
	body of the function
}

stay C In language , A function consists of a function header and a function body .

All components are as follows :

  1. Return type : A function can return a value .return_type Is the data type of the value returned by the function . Some functions perform the required operations without returning values , In this case ,return_type Is the key word void
  2. The name of the function : This is the actual name of the function . Function name and parameter list together constitute function signature .
  3. Parameters : Parameters are like placeholders . When a function is called , You pass a value... To the parameter , This value is called the actual parameter . The parameter list includes the types of function parameters 、 The order 、 Number . Parameters are optional , in other words , Function may not contain arguments .
  4. Function main body : The function body contains a set of statements that define the function's execution tasks .
 example :
	#include <stdio.h>
	int max(int num1, int num2){
    		//  Returns the larger of two numbers 
	   int result;		//  Local variable declaration 
	   if(num1 < num2){
    
			result = num2;
	   }else{
    
			result = num1;
	   }
	   return result;
	}

	int main(){
    
		int a = 100;		//  Defining local variables 
		int b = 200;
		int ret;
		ret = max(a, b);		//  Call function 
		printf("max is : %d \n", ret);
		return 0;
	}

 

3、 ... and : Function parameter

If the function uses parameters , You must declare the variable that accepts the value of the parameter . These variables are called formal parameters of functions . Formal arguments are like other local variables in a function , Created when entering a function , Destroy on exit .

 

Four : How parameters are passed :

stay c Every variable in the language has two properties. One is a value , One is the address . By default ,C Use value passing calls to pass parameters . Generally speaking , This means that the code within the function cannot change the actual parameters used to call the function .

4.1: Value transfer call ( Pass value )

This method copies the actual value of the parameter to the formal parameter of the function . under these circumstances , Modifying the formal parameters in a function does not affect the actual parameters .

 example :
	#include <stdio.h>
	//  Defined function 
	void swap(int x, int y){
    
		int temp;
		temp = x;       //  Temporary storage x Value 
		x = y;
		y = temp;
		return ;
	}

	void swqp(int x, int y);    //  Function declaration 

	int main(){
    
		int a = 100;
		int b = 200;
		printf(" Before value transfer a: %d \n", a);
		printf(" Before value transfer b: %d \n", b);

		swap(a, b);         //  Call function , stay swap Change in function x and y Value , Follow a and b It has nothing to do with itself  
		printf(" After value transfer a: %d \n", a);
		printf(" After value transfer b: %d \n", b);
		return 0;
	}
	 result :
		┌──(rootkali)-[~/Desktop/c_test]
		└─# vim chuanzhi.c
		┌──(rootkali)-[~/Desktop/c_test]
		└─# gcc chuanzhi.c -o chuanzhi   
		┌──(rootkali)-[~/Desktop/c_test]
		└─# ./chuanzhi                
		 Before value transfer a: 100 
		 Before value transfer b: 200 
		 After value transfer a: 100 
		 After value transfer b: 200 

4.2: Reference call ( Byref )

This method copies the address of the parameter to the formal parameter . Within the function , This address is used to access the actual parameters to be used in the call . It means , Modifying formal parameters will affect actual parameters .

 example :
		#include <stdio.h>
		//  Defined function 
		void swap(int *x, int *y){
    
			int temp;
			temp = *x;
			*x = *y;
			*y =temp;
			return;
		}

		void swap(int *x, int *y);  //  Function declaration 

		int main(){
    
			int a = 100;
			int b = 200;
			printf(" Before value transfer a: %d \n", a);
			printf(" Before value transfer b: %d \n", b);

			swap(&a, &b);       //  Call function ,&a To point to a The pointer to , Namely variable a The address of .
			printf(" After value transfer a: %d \n", a);
			printf(" After value transfer b: %d \n", b);
			return 0;
		}
	 result :
		┌──(rootkali)-[~/Desktop/c_test]
		└─# vim yingyong.c
		┌──(rootkali)-[~/Desktop/c_test]
		└─# gcc yingyong.c -o yingyong
		┌──(rootkali)-[~/Desktop/c_test]
		└─# ./yingyong
		 Before value transfer a: 100 
		 Before value transfer b: 200 
		 After value transfer a: 200 
		 After value transfer b: 100 

原网站

版权声明
本文为[Prison plan progress 50%]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270404363736.html