Function - C (on)

foreword

How to organize functions? C is designed to use functions as building blocks, self-contained units of program code that accomplish specific tasks. This article will introduce the structure, usage and details behind the function in detail. Hope to help readers further understand the C function.

1. What is a function

  • A function is a certain part of code in a large program, consisting of one or more statement blocks, responsible for completing a specific task, and compared with other codes, it is relatively independent.
  • Functions generally have input parameters and return values, providing encapsulation and hiding of details of the process, and these codes are usually integrated into software libraries.

2. Library functions

Why are there library functions

In the C language, there are some functions that describe basic functions such as printf() and strcpy(). They are not business codes, and every programmer may use them during the development process. In order to support portability and improve program efficiency , the basic library of C language provides a series of similar library functions to help programmers in software development.

So how to learn library functions?

www.cplusplus.com
http://en.cppreference.com (English version)
http://zh.cppreference.com (Chinese version)
MSDN(Microsoft Developer Network)

The following are two specific examples:
1. strcpy
insert image description here
2. memset
insert image description here

3. Custom functions

Library functions can't do everything, otherwise why should programmers do it? So more important is the custom function.
Like library functions, custom functions have function names, return value types, and function parameters . But the difference is that these are designed by ourselves, giving programmers more room to play.

Let's take an example

Write a function that finds the maximum of two integers.

#include <stdio.h>
//自定义函数,get_max函数设计
int get_max(int x, int y)
{
    
    
	return x > y ? x : y;
}

int main()
{
    
    
	int num1 = 0;
	int num2 = 0;
	//输入
	scanf("%d %d", &num1,&num2);
	//计算
	int max = get_max(num1, num2);
	//输出
	printf("max=%d\n", max);
}

give another example

Write a function to swap the contents of two integer variables

//以下写法是错误的
#include <stdio.h>
//形式参数——形参
void Swap(int a, int b)
{
    
    
	int tmp = a;
	a = b;
	b = tmp;
}

int main()
{
    
    
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	printf("交换前: a=%d b=%d\n", a, b); //实际参数——实参
	// (传值调用) 在这种设计中,把实参传给形参时
	//形参相当于实参的一份临时拷贝
	//所以对形参的修改不会影响实参
	Swap(a, b);
	printf("交换后:a=%d b=%d\n", a, b);
	return 0;
}
![运行结果](https://img-blog.csdnimg.cn/fe1628f8f3294228983a44aa3ae9a135.png#pic_center)

//正确学法
#include <stdio.h>
void Swap(int* a, int* b)
{
    
    
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int main()
{
    
    
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	printf("交换前: a=%d b=%d\n", a, b);
	Swap(&a, &b);
	// (传址调用)通过形参的指针可以能过访问函数外部的变量
	//并对该变量进行操作
	printf("交换后:a=%d b=%d\n", a, b);
	return 0;
}
![运行结果](https://img-blog.csdnimg.cn/2b4c2ac9d50d4cb3a51125745805173b.png#pic_center)

4. Function parameters

4.1 Actual parameters (actual parameters)

The parameters actually passed to the function are called actual parameters.
Actual parameters can be: variables, constants, expressions, functions, etc.
No matter what type of parameters the actual parameters are, they must have definite values ​​when the function is called, so that these values ​​can be passed to the formal parameters.

int get_max(int x, int y)
{
    
    
	int z = (x > y ? x : y);
	return z;
}

int main()
{
    
    
	int a = 0;
	int b = 0;
	//输入
	scanf("%d %d", &a, &b);//3 5
	//使用函数来完成
	int m = get_max(a, b);//变量

	m = get_max(a, 7);//变量,常量

	m = get_max(a, 2+3);//表达式

	m = get_max(a, get_max(4, 8));//函数的参数是函数调用

	//输出
	printf("较大值是:%d\n", m);

	return 0;
}

4.2 Formal parameters (formal parameters)

Formal parameters refer to the variables in parentheses after the function name, because formal parameters are only instantiated (allocated memory units) when the function is called, so they are called formal parameters.
Formal parameters are destroyed when the function call is complete, so formal parameters are only useful within functions.

insert image description here
Here we can see that when the Swap1() function is called, x and y have their own space and have exactly the same content as the actual parameters, while when the Swap2() function is called, the addresses of px and py are the same as the actual parameters.
So we can simply think that: After the formal parameter is instantiated, it is actually equivalent to a temporary copy of the actual parameter .

5. Function calls

5.1 Call by value

  • The formal parameters and actual parameters of the function occupy different memory blocks, and the modification of the formal parameters will not affect the actual parameters.

insert image description here
insert image description here

5.2 Call by reference

  • Call by address is a way to call a function by passing the memory of the variable created outside the function to the function parameter.
  • This way of passing parameters can establish a real connection between the function and the variables outside the function , that is, the inside of the function can directly manipulate the variables outside the function .

insert image description here
insert image description here

end

This is the end of this article. If it is helpful to you, remember to like and pay attention! thank you for your support! !
Function - C (Part 2)

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/130535744