当前位置:网站首页>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 .
     Insert picture description here
    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 functions
    There 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);

 Insert picture description here
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)

 Insert picture description here
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 .

原网站

版权声明
本文为[Try!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222045094317.html