C language basics: function parameters and return values

        In this section, we will describe in detail the parameter list and return value of the function. When defining a function, we need to determine the parameter list and return value of the function.

        1. Parameter table

        The parameter table is what we input to the function when we call it. Through the input to the function, the function has different outputs under different input conditions, that is, the return value. For example we define a function to calculate the sum of two variables:

int plus(int a, int b)
{
	return a + b;
}

int main(int argc, char *argv[])
{
	int c = 3, d = 5;
	int e = add(c, d);
	printf("%d\n", c, e);
	return 0;
}

        When we define the function, we define two parameters for the function, a and b, and when we call it, we pass two variables c and d to the function. where a and b are the formal parameters of the function, and c and d are the actual parameters of the function call. That is, when the function is called, the value of c is passed to a, and the value of d is passed to b, so the value of a is 3 and the value of b is 5. And in the function body, the function will calculate the sum of 3 and 5, and return the result through the return statement return.

        The actual parameters of the function just pass the value of the variable into the function, the function saves the value into the formal parameters, and uses those variables. Modifying the value of the formal parameter inside the function will not affect the actual parameter outside the function, for example:

int wrongplus(int a, int b)
{
	a++;
	b++;
	return a + b;
}

int main(int argc, char *argv[])
{
	int c = 3, d = 4;
	int e = wrongplus(c, d);
	printf("%d\n", c, e);
	return 0;
}

        In the above example, the two variables a and b defined in the main function main are actual parameters, while a and b in the parameter table in wrongplus are formal parameters. Modifying the formal parameters inside the function will not affect the actual external parameters. parameters, so the values ​​of a and b inside the function are 4 and 6 respectively, while the values ​​of a and b outside the function are still 3 and 5.

    

        The return value of the function

        The return value of a function is a result that is returned to the caller of the function when the function is called and the execution ends. Note that, unlike parameter lists, a function can have multiple input parameters, but only one return result.

        The function needs to use the return statement to define its return value. The expression after the return statement will be used as the return value of the function, and this value must be the same as the return value type when the function is defined. For example, let's define a function that computes and returns the larger of three variables:

int max(int a, int b, int c)
{
	int m;
	if (a > b && a > c)
	{
		m = a;
	}
	else if (b > a && b > c)
	{
		m = b;
	}
	else
	{
		m = c;
	}
	return m;
}

int main(int argc, char *argv[])
{
	printf("%d\n", max(1, 5, 6));
	return 0;
}

        Note that when the return statement is executed, the execution of the current function ends. The program following the return statement in the current function body will not be executed.

        In addition, the variable max we define inside the function body is only valid inside the function, and the variables defined inside the function cannot be used outside the function. We will learn about the lifetime of variables in subsequent chapters.

        Let's make some modifications to the above program:

int max(int a, int b, int c)
{
	if (a > b && a > c)
	{
		return a;
	}

	if (b > a && b > c)
	{
		return b;
	}
	return c;
}

    

        Three, no parameter function and no return value function

        Functions are horribly defined as having no parameter type or no return type. When the function does not have a parameter list, you can use the void keyword to define the function's parameter list. When the function has no return value, the return value type of the function can also be defined by void. E.g:

float pi_value(void)
{
	return 3.141562654;
}

void display_value(int v)
{
	printf("%d\n", v);
}

        同样我们还可以定义一个即无参数表也无返回值的函数:

void hello(void)
{
	printf("Hello World!\n");
}

        另外,当没有参数表时,小括号中的void可以省略,例如:

void hello()
{
	printf("Hello World!\n");
}


欢迎关注公众号:编程外星人

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691247&siteId=291194637