如何声明参数? [英] How are parameters declared?

查看:150
本文介绍了如何声明参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我是c ++编程的初学者。我遇到了一个我无法理解的功能问题。我给出的代码如下....

Basically i am a beginner on c++ programming. I have encountered a problem on function which i cannot understand.I am giving the code below....

#include <stdio.h>
int test_function(int x)
{
int y = x;
x = 2 * y;
return (x * y);
}
int main()
{
int x = 10, y = 20, z = 30;
z = test_function(x);
printf("%d %d %d\n", x, y, z);
return 0;
}





我真正的问题是为什么参数只保存(int x)?当我把它放进去时它表示你重新声明为不同类型的符号

当我把int z放在参数中时它说参数太少了功能'test_function'



我尝试过:





my real question is why the parameter only holds (int x)? and when I am putting int y
it says " y redeclared as different kind of symbol"
and when I put int z in the parameter it says "too few arguments to function 'test_function'"

What I have tried:

#include <stdio.h>
int test_function(int x,int y)
{
int y = x;
x = 2 * y;
return (x * y);
}
int main()
{
int x = 10, y = 20, z = 30;
z = test_function(x);
printf("%d %d %d\n", x, y, z);
return 0;
}

推荐答案

定义函数时,为它创建签名:为了工作需要给出的参数,以及它返回给你的代码的值的类型。

在你的第一个代码片段中, test_function 被声明为具有接受单个整数参数的签名,并返回一个整数值。

所以当你调用它时,你正确地提供了一个参数,并使用结果:

When you define a function, you create a signature for it: the parameters it needs to be given in order to work, and the type of the value it returns to your code.
In your first code snippet, test_function is declared as have a signature of accepting a single integer parameter, and returning an integer value.
So when you call it, you correctly provide one parameter, and use the result:
z = test_function(x);

你传递x的值(不是变量本身 - C总是传递值而不是引用,但不要担心一会儿)然后将它自己和两个相乘。

所以你可以提供价值十,它将返回200.



您的第二个版本使用不同的签名重新定义 test_function :它现在需要将两个整数传递给它,并且仍然返回一个整数值。

但是当你试图调用函数时:

You pass the value of x (not the variable itself - C always passes values rather than references, but don't worry about that for a moment) and return it multiplied by itself and two.
So you can supply the value ten, and it will return 200.

Your second version redefines test_function with a different signature: it now expects two integers to be passed to it, and still returns a single integer value.
But when you try to call the function:

z = test_function(x);

你只提供一个参数,所以系统没有知道该做什么,并产生错误。

如果你想使用那个签名,你需要传递两个参数:

You only supply one parameter, so the system does not know what to do, and generates an error.
If you want to use that signature, you need to pass two parameters:

z = test_function(x, y);



但即使这样,你所看到的代码也行不通:


But even then, your code as shown won't work:

int test_function(int x, int y)
    {
    int y = x;
    x = 2 * y;
    return (x * y);
    }

因为函数体内的新 y 的定义隐藏你告诉它期望的版本参数,并不确定你打算使用哪一个。



试试这个:

Becuase the definition of a new y inside the body of the function "hides" the version you told it to expect as a parameter, and it isn't really sure which one you meant to use.

Try this:

int test_function(int x, int y)
    {
    int y2 = x;
    x = 2 * y;
    return (x * y2);
    }



And

z = test_function(x, y);

它应该正确编译。



请注意,一个函数中的变量名称与另一个函数中的变量名称无关:您可以将此函数编写为:

and it should compile correctly.

Do note that the name of variables in one function have no bearing on those in another: you could write your function as this:

int test_function(int a, int b)
    {
    int c = a;
    a = 2 * b;
    return (a * c);
    }

它的工作方式完全相同,因为调用函数时不会传输名称x和y,只是它们包含的值。变量不能直接在声明它们的函数之外访问,除非它们用于将值传递给另一个函数,或者返回给调用者。

And it work exactly the same, because the names "x" and "y" are not transferred when you call the function, just the values they contained. Variables cannot be accessed directly outside the function in which they are declared unless they are used to pass a value to another function, or returned to the caller.


这篇关于如何声明参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆