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!