当前位置:网站首页>The basis of C language grammar -- function definition learning

The basis of C language grammar -- function definition learning

2022-06-26 09:52:00 Tanzanian auduvi Canyon expert

Function definition :

Function from English function Translated from ,function It translates to function , It's also a function , In essence , Functions are collections used to complete certain functions .

Why use functions
For example , In a long piece of code , There are many businesses , Many places need to calculate the monthly sales of goods , They all realize the same function , Is every business , Each place writes its own set of query and calculation logic , In this way, the code is very redundant and complicated , Then we can package this code , Let it stand alone , Function encapsulation is also used , such , everyone , If every business wants to use this function , Just one sentence to call , It's like I say a word to you , I want to use the function named so and so , I transferred him , Then he will be used by me .

Defining a function should include the following :
(1) Specify the name of the function , To call by name later .
(2) Specifies the type of function , That is, the type of function return value .
(3) Specifies the name and type of the function's arguments , To pass data to functions when they are called , This is not required for nonparametric functions .
(4) Specifies what the function should do , That is, what functions do , The function of a function . That's the most important thing , It is solved in the function body .

Function definition method

//  Mode one   Parameterless function 
 Type name   Function name (){
      //  The type name defines the type according to the type of the return value of the function 
	 The body of the function 
}

//  Mode two   Parameterless function 
 Type name   Function name (void{
     // void  It means empty , Indicates that the function has no parameters 
	 The body of the function 
}

//  Mode three   Parameterless function 
void  Function name (){
    
	 The body of the function 
}

//  Mode 4   There are parametric functions 
 Type name   Function name ( Formal parameter table column ){
    
	 The body of the function 
}

//  Parameterless function 
#include<stdio.h>
int main()
void print _ star();// Statement print _ star function 
void print _ message();// Statement print _ message function 
print _ star();/ call print _ star function 
print _ message();//print _ message function 
print _ star();// call print _ star function 
return 0;
)


void print _ star()// Definition print _ star function  ; The type is void Function of , There is no need for return Return value 
{
    
print[('******************\n');// Output one line × Number 
}

void print _ message()// Definition print _ message function 
(printf(”How do you do!\n");// Output a line of text 
)


 Running results :
How do you do?
*******************
//  There are parametric functions 
int max(int x, int y){
     //  here x、y It's a parameter 
	int sum; // Declaration part 
	sum=x+y;  //  Execute statement part 
	return sum;
}

//  call 
int max(a, b) //  here a、b Is the argument 

原网站

版权声明
本文为[Tanzanian auduvi Canyon expert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260911169079.html