当前位置:网站首页>Programming learning records - Lesson 6 [functions]
Programming learning records - Lesson 6 [functions]
2022-07-27 06:23:00 【Autumn mountain chariot God AE】
function
In computer science , A function is a piece of code in a large program , Consists of one or more statement blocks . It's responsible for a particular task , And compared to other code , With relative independence .
Generally, there will be input parameters and return values , Provide encapsulation of the process and hiding of details . These codes are usually integrated into software libraries .
Library function
void * memset ( void * ptr, int value, size_t num );Library function query tool :
Custom function
Like library functions ; Custom functions include There's a function name , Return value types and function parameters .
Unlike library functions , User defined functions are designed by programmers , Give programmers a lot of room to play .
Custom functions are defined by Return type , Function parameter , Statement item composition
Arguments and formal parameters
Shape parameter : Formal parameters are formal parameters , Formal parameters refer to the variables in brackets after the function name , Because formal parameters are only instantiated when the function is called ( Allocate memory units ), Only valid in functions , After the function call is completed, it is destroyed .
Actual parameters : Arguments are actual parameters , That is, the value actually passed to the function , The argument can be : Constant 、 Variable 、 expression 、 Functions, etc . Whatever the type of argument is , When you make a function call , They all have to have certain values , So that we can send these values to the shape ginseng .
Function call
1. Value transfer call
A formal parameter is essentially a temporary copy of an argument , Just pass the value to the function , The modification of formal parameters in the function will not affect the actual parameters .
2. Address call
Address calling is a form of creating variables outside the function and passing the memory address to the function parameters , Make the variables inside and outside the function establish a real connection , Thus, the variables outside the function can be modified directly in the function .
You can modify variables inside functions , example :
// Exchange integer
int exchange(int* x, int* y)
{
int tmp = *x;
*x = *y;
*y = tmp;
return 0;
}
int main()
{
int i = 0;int j=0;
scanf("%d", &i, &j);
exchange(&i, &j);
printf("%d %d", i, j);
return 0;
}It realizes the exchange of the values of two variables inside the function .
Nested calls and chained access to functions
Nested calls
Combine functions according to their own needs , A function can be called inside another function , It is called nested call , Functions can be called nested , But you can't nest definitions , That is, you cannot define another function within a function , in addition , Call another function within one function , This function to be called is required to be defined before .
Chained access
Pass the value returned by one function to another function as a parameter . It is called chain access .
example :
int main()
{
printf("%d", printf("%d", printf("%d", 43)));
return 0; }
The result of printing is 4321, because printf The value returned by the function is the number of characters printed .
Practice code today :
// Prime judgment
int sushu(int i)
{
int j = 0;
for (j = 2;j < i;j++)
{
if (i % j == 0) { return 0; }
else if (j == i - 1) { return 1; }
}
}
int main()
{
for (int i = 100;i <= 200;i++)
{
if (sushu(i) == 1) { printf("%d ", i); }
}
return 0;
}
// Leap year judgment
int run(int year)
{
if (year % 4 == 0 && year % 100 != 0) return 1;
else if (year % 400 == 0) return 1;
else return 0;
}
int main()
{
int year = 0;
scanf("%d", &year);
if (run(year) == 1) printf(" It's a leap year ");
else printf(" It's not a leap year ");
return 0;
}
// Exchange integer
int exchange(int* x, int* y)
{
int tmp = *x;
*x = *y;
*y = tmp;
return 0;
}
int main()
{
int i = 0;int j=0;
scanf("%d", &i, &j);
exchange(&i, &j);
printf("%d %d", i, j);
return 0;
}
// Multiplication table
int biao(int i)
{
int m = 0;int n = 0;
for (m = 1;m <= i;m++)
{
for (n = 1;n <= m;n++)
{
printf("%d * %d=%2d ", n, m, m * n);
}
printf("\n");
}
return 0;
}
int main()
{
int i = 0;
scanf("%d", &i);
biao(i);
return 0;
}边栏推荐
猜你喜欢
随机推荐
多线程常见锁的策略
Understand the pointer in a picture
Unity engine starts to migrate from mono to.Net coreclr
ROS中的头文件与源文件
[dynamic planning - steel bar cutting]
Unity practical tips (updating)
Related knowledge of internal classes
TF coordinate transformation
ROS工作空间覆盖
Dynamic programming for solving problems (1)
Reading and writing of file content - data flow
What is the difference between single line and three line when renting servers in Hong Kong?
多线程CAS、synchronized锁原理 、JUC以及死锁
数据库的索引和事务(重点)
软件测试基础概念篇
wireshark图形界面介绍
数据库在终端的增删改查
Force deduction problem solving monotonous stack
Communication mechanism cases
IP核之RAM









