当前位置:网站首页>C language programming (Chapter 6 functions)
C language programming (Chapter 6 functions)
2022-07-06 00:42:00 【Classmate Zhang】
C Language programming ( Chapter six function )
1、 Why define functions
Defining functions includes 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 a function does , The function of a function . That's the most important thing , It is solved in the function body .
2、 How to define a function
(1) Define nonparametric functions
Type name Function name (){
The body of the function
}
or
Type name Function name (void){
The body of the function
}
#include<stdio.h>
int main(){
void print_star();
void print_message();
print_star();
print_message();
print_star();
return 0;
}
void print_star(){
printf("*******************\n");
}
void printf_message(){
printf("Hello,world!\n");
}
Execution results :
*******************
Hello,world!
*******************
(2) Define parametric function
As defined below max Function is a parametric function :
Type name Function name ( Formal parameter table column ){
The body of the function
}
int max(int x,int y){
int z;
z=x>y?x:y;
return(z);
}
(3) Define empty functions
Type name Function name (){
}
for example :
void dummy(){
}
3、 Call function
(1) The form of function call
The general form of a function call is
Function name ( Argument table column )
print_star(); // Call parameterless function
c=max(a,b); // Call the parameter function
(2) Function call statements
Take the function call as a single statement , The function is not required to bring back the value , Only the function is required to complete certain operations . for example :
print_start();
(3) Function expression
The function call appears in another expression , Such as “c=max(a,b);”,max(a,b) It's a function call , It's part of the assignment expression . At this time, the function is required to bring back a certain value to participate in the operation of the expression . for example :
c=2*max(a,b);
(4) Function parameter
The argument when a function call is used as another function call . for example :
m=max(a,max(b,c));
4、 Function call data transfer
(1) Formal parameters and actual parameters
Calling There are parametric functions when , There is a data transfer relationship between the calling function and the called function . Known from the front : When defining a function, the variable name in parentheses after the function name is “ Formal parameters ”( abbreviation “ Shape parameter ”) or “ Virtual parameters ”. When calling a function in the keynote function , The arguments in parentheses after the function name are called “ The actual parameter ”( abbreviation “ Actual parameters ”). The actual parameter can be a constant 、 Variable or expression .
(2) Data transfer between arguments and formal parameters
In the process of calling the function , The system will pass the value of the argument to the formal parameter of the called function .
Example 1: Enter two integers , The larger value is required to be output . It requires a function to find a large number .
#include<stdio.h>
int max(int x,int y){
int z;
z=x>y?x:y;
return(z);
}
int main(){
int max(int x,int y);
int a,b,c;
printf(" Please enter two integers :");
scanf("%d%d",&a,&b);
c=max(a,b);
printf("max is %d\n",c);
return 0;
}
Execution results :
Please enter two integers :18 48
max is 48
(3) The return value of the function
① The return value of the function is passed through return The statement gets .
② The type of function value
③ When defining a function, the specified function type should be similar to return The expression types in statements are the same , That is, the function type determines the type of the return value .
④ For functions not brought back , The function should be defined as “void type ”( Or called “ Empty type ”)
5、 Nested calls to functions
Example 2: Input 4 It's an integer , Find the largest number . Use the nesting of functions to deal with .
#include<stdio.h>
int main(){
int max4(int a,int b,int c,int d);
int a,b,c,d,max;
printf(" Please enter 4 A digital :");
scanf("%d%d%d%d",&a,&b,&c,&d);
max=max4(a,b,c,d);
printf("max is %d\n",max);
return 0;
}
int max4(int a,int b,int c,int d){
int max2(int a,int b);
int m;
m=max2(a,b);
m=max2(m,c);
m=max2(m,d);
return m;
}
int max2(int a,int b){
if(a>=b){
return a;
}
else
return b;
}
Execution results :
Please enter 4 A digital :43 65 6 8
max is 65
6、 Recursive call of function
In the process of calling a function, it occurs to call the function itself directly or indirectly , Called recursive calls to functions .
Example 3: Find... Recursively n!
#include<stdio.h>
int main(){
int fac(int n);
int n,y;
printf(" Please enter the number of factorials :");
scanf("%d",&n);
y=fac(n);
printf("%d!=%d\n",n,y);
return 0;
}
int fac(int n){
int f;
if(n<0){
printf("n<0,data Error!");
}
if(n==0 || n==1){
f=1;
}
else{
f=fac(n-1)*n;
}
return f;
}
Execution results :
Please enter the number of factorials :10
10!=3628800
Example 4: The hanotta problem
#include<stdio.h>
int main(){
void hanoi(int n,char one,char two,char three);
int m;
printf(" Please enter the number of plates :");
scanf("%d",&m);
printf("%d Plate movement :\n");
hanoi(m,'A','B','C');
return 0;
}
void hanoi(int n,char one,char two,char three){
void move(char x,char y);
if(n==1){
move(one,three);
}
else{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y)
{
printf("%c->%c\n",x,y);
}
7、 One dimensional array name as function parameter
Example 5: There's a one-dimensional array score, Place... Inside 10 Students' grades , For the average score
#include<stdio.h>
int main(){
float average(float array[10]);
float score[10],aver;
int i;
printf(" Please enter 10 Scores of students :\n");
for(i=0;i<=9;i++){
scanf("%f",&score[i]);
}
aver=average(score);
printf("average score is %5.2f\n",aver);
return 0;
}
float average(float array[10]){
int i;
float aver,sum=0;
for(i=0;i<=9;i++){
sum+=array[i];
}
aver=sum/10;
return aver;
}
Execution results :
Please enter 10 Scores of students :
100 56 78 98 67.5 99 54 88.5 76 58
average score is 77.50
8、 Multidimensional array name as function parameter
Example 6: There is one 3×4 Matrix , Find the maximum of all elements
#include<stdio.h>
int main(){
int max(int array[][4]);
int a[3][4]={
{
1,3,5,7},{
2,4,6,8},{
12,13,17,14}};
printf("max value is %d\n",max(a));
return 0;
}
int max(int array[][4]){
int i,j,max;
max=array[0][0];
for(i=0;i<3;i++){
for(j=0;j<4;j++){
if(array[i][j]>max){
max=array[i][j];
}
}
}
return max;
}
Execution results :
max value is 17
边栏推荐
- Date类中日期转成指定字符串出现的问题及解决方法
- [groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
- MySQL storage engine
- Set data real-time update during MDK debug
- Promise
- Free chat robot API
- STM32按键消抖——入门状态机思维
- NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- Notepad++ regular expression replacement string
猜你喜欢
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
如何制作自己的機器人
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
vSphere实现虚拟机迁移
Basic introduction and source code analysis of webrtc threads
Ffmpeg captures RTSP images for image analysis
Leetcode:20220213 week race (less bugs, top 10% 555)
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
随机推荐
MySQL存储引擎
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
Model analysis of establishment time and holding time
详细页返回列表保留原来滚动条所在位置
The value of applet containers
Common API classes and exception systems
NLP generation model 2017: Why are those in transformer
synchronized 和 ReentrantLock
Spark DF增加一列
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
LeetCode 斐波那契序列
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
Extension and application of timestamp
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
curlpost-php
Notepad + + regular expression replace String
How to solve the problems caused by the import process of ecology9.0
建立时间和保持时间的模型分析
Folding and sinking sand -- weekly record of ETF