当前位置:网站首页>[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)
边栏推荐
- 获取并监控远程服务器日志
- Common interview questions of microservice
- About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller
- 一起上水硕系列】Day 9
- Xaml gradient issue in uwp for some devices
- Training method of grasping angle in grasping detection
- Kubernetes resource object introduction and common commands (V) - (configmap)
- 2022.7.2day594
- Notepad++ wrap by specified character
- How to install and configure altaro VM backup for VMware vSphere
猜你喜欢

How to set up altaro offsite server for replication

"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()

Go practice - gorilla / handlers used by gorilla web Toolkit

Communication - how to be a good listener?

Export the altaro event log to a text file

Configure DTD of XML file

Progressive multi grasp detection using grasp path for rgbd images

(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)

Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
![[Shangshui Shuo series together] day 10](/img/a3/e8b9df588bef67ead925813a75c8c0.png)
[Shangshui Shuo series together] day 10
随机推荐
Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
ES7 easy mistakes in index creation
The IntelliJ platform completely disables the log4j component
Simpleitk learning notes
Xaml gradient issue in uwp for some devices
聊聊如何利用p6spy进行sql监控
Go practice -- use redis in golang (redis and go redis / redis)
Introduction to redis and explanation of data types
Redis Introduction et explication des types de données
32GB Jetson Orin SOM 不能刷机问题排查
【无标题】
EMD distance - example of use
Configure DTD of XML file
AtCoder Beginner Contest 258(A-D)
Go practice -- gorilla / websocket used by gorilla web Toolkit
一起上水碩系列】Day 9
Redis使用Lua脚本简介
期末复习(day3)
Capacity expansion mechanism of map