当前位置:网站首页>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
边栏推荐
- XML Configuration File
- Leetcode:20220213 week race (less bugs, top 10% 555)
- Promise
- Spark SQL null value, Nan judgment and processing
- The value of applet containers
- Basic introduction and source code analysis of webrtc threads
- Uniapp development, packaged as H5 and deployed to the server
- 如何制作自己的機器人
- Lone brave man
- FFmpeg抓取RTSP图像进行图像分析
猜你喜欢

notepad++正則錶達式替換字符串

Classic CTF topic about FTP protocol

Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
![[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte](/img/61/73becfc3b46669d31b0cf334aa54f2.jpg)
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte

Ffmpeg captures RTSP images for image analysis

Classical concurrency problem: the dining problem of philosophers

Introduction of motor

从 1.5 开始搭建一个微服务框架——调用链追踪 traceId

Leetcode 450 deleting nodes in a binary search tree
![Atcoder beginer contest 258 [competition record]](/img/e4/1d34410f79851a7a81dd8f4a0b54bf.gif)
Atcoder beginer contest 258 [competition record]
随机推荐
STM32按键消抖——入门状态机思维
LeetCode 6005. The minimum operand to make an array an alternating array
Opencv classic 100 questions
Analysis of the combination of small program technology advantages and industrial Internet
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
Go learning - dependency injection
Spark AQE
Spark SQL null value, Nan judgment and processing
Why can't mathematics give machine consciousness
KDD 2022 | 脑电AI助力癫痫疾病诊断
Spark SQL空值Null,NaN判断和处理
LeetCode 6004. Get operands of 0
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
Notepad++ regular expression replacement string
Introduction of motor
Spark获取DataFrame中列的方式--col,$,column,apply
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
Ffmpeg captures RTSP images for image analysis
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet