当前位置:网站首页>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
边栏推荐
猜你喜欢
免费的聊天机器人API
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
LeetCode 1598. Folder operation log collector
OpenCV经典100题
Room cannot create an SQLite connection to verify the queries
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
Keepalive component cache does not take effect
Browser reflow and redraw
How to solve the problems caused by the import process of ecology9.0
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
随机推荐
synchronized 和 ReentrantLock
Analysis of the combination of small program technology advantages and industrial Internet
新手入门深度学习 | 3-6:优化器optimizers
OpenCV经典100题
MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
An understanding of & array names
Opencv classic 100 questions
Extracting profile data from profile measurement
Codeforces Round #804 (Div. 2)【比赛记录】
孤勇者
Notepad + + regular expression replace String
Room cannot create an SQLite connection to verify the queries
logstash清除sincedb_path上传记录,重传日志数据
KDD 2022 | EEG AI helps diagnose epilepsy
Browser reflow and redraw
免费的聊天机器人API
【线上小工具】开发过程中会用到的线上小工具合集
LeetCode 8. String conversion integer (ATOI)
Spark DF adds a column
看抖音直播Beyond演唱会有感