当前位置:网站首页>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
边栏推荐
- After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
- OpenCV经典100题
- Spark DF adds a column
- CTF daily question day44 rot
- Idea远程提交spark任务到yarn集群
- Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
- Go learning --- read INI file
- Codeforces Round #804 (Div. 2)【比赛记录】
- STM32 configuration after chip replacement and possible errors
- 小程序容器可以发挥的价值
猜你喜欢
How to make your own robot
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
看抖音直播Beyond演唱会有感
数据分析思维分析方法和业务知识——分析方法(二)
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
uniapp开发,打包成H5部署到服务器
Extension and application of timestamp
Comment faire votre propre robot
I'm interested in watching Tiktok live beyond concert
随机推荐
Why can't mathematics give machine consciousness
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
几百行代码实现一个 JSON 解析器
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Lone brave man
LeetCode 6004. Get operands of 0
How spark gets columns in dataframe --column, $, column, apply
The detailed page returns to the list and retains the original position of the scroll bar
Notepad++ regular expression replacement string
免费的聊天机器人API
电机的简介
STM32 key chattering elimination - entry state machine thinking
Room cannot create an SQLite connection to verify the queries
Spark SQL UDF function
OpenCV经典100题
Novice entry depth learning | 3-6: optimizer optimizers
MCU通过UART实现OTA在线升级流程
MySQL存储引擎
Extension and application of timestamp