功能及其含义..请帮助.. [英] fuctions and their implimentation.... pls help..

查看:78
本文介绍了功能及其含义..请帮助..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对功能有些困惑.我们有

  int   add ( int  a, int  b)
{
    c = a + b;
    返回 c;
} 



因为我们有两个输入参数a和b以及一个返回类型..就是c,但是在

 公共  void  withdraw( int 金额)
{
    acctbal = acctbal +金额;
} 



因为我们没有返回类型,我们只对输入参数进行评估.


y是它.它应该具有int acctbal的一种返回类型和两个输入参数..


请帮忙

解决方案

声明函数(在C#中它们被称为方法")时,您要声明其返回类型:

  int   add ( int  a, int  b)

此方法声明一个使用两个整数的方法,并返回一个整数.

public void withdraw( int amount)

此方法声明一个使用以下方法的方法:整数,但不返回任何内容.

如果某个方法不需要返回任何内容,或者没有任何有意义的方法可以返回,则将其声明为返回void-如果您不小心尝试了
,则编译器将进行投诉. 1)在方法中返回一个值,

2)尝试在计算中使用返回值.

对于withdraw方法,您不需要两个参数,因为该帐户只有一个总计-余额.假设您从帐户中提款时,余额将减少该金额.因此,无需指定从中提取现金的位置或将新值放回的位置.否则,您会说相当于从Johns的帐户中获取


500,然后将新的余额放入Mikes的帐户中"-这显然是错误的做法,可能会令Mike和/或John尽其所能...

OP回应:

  double  tax()
{
 double  tax = acctbal * 3
退货税;
} 



现在它正在退税..它将用作输入参数..输入参数中没有空白

否,因为在创建不带参数的方法时不需要将void指定为参数列表.您只需要在函数返回类型上指定void即可将它们与类构造函数区分开-不用担心它们,它们将在您的课程后面进行解释.

因此,如果您不需要为方法提供任何更多信息,则只需声明不带任何参数的方法即可.例如,如果您在车上,并且想启动发动机,则转动钥匙/按下启动按钮.您无需指定哪个键或哪个按钮,因为您要做的就是开始".额外的信息只会使这里变得混乱!


首先,如果您放置static修饰符,则方法第一个方法实际上将具有两个参数:

 静态  int   add ( int  a, int  b)
{
    c = a + b;
    返回 c;
} 



您应该使用此static,因为您没有尝试访问您的类或结构实例.在第二种方法中,不能使用static,但这不是具有一个输入参数的方法.还有另一个隐藏的输入参数,称为"this":

  void 提款( int 金额)
{
     .acctbal + = +金额;
} 



所有非静态方法(或实例方法)都隐含了不可见的"this"参数.它是一个实参数,传递方式与所有其他参数完全相同,可以像上面的示例中一样显式使用.此参数用于访问类声明方法的实例.

这是OOP的基础,甚至在进行后期绑定之前也是如此.

—SA


i have some confusion in fuctions. we have

int add(int a ,int b)
{
    c = a + b;
    return c;
}



in that we have two input parameters a and b and one return type.. which is c, but in

public void withdraw(int amount)
{ 
    acctbal = acctbal + amount;
}



in that we have no return type and we rate in input parameter of is only amount..


y is it.. it should have one return type of int acctbal and two input parameters..


pls help

解决方案

When you declare a function (in C# they are called "methods" by the way) you declare its return type:

int add(int a ,int b)

This declares a method that takes two integers, and returns an integer.

public void withdraw( int amount)

This declares a method that takes an integer, but returns nothing.

If a method does not need to return anything, or there is nothing sensible it can return, the you declare it as returning a void - the compiler will then complain if you accidentally try to either
1) return a value in the method,
or
2) try to use the return value in a calculation.

In the case of your withdraw method, you do not need two parameters, because there is only one total for the account - the balance. It assumes that when you withdraw from your account, the balance is reduced by that amount. Therefore, there is no need to specify the place to withdraw cash from, or the place to put the new value back into. Otherwise, you would say the equivalent of "Take


500 from Johns'' account, and put the new balance into Mikes'' account" - which would obviously be the wrong thing to do, much as it might please Mike and / or John...

The OP responded:

double tax()
{
double tax= acctbal*3
return tax;
}



now it is returning tax .. wot it is taking as a input paramenter.. there is no void in input paramenter

No, because you do not need to specify void as a parameter list when you are creating a method with no parameters. You only need to specify void on the function return type to differentiate them from class constructors - don''t worry about those, they will be explained later in your course.

So if you don''t need to give any more information to a method, you just declare it with no parameters. For example, if you are in you car, and want to start the engine, your turn the key / press the start button. You don''t need to specify which key, or which button, because all you have to do is go "start". Additional info would just confuse things here!


To start with, the method first method will really have two parameters if you put static modifier:

static int add(int a ,int b)
{
    c = a + b;
    return c;
}



You should use this static, because you''re not trying to access your class or structure instance. In second method, you cannot use static, but this is not a method with one input parameter. There is another hidden input parameter called "this":

void withdraw(int amount)
{
    this.acctbal += + amount;
}



All non-static methods (or instance methods) has implied invisible "this" parameter; it is a real parameter, passed exactly the same way as all other parameters and it can be uses explicitly as in the sample above. This parameter is used to access the instance of the class declaring method.

This is the very base of OOP, even before late binding, etc.

—SA


这篇关于功能及其含义..请帮助..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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