The simple concept of the function of C language Kingdom Adventure

Kingdom Adventures Series

Article Directory (5)


Table of contents

Kingdom Adventures Series

Article Directory (5)

foreword

1. The basic concept of function

Second, the difference between calling an external function and the main() function

2.1 If we put the definition of the function behind, is it possible?

Summarize


foreword

Adventures in the Kingdom of C Language is our journey of learning C language and growing up from a novice to a master. In this chapter, we will initially explore the two knowledge levels of functions.


1. The basic concept of function

We have learned functions in mathematics. In fact, a function describes a relationship.
For example, it describes the relationship between y and x.
If y = 2*x+5, this describes the relationship between x and y.
Give x a value. , and then y becomes another value through this relationship.
This is a function in mathematics.
So what does the function in our c language look like?
The functions in the c language are very similar to the functions in our mathematics, which
can be expressed as y = f(x) function
 y = f(x)
     = 2*x+5
We call this logic function f(x) , f(x) This function describes the relationship between x and y

Second, the difference between calling an external function and the main() function

Now we want to write a code that calculates the sum of

We will explain the knowledge points in the code plus comments and the result running diagram:

int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);//scnaf()里面要取地址的
	//求和
	int s = a + b;//我们创建一个变量s,存放a+b求和得来的值
	printf("%d", s);//然后进行打印
	return 0;
}

We just did the sum directly in the main() function.
Can we create an external function to help us do the sum? Of course it is possible.

We will explain the knowledge points in the code plus comments and the result running diagram:

//这就是函数的定义
int Add(int x, int y)
 //每一个参数都对应着一个类型,每一个实参都对应着一个形参,跟跟下面的传参要匹配起来
//我们实现的Add函数,a和b传过去,是不是要有有东西接收啊
//x是接受a的值,y是接收b的值
{
	int z = x + y;//我们将x和y相加的和放到z里面去
	return z;//最后我们返回一个z给main()函数
}
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);//scnaf()里面要取地址的
	//创建一个叫做Add的外部函数进行求和
	int s = Add(a,b);//函数调用
	//我要想求a和b的和是不是要把a和b的值传到Add里面进行求和
	//这个时候Add函数才能知道自己要干什么,Add就是要把a和b的值求出来呀。
	//求出来是不是要把结果告诉main函数啊,这个时候Add会返回一个值,将这个值给s
	printf("%d",s );//然后进行打印
	return 0;
}

 The understanding of the external call function:
the result of calling the external function to perform the summation operation is the same as the result of the direct summation in the main() function. In
fact, we can think of the function as a processing plant.
When we give raw materials to the processing plant, After processing by the processing plant, we can get the product.
The function is very similar to the processing plant. Do we need to input parameter values ​​to the function when we call the function?
After calling the function, the function will give us a return value.
Why is it in main( ) The summation in it is just one line of code.
Why do you need to create a function outside the main() function, call it, and then return the value?
We need to open up the layout.
The code just now may be just one line of code,
but if we write a function in the future, we need many lines of code.
We can’t always write it once in main() as long as it is called. In that case, the function will be more redundant.
Therefore, we encapsulate this function into a function. Every time we call it, we only need one sentence to call this function. This
reduces the redundancy of the code a lot. Secondly, the code can be reused, which improves the efficiency of our code writing.

 

2.1 If we put the definition of the function behind, is it possible?

It is possible to put the code function in the back, but a warning will be reported when the code is compiled in the past,
because when the C language compiles this .c file, it scans from the first line of code downwards
. Go down to the main() function, and when the Add function is called, the compiler finds that the Add function has never been seen before. In fact, is there
, yes, behind. But when it scanned the Add() code, it didn’t see it, so it reported an alarm.
If we must put the function definition behind,
then we must make a statement in front of the function, so that no error will be reported. up

The function must be declared before the definition.
However, if the definition of the function is placed in front, no warning will be reported, because the definition of the function is a declaration. It is
very important to know how the function is written.
In a project, there can be multiple .c files,
but there are many There can only be one mian in each .c file

int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);//scnaf()里面要取地址的
	//创建一个叫做Add的外部函数进行求和
	int s = Add(a,b);//函数调用
	//我要想求a和b的和是不是要把a和b的值传到Add里面进行求和
	//这个时候Add函数才能知道自己要干什么,Add就是要把a和b的值求出来呀。
	//求出来是不是要把结果告诉main函数啊,这个时候Add会返回一个值,将这个值给s
	printf("%d",s );//然后进行打印
	return 0;
}
//将代码函数放到后面去,是可以的,但是当年去编译代码的时候就会报警告
//因为c语言对这种.c文件进行编译的时候,是从第一行代码,往下扫描的
//当扫描到main()函数的时候往下走,到了调用Add函数的时候,编译器发现之前根本就没有见过Add函数
//实际上有没有,有,在后面呢。但是在扫描到Add()那个代码的时候,它没有见过,所以就报了一个警报
//如果我们一定要将函数定义放到后面的话,
//那就一定要函数前面进行一个声明,这样就不会报错了
int Add(int x, int y)
{
	int z = x + y;
	return z;
}
//函数要先声明,在定义
//但是函数的定义放在前面就不报警告了呀,因为函数的定义就是一种声明
//知道函数是怎么写的很重要
//一个工程中,可以有多个.c文件
//但是多个.c文件中只能有一个mian

There is no problem adding a function declaration in front of the main function, and there will be no alarm 

//函数的声明
int Add(int x, int y);
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);//scnaf()里面要取地址的
	//创建一个叫做Add的外部函数进行求和
	int s = Add(a,b);//函数调用
	//我要想求a和b的和是不是要把a和b的值传到Add里面进行求和
	//这个时候Add函数才能知道自己要干什么,Add就是要把a和b的值求出来呀。
	//求出来是不是要把结果告诉main函数啊,这个时候Add会返回一个值,将这个值给s
	printf("%d",s );//然后进行打印
	return 0;
}
//将代码函数放到后面去,是可以的,但是当年去编译代码的时候就会报警告
//因为c语言对这种.c文件进行编译的时候,是从第一行代码,往下扫描的
//当扫描到main()函数的时候往下走,到了调用Add函数的时候,编译器发现之前根本就没有见过Add函数
//实际上有没有,有,在后面呢。但是在扫描到Add()那个代码的时候,它没有见过,所以就报了一个警报
//如果我们一定要将函数定义放到后面的话,
//那就一定要函数前面进行一个声明,这样就不会报错了
int Add(int x, int y)
{
	int z = x + y;
	return z;
}
//函数要先声明,在定义
//但是函数的定义放在前面就不报警告了呀,因为函数的定义就是一种声明
//知道函数是怎么写的很重要
//一个工程中,可以有多个.c文件
//但是多个.c文件中只能有一个mian

Summarize

The above is what I want to talk about today. This article only comprehensively introduces the simple concept of functions, which can make your adventure in the C language kingdom more interesting and fulfilling.

Guess you like

Origin blog.csdn.net/weixin_73466540/article/details/131491792