当前位置:网站首页>C language -- 17 function introduction
C language -- 17 function introduction
2022-06-22 22:55:00 【Try!】
about “ function ”, The knowledge to be mastered includes the following :
- Definition of function
- Library function
- Custom function
- Function parameter
- Function call
- Nested calls and chained access to functions
- Function declaration and definition
- Function recursion
1、 What is a function ?
In mathematics, we often see the concept of function and use it , Such as f(x)=2x. So in C What is the definition of a function in a language ? How to use it ? In Wikipedia, the definition of function is : Subroutines .( in other words , A subroutine is a function )
- In computer science , Subroutines (subroutine,procedure,function,routine,method,subprogram,callable unit), It's a piece of code in a large program , Consists of one or more statement blocks . It is responsible for completing a specific task , And compared to other code , With relative independence .
- Functions usually have input parameters and return values , Provide encapsulation of the process and hiding of details , These codes are usually integrated into software libraries .

The above figure means that for a function , You need input parameters to get output parameters , Don't pay attention to the implementation process and details of the function when using it , If you know its function, just take it and use it .
2、C Classification of functions in language
C Functions in language are divided into Library function and Custom function .
(1) Library function Why do library functions exist ?
Before we learn C In language programming , You always want to see the results in the print results window after a code is written , At this time, we will frequently use a function , That is to print the information to the screen in a certain format ( This is also printf The function of ); In the process of programming , We may also do some string copying frequently (strcpy); You will also use calculation functions , For example, calculation n Of k Such an operation to the power (pow).
Basic functions like those described above , They are not business code , In the development process, many people may use , In order to support portability and improve program efficiency ,C A series of similar library functions in the basic library of the language , Facilitate software development . Provides several ways to learn library functions 、 website
- cplusplus
- Online tools MSDN
- cppreference Chinese display
C Library functions commonly used in languages - Input and output (IO) function :printf,scanf,getchar,putchar etc.
- String manipulation functions :strcmp,strlen etc.
- Character manipulation functions :toupper function ( Lower case to upper case function )
- Memory manipulation function :memcpy,memcmp,memset etc.
- Time 、 Date function :time etc.
- Mathematical functions :sqrt,pow etc.
- Other library functions
With cplusplus Web site as an example , Introduce how to learn library functionsThere is one thing that needs special attention , That is, using library functions must include #include The header file of a
strcpy: char *strcpy (char *destination, const char *source);

Example 1 :strcpy Function to copy a string , take arr2 Copy content from to arr1 In the middle
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = {
0 };
char arr2[] = "hello,CSDN";
strcpy(arr1,arr2);// take arr2 Copy the contents of to arr1
printf("%s", arr1);// Print arr1 This string %s--- Print in string format
return 0;
}
The operation results are as follows :
hello,CSDN
memset: void *memset(void *ptr,int value,size_t num)

Example 2 :memset function
memory It is translated into memory when translating at ordinary times , It is translated into memory in computer science , therefore memset stay C It is translated into “ Memory settings ”.
int main()
{
char arr[] = "hello CSDN";// Now I want to put arr The first five characters inside are replaced by x
memset(arr,'x',5);
printf("%s",arr);
return 0;
}
Running results :
xxxxx CSDN
(2) Custom function
Library functions don't do everything , It still needs programmers to do some “ Custom function ” To improve the functions required in the program . Custom functions are the same as library functions , All have function names 、 Return value types and function parameters . But the difference is , We need to design these by ourselves .
边栏推荐
- 2021-08-21
- What are the methods of software stress testing and how to select a software stress testing organization?
- 2020-12-04
- 2020-12-20
- R language builds a binary classification model based on H2O package: using H2O GLM constructs regularized logistic regression model and uses H2O AUC value of AUC calculation model
- . Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
- Atcoder abc256 full problem solution (interval merging template, matrix fast power optimization DP, line segment tree...)
- Grafana report display of sentinel based high availability current limiting system
- leetcode. 11 --- container with the most water
- In a frame because it set 'X-FRAME-OPTIONS' to' deny '
猜你喜欢

Summary of just meal with 900W increase in playback and acclaim from station B users

The method of making videos of knowledge payment system support m3u8 format playback
![Next permutation [give play to subjective initiative to discover laws]](/img/bb/262e1a21e4babb8d221d737ced3bcc.png)
Next permutation [give play to subjective initiative to discover laws]

In the middle of the year, we will promote the integration of worry free, and the value-added package will be reduced by 6

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

2021-04-14

Grafana report display of sentinel based high availability current limiting system
![[geometric vision] 4.2 piecewise linear transformation](/img/1e/a810f4d7e9a6a34647b5cb56fdde67.png)
[geometric vision] 4.2 piecewise linear transformation

2021-08-26

2021-08-22
随机推荐
Uniapp applet mall develops thinkphp6 points mall, group purchase and seckill packaged app
pycharm 配置远程连接服务器开发环境
Palindromes (simple version)
volume rendering
Task management of embedded development foundation (thread management)
The link added in the bottom menu cannot jump to the secondary page
2021-04-05
MySQL functions
Greedy distribution problem (2)
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
2021-04-14
Greedy interval problem (1)
2021-04-16
LinkedList 源码解析
A case of 94 SQL optimization (the writing method used is often rejected)
2021-04-16
Lua-- use of data types, variables, loops, functions and operators
2021-08-21
Talk about SQL profile again: can I fix the execution plan?
Dragon City in Europe | National Geographic the most romantic and safe destination in the world