当前位置:网站首页>[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
2022-07-03 05:38:00 【Sobtemesa】
“ The boy looked back Laugh, I'm not fast enough to keep up .”
Catalog
4. The parameters of the function
7. Nested calls and chained access to functions
1. What is a function
A program may be used many times , If you write such a repeated piece of code every time , It's not only time-consuming and laborious 、 It's easy to make mistakes , And it's troublesome to give it to others , therefore C Language provides a function , It allows us to encapsulate common code into a separate module in a fixed format , As long as you know the name of this module, you can reuse it , This module is called function (Function). Function 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 library .
2. Library function
C Function classification in language :
1. Library function
2. Custom function
Why are there library functions ?
Before we learn C In language programming , It's a little bit like scanf,printf,strcpy,strlen And other basic functions , They are not business code , In the process of development, every programmer will use , in order to Support portability and improve program efficiency , therefore C The language base library provides a series of similar library functions , It is convenient for programmers to develop software .
notes : Use library functions , must #include The corresponding header file .
Take a chestnut :
1.
int main()
{
char arr1[] = "#####################";
char arr2[] = "hello world";
strcpy(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
2.
int main()
{
char arr2[] = "hello world";
memset(arr2, 'x', 5);
printf("%s\n", arr2);
return 0;
}
notes :memset It is initialized in bytes .
3. Custom function
Except for library functions , We can still Write your own functions , Expand the function of the program . The function written by yourself is called user-defined function . Custom functions and library functions are written and used in exactly the same way , It's just written by different institutions .
ret_type fun_name ( para1 )
{ statement;// Statement item }
ret_type Return type fun_name Function name para1 Function parameter
Take a chestnut :
int Max(int a, int b)
{
return (a > b ? a : b);
}
int main()
{
int a = 0;
int b = 0;
scanf("%d%d", &a, &b);
int max=Max(a, b);
printf("%d", max);
return 0;
}
void Change(int *a, int *b)
{
int tmp = 0;
tmp = *a;
*a =*b;
*b = tmp;
}
int main()
{
int a = 0;
int b = 90;
printf("a:%db:%d\n", a, b);
Change(&a,&b);
printf("a:%db:%d\n", a, b);
return 0;
}
4. The parameters of the function
Actual parameters :
The parameters that are actually passed to the function . It can be : Constant , Variable , expression , Functions, etc . When you make a function call , They all have to have certain values , In order to pass these values to the parameter .
Shape parameter :
When you define a function , Variables in parentheses after function names , because Formal parameters are instantiated only when called ( Allocate space ), So it's called formal parameter . Formal parameters will be destroyed after the function is called , Therefore, formal parameters are only valid in functions .
5. Function call
Value transfer call :
Function parameters and arguments occupy different memory blocks , Changes to formal parameters do not affect arguments . After the formal parameter is instantiated, it is actually equivalent to a temporary copy of the argument .
Address call :
Address calling is a calling method that passes the memory address of the variable created outside the function to the function parameters .
This way of parameter transfer can establish a real connection between the function and the variables outside the function . In other words, the variables outside the function can be directly manipulated inside the function .
6. practice
1. Write a function to determine whether a number is a prime .
int is_prime(int i)
{
for (int j = 2; j <= sqrt(i); j++)
{
if (i%j == 0)
{
return 0;
}
}
return 1;
}
int main()
{
for (int i = 100; i <= 200; i++)
{
if (is_prime(i))
{
printf("%d ", i);
}
}
return 0;
}
2. Write a function to determine whether a year is a leap year .
int is_leap_year(int y)
{
return((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0));
}
int main()
{
int y = 0;
int i = 0;
scanf("%d", &y);
if (is_leap_year(y))
{
printf(" It's a leap year ");
}else
printf(" It's not a leap year ");
return 0;
}
3. Write a function , To realize binary search of an ordered array .
int binary_search(int k,int arr[], int sz)
{
int left = 0;
int right = sz - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid]>k)
{
right = mid - 1;
}
else if (arr[mid] == k)
{
return mid;
}
}
return -1;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int k = 7;
int sz = sizeof(arr) / sizeof(arr[0]);
int tmp=binary_search(k,arr, sz);
if (tmp == -1)
{
printf(" Can't find ");
}
else
{
printf(" eureka , Subscript to be %d\n", tmp);
}
return 0;
}
4. Write a function , Every time you call this function , Will be num The value of the increase 1.
int Count(int* pnum)
{
(*pnum)++;
}
int main()
{
int num = 0;
Count(&num);
Count(&num);
Count(&num);
Count(&num);
printf("%d", num);
return 0;
}
7. Nested calls and chained access to functions
Nested calls
void Print()
{
printf("hello world\n1");
}
void test()
{
Print();
}
int main()
{
test();
return 0;
}
Functions can be called nested, but not defined nested .
// Error model
void Print()
{
printf("hello world\n1");
}
int main()
{
void test()
{
Print();
}
return 0;
}
Chained access
Chained access is a function of Return value As Parameters of another function .
#include <stdio.h>
int main()
{
printf("%d", printf("%d", printf("%d", 43)));
// The result is what ?
return 0;
}
analysis : 1. Execute first printf("%d", 43), Its return value is 2
2. Re execution printf("%d",2), Its return value is 1
3. Re execution printf("%d",1)
边栏推荐
- (subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
- 期末复习(Day5)
- Win10 install pytullet and test
- 大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
- Latest version of source insight
- 联想R7000显卡的拆卸与安装
- [untitled]
- Can altaro back up Microsoft teams?
- Get and monitor remote server logs
- ES7 easy mistakes in index creation
猜你喜欢
Deploy crawl detection network using tensorrt (I)
mapbox尝鲜值之云图动画
一起上水硕系列】Day 9
SimpleITK学习笔记
[Shangshui Shuo series together] day 10
Common interview questions of microservice
2022.DAY592
Training method of grasping angle in grasping detection
求质数的方法
Intégration profonde et alignement des séquences de protéines Google
随机推荐
2022.6.30DAY591
Interview question -- output the same characters in two character arrays
Why should we rewrite hashcode when we rewrite the equals method?
Altaro virtual machine replication failed: "unsupported file type vmgs"
Kubernetes resource object introduction and common commands (V) - (configmap)
Redis encountered noauth authentication required
ES7 easy mistakes in index creation
Map的扩容机制
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
Introduction to redis and explanation of data types
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
2022.7.2 模拟赛
Talk about how to use p6spy for SQL monitoring
Brief introduction of realsense d435i imaging principle
Sophomore dilemma (resumption)
Best practices for setting up altaro VM backups
[Shangshui Shuo series together] day 10
Calculation method of AUC
ES 2022 正式发布!有哪些新特性?
Redis expiration elimination mechanism