C编程语言中功能参数的范围

The list of the variables which are declared with function declaration/definition are known as function parameters.

函数声明/定义声明的变量列表称为函数参数

Note: There is no need to declare variables as function parameters while declaring a function, function declaration also works with the types of function parameters.

注意:在声明函数时,无需将变量声明为函数参数,函数声明也适用于函数参数的类型。

For example,

例如,

If we want to declare a function that will be used to find sum of two integer numbers. In the case, function will accept two integer parameters and return the sum as an integer.

如果我们要声明一个函数,该函数将用于查找两个整数的和 。 在这种情况下,函数将接受两个整数参数并将总和作为整数返回。

Declaration1:

声明1:

 int sum(int, int );

Declaration2:

声明2:

 int sum ( int a, int b );

Declaration1 has only types of the function parameters. While, declaration2 has its name also, in second case, while defining a function, variable names should be the same which we have declared during the declaring a function.

声明1仅具有函数参数的类型。 虽然,declaration2也有其名称,但在第二种情况下,在定义函数时,变量名称应与我们在声明函数时声明的名称相同。

功能参数范围 (Scope of function parameters)

Local variables have their scope to the function only in which they are declared, while global variables have scope to the program and they can be accessed by any function in the program.

局部变量仅在声明它们的函数中具有作用域,而全局变量具有程序的作用域,并且程序中的任何函数都可以访问它们。

Coming up to scope of the function parameters - “function parameters are local variables for that function only, we can say function parameter’s scopes are local to that function, in which they are declared.”

谈到函数参数的范围 -“函数参数仅是该函数的局部变量,我们可以说函数参数的范围对于该函数是局部的,在其中声明了它们。”

Look at following function:

看下面的函数:

    int findSum(int x, int y )
    {
	    int sum;
	    sum = x+y;
	    return sum;
    }

Here, x and y are the function parameters and sum is the local variable. All three variables have the same scope. But, if you will be thinking that x and y can also be declared inside the findSum() function – then, its totally wrong. Function parameters are used to make communication between calling and called function.

此处, x和y是函数参数, sum是局部变量。 所有这三个变量具有相同的范围。 但是,如果您认为x和y也可以在findSum()函数中声明–则完全错误。 函数参数用于在调用函数和被调用函数之间进行通信。

Functions are used to do some specific task and they need input. So, to provide input - we use function parameters. When we pass the values to the called function from calling function, the values will be copied to the function parameters and then function can operate on those values.

函数用于执行某些特定任务,并且需要输入。 因此,要提供输入-我们使用函数参数。 当我们将值从调用函数传递给被调用函数时,这些值将被复制到函数参数,然后函数可以对这些值进行操作。

Consider the following function declaration and calling statements

考虑以下函数声明和调用语句

#include <stdio.h>
int findSum(int x, int y)
{
	int sum;
	sum = x+y;
	return sum;
}

//main code
int main ()
{
	printf("SUM = %d\n", findSum(10,20));
	return 0;
}

Output

输出量

    SUM = 30

Here, int findSum(int x, int y) {...} is the function definition and findSum(10, 20) is the function calling, when function is called, actual values from main() function will be copied to x and y – which are the local variables for findSum() function.

在这里, int findSum(int x,int y){...}是函数定义,而findSum(10,20)是函数调用,当调用函数时, main()函数的实际值将被复制到x并且y –这是findSum()函数的局部变量。

翻译自: https://www.includehelp.com/c/scope-of-function-parameters.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值