The return value of a C language function

Through the function call, the calling function can get a certain value, which is the function value (the return value of the function).
1. The return value of the function is obtained through the return statement in the function.
The return statement brings a certain value from the called function back to the calling function. If you need to bring back a function value from the called function (for use by the calling function), the called function must contain a return statement. If you don't need to bring back the function value from the called function, you don't need the return statement.
A function can have more than one return statement, which return statement is executed, and which return statement takes effect. The value after return can also be an expression. For example

Max(int x,int y)
{
    
    
	return (x>y?x:y);
}

2. The type of function value
Since the function has a return value, this value should of course belong to a certain type, and the type of the function value should be specified when defining the function. For example, the following are the first lines of the 3 functions:

int Max(float x,float y)    //函数值为整型
char Letter(char c1,char c2)     //函数值为字符型
double Min(int x,int y)     //函数值为双精度型

Note: When defining a function, specify the function type.
3. The function type specified when defining the function should generally be consistent with the expression type in the return statement.
If the type of the function value is inconsistent with the value of the expression in the return statement, the function type shall prevail. For numeric data, type conversion can be performed automatically. That is, the function type determines the type of the return value.
Example: Change the variable z defined in the Max function to float type. The function return value type is different from the specified function type.

int Max(float x, float y)
{
    
    
	float z;
	if (x > y)
	{
    
    
		z = x;
	}
	else
		z = y;
	return z;
}
int main()
{
    
    
	float a, b;;
	printf("输入要比较的两个整数:\n");
	scanf("%f%f", &a, &b);
	int c;
	c = Max(a, b);
	printf("Max=%d", c);
	return 0;
}

Output:
Please add a picture description
[Program Analysis]
The formal parameter of the Max function is float type, and the values ​​input to a and b in the main function are 1.5 and 2.6. When calling Max(a,b), the values ​​1.5 and 2.6 of a and b are passed to the formal parameters x and y. Execute the if else statement in the function Max, and the variable z gets the value 2.6. There is a contradiction: the function definition is int type, z in the return statement is float type, and the value of z should be used as the return value of the function, the two are inconsistent. According to the assignment rules , first convert the value of z to int type to get 2, which is the return value obtained by the function. Finally, Max(x,y) returns an integer value 2 and returns to the calling function main.
4. For functions that do not return a value, the function should be defined as "void type" (or "empty type").
In this way, the system guarantees that the function will not return any value, that is, it is forbidden to use the return value of the called function in the calling function. At this time, the return statement cannot appear in the function body.

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/127862992#comments_25900592