当前位置:网站首页>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
边栏推荐
- curlpost-php
- [simple implementation of file IO]
- NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
- How to make your own robot
- Extension and application of timestamp
- AtCoder Beginner Contest 254【VP记录】
- Comment faire votre propre robot
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- Room cannot create an SQLite connection to verify the queries
- The value of applet containers
猜你喜欢
Opencv classic 100 questions
常用API类及异常体系
Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
小程序技术优势与产业互联网相结合的分析
Free chat robot API
LeetCode 1189. Maximum number of "balloons"
Spark AQE
Search (DFS and BFS)
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
随机推荐
电机的简介
KDD 2022 | 脑电AI助力癫痫疾病诊断
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
程序员成长第九篇:真实项目中的注意事项
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
Spark获取DataFrame中列的方式--col,$,column,apply
《编程之美》读书笔记
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
XML配置文件
Leetcode 44 Wildcard matching (2022.02.13)
The value of applet containers
STM32按键消抖——入门状态机思维
Search (DFS and BFS)
LeetCode 1189. Maximum number of "balloons"
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
几百行代码实现一个 JSON 解析器
Lone brave man
Idea远程提交spark任务到yarn集群