当前位置:网站首页>7. Functions of C language, function definitions and the order of function calls, how to declare functions, prime examples, formal parameters and arguments, and how to write a function well
7. Functions of C language, function definitions and the order of function calls, how to declare functions, prime examples, formal parameters and arguments, and how to write a function well
2022-07-28 20:05:00 【A programmer who loves playing badminton】
The order of function calls and function definitions
If the function call is written in front of the function definition , You must add a function pre declaration
Function pre declaration function :
- Tell compiler , The several letters that may appear soon represent a function
- Tell the compiler about the possible formal parameters and return values of the function represented by several letters
- A function declaration is a statement , You must add a semicolon at the end
- The library function is declared through #include< The name of the file where the library function is located .h> To achieve
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
void judge_Prime(int val);
int main(int argc, char const *argv[])
{
printf(" Please enter the range you want to test :\n");
int test_number;
scanf("%d", &test_number);
printf("1~%d The prime between is :\n");
for (int i = 1; i <= test_number; i++)
{
judge_Prime(i);
}
return 0;
}
// Judge whether a function is prime
void judge_Prime(int val)
{
int i;
for (i = 2; i <= sqrt(val); i++)
{
if (val % i == 0)
{
break;
}
}
if (i <= sqrt(val))
{
printf("%d is not a Prime\n", val);
}
else
{
printf("%d is a Prime\n", val);
}
}For example, this example of judging prime numbers , We write judge_Prime(int val) function , because main Function in front , We're executing main Function , I do not know! judge_Prime(int val) What is this function , So you need to state in advance .
Formal parameters and actual parameters
Formal parameters are when we define functions , Parameters written in parentheses
The argument is when we call the function , Incoming parameter
The number of formal and actual parameters must be the same , The positions should correspond one by one , Data types should be compatible
void test(int a){
printf("%d",a);
}
int main(){
test(100);
return 0;
}For example, this program ,test function () Medium a Is the parameter ,main Function test(100),100 It's a real parameter
The function of formal parameter and actual parameter is to transfer data . When a function call occurs , The main function transfers the value of the real parameter to the formal parameter of the called function, so as to realize the data transfer from the main function to the called function .
How to write a function ?
The function of a function should be as independent as possible , A single , To ensure the portability of functions
边栏推荐
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- Tencent cloud deployment lamp_ Experience of building a station
- CodeIgnier框架实现restful API接口编程
- Leetcode day2 连续出现的数字
- String中常用的API
- Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
- C language array and bubble sort
- 基于 MinIO 对象存储保障 Rancher 数据
- Implementation of strcat in C language
- Edge detection and connection of image segmentation realized by MATLAB
猜你喜欢

数字图像理论知识(一)(个人浅析)
![[C language] Gobang game [array and function]](/img/73/1e708b0640c2dbc86393206840b059.png)
[C language] Gobang game [array and function]

河北:稳粮扩豆助力粮油生产提质增效

Saltstack advanced

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation

Edge detection and connection of image segmentation realized by MATLAB

云计算笔记part.2——应用管理

【NPP安装插件】

editor.md中markdown编辑器的实现
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]
随机推荐
MySQL8 Encrypting InnoDB Tablespaces
Implementation of markdown editor in editor.md
[wechat applet development] page navigation and parameter transmission
MIR专题征稿 | 常识知识与推理:表示、获取与应用 (10月31日截稿)
XOR operation and its usage
云计算笔记part.2——应用管理
Two methods to judge the size end
Implementation of strcat in C language
Source insight project import and use tutorial
Why is there no log output in the telnet login interface?
[网络]跨区域网络的通信学习IPv4地址的分类和计算
个人博克系统登录点击图形验证码的集成与实现
Use of strtok and strError
Leetcode Day1 score ranking
Prometheus deployment
High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
河北邯郸:拓展基层就业空间 助力高校毕业生就业
Overcome the "fear of looking at teeth", and we use technology to change the industry
What is the process of swing event processing?
【微信小程序开发】页面导航与传参