当前位置:网站首页>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
边栏推荐
- CDGA|工业互联网行业怎么做好数据治理?
- Common APIs in string
- [C language] function
- Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- [C language] step jumping problem [recursion]
- Basic knowledge of C language
- Implementation of markdown editor in editor.md
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- Labelme (I)
猜你喜欢

zfoo增加类似于mydog的路由

ssm中项目异常处理

English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages

KPMG China: insights into information technology audit projects of securities fund management institutions

Cloud computing notes part.2 - Application Management

Information management system and games based on C language

Saltstack advanced

There is a 'single quotation mark' problem in the string when Oracle inserts data

Prometheus deployment

String中常用的API
随机推荐
软考高级考试中有五大证书,哪个更值得考?
2022年下半年系统集成项目管理工程师认证8月20日开班
Leetcode Day2 consecutive numbers
How to write the SQL statement of time to date?
Know small and medium LAN WLAN
Labelme(一)
Const pointer of C language and parameter passing of main function
Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
Cloud computing notes part.2 - Application Management
leetcode day3 超过经理收入的员工
Source insight project import and use tutorial
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
[C language] advanced pointer exercise 1
Circular linked list OJ question
Thoroughly understand bit operations - shift left, shift right
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
C language operators and input and output
Leetcode Day5 delete duplicate email
Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
Function fitting based on MATLAB