当前位置:网站首页>[C language - zero foundation lesson 9] love and hate in functions
[C language - zero foundation lesson 9] love and hate in functions
2022-07-27 09:15:00 【Super Daxiong】
Preface
Blogger :Super Daxiong ( A cute new blogger )
C Language column :0 Basic science C Language column
LeetCode special column :LeetCode special column
This issue is about love and hate in functions , If there is any mistake, please point out thank you !
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
Catalog
The return value of the function
The parameters of the function
The value transfer of a function
What is a function
Let's take an example , You are the boss now. You ask your secretary to complete a task. You can tell him to do a task , Or don't tell him to do a task ( Hint him ) Of course you have to be the boss . As long as you are the boss, you can ask the Secretary to do a task unlimited times , And he didn't complain .
Let's see The literal definition of the function :
A function is a piece of code that can be reused , Used to perform a function independently , It can receive data transmitted by users , You can also not accept . The function receiving user data shall specify parameters when defining , If user data is not received, there is no need to indicate , According to this point, functions can be divided into parametric functions and nonparametric functions .
Definition of function
The format of the function definition :
return type Function name ( Parameters , Parameters ,……)
{
Execute statement
}
#include<stdio.h>
void fun (){
printf("HelloWold");
}
int main(){
fun();
return 0;
} Be careful :
1. Functions should be defined before use
2.C The source program starts with the main function , End with the main function .( The main function is main function )
3. Arguments are responsible for passing , Formal parameters are responsible for accepting , Assign the argument to the corresponding formal parameter when calling the function
Arguments are actual parameters , Formal parameters are formal parameters .
The actual parameters are in the main function , The function that enters the call cannot be used . You need to pass parameters , The scope of the shape parameter is within the function being called , This function doesn't work . The formal parameter cannot change the value of the actual parameter .
Be careful : Functions can be called nested , However, nested calls are not allowed
Declaration of functions
Function should be defined first and then called. , If the call to the function occurs before the definition , The function is declared .
#include<stdio.h>
void fun();// Declaration of functions
int main(){
fun();
return 0;
}
void fun (){
printf("HelloWold");
} The return value of the function
There is a return value
form 1 : return( expression )
#include<stdio.h>
int fun();// Declaration of functions
int main(){
printf("%d",fun()) ;
return 0;
}
int fun (){
return (1+2);
} form 2 :return expression ;
#include<stdio.h>
int fun();// Declaration of functions
int main(){
printf("%d",fun()) ;
return 0;
}
int fun (){
return 3+2;
} form 3 : adopt return sentence , Function can only return one value
#include<stdio.h>
int fun();// Declaration of functions
int main(){
printf("%d",fun()) ;
return 0;
}
int fun (){
return 10;
} The default return value type is int type
No return value void
#include<stdio.h>
void fun();// Declaration of functions
int main(){
fun();
return 0;
}
void fun (){
printf("HelloWold");
} Be careful
void A defined function body cannot have return sentence
void A defined function cannot be used to assign a value to a variable
Return value refers to return The value behind
The parameters of the function
Classification of functions :
Shape parameter : Formal parameters when defining a function
Actual parameters : Parameters when calling the function
Parameter passing
- Arguments are passed unidirectionally to shape parameters
- Actual parameters , The order and number of formal parameter types correspond to each other one by one
- No space is occupied when defining formal parameters , System allocated space at call time
Function call
Function call format
1. The statement calls
Fun(x,y);
2. Expression call
A=fun(x,y);
Parameter call
Call the return value of the function as an argument , Parameter calls called functions
#include<stdio.h>
int fun (int x,int y){
return y-x;
}
int main(){
int a=10,b=5;
printf("%d",fun(a,fun(a,b))) ;
return 0;
}
Recursively call
Recursive conditions :
1. The main functions of the function
2. Clear end conditions
3. Find out the equivalent relation of function
#include<stdio.h>
int fun (int x){
if(x==1){// The end condition
return 1;
}
return x+fun(x-1); // Function relation
}
int main(){
printf("%d",fun(10));
}
The value transfer of a function
The parameter transfer of function is divided into address transfer ( The pointer will say ) And value passing
Be careful :
The type of the argument can be different from the argument type , The formal parameter type shall prevail
Storage units occupied by arguments and formal parameters
Formal parameters need to be defined separately , It's a temporary variable , Allocate a storage unit to it when called , Release its storage unit after the function call .
The default type of function is int type , Omission .
The type of the return value of a function is determined by the return value type when the function is defined .
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
边栏推荐
- How to register code cloud account
- 基于ArkUI eTS开发的坚果笑话(NutJoke)
- Deep understanding of Kalman filter (3): multidimensional Kalman filter
- 5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises
- 苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击
- PyTorch自定义CUDA算子教程与运行时间分析
- Explicit animation in arkui
- BOM的常用操作和有关获取页面/窗口高度、宽度及滚动的兼容性写法
- A survey of robust lidar based 3D object detection methods for autonomous driving paper notes
- Programming style
猜你喜欢

CUDA programming-04: CUDA memory model

Ctfshow ultimate assessment

NPM install error forced installation

C# 窗体应用常用基础控件讲解(适合萌新)

Explanation of common basic controls for C # form application (suitable for Mengxin)

Apple cut its price by 600 yuan, which was almost a devastating blow to the collapse of its domestic flagship mobile phone

PyQt5快速开发与实战 4.1 QMainWindow

罗克韦尔AB PLC 通过RSLinx Classic与PLC建立通信的具体方法步骤

js call和apply

Deep understanding of Kalman filter (3): multidimensional Kalman filter
随机推荐
BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans
"Weilai Cup" 2022 Niuke summer multi school training camp 1
函数防抖节流
Rewrite the tensorrt version deployment code of yolox
ArkUI框架中的两个小技巧
罗克韦尔AB PLC 通过RSLinx Classic与PLC建立通信的具体方法步骤
【进程间通信IPC】- 信号量的学习
[cloud native kubernetes practice] deploy the rainbow platform under the kubernetes cluster
Pymongo fuzzy query
Summary of traversal methods
易语言编程: 让读屏软件可获取标签控件的文本
linux安装和远程连接mysql记录
Storage and computing engine
Can "Gulangyu yuancosmos" become an "upgraded sample" of China's cultural tourism industry
CUDA Programming -03: thread level
ES6 new symbol data type
Deep understanding of Kalman filter (2): one dimensional Kalman filter
Explanation of common basic controls for C # form application (suitable for Mengxin)
CUDA programming-04: CUDA memory model
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生