Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have some confusion in fuctions. we have

C#
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

C#
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
Posted
Updated 20-Mar-11 4:37am
v3
Comments
Sergey Alexandrovich Kryukov 20-Mar-11 13:24pm    
Not true. First method has actually 3 parameters, not 2; second one - 2 parameters, not 1.
If you new about additional parameter "this", you would know the answer. Please read mine.
--SA

When you declare a function (in C# they are called "methods" by the way) you declare its return type:
C#
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:
SQL
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!
 
Share this answer
 
v2
Comments
codegeekalpha 20-Mar-11 12:23pm    
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
OriginalGriff 20-Mar-11 12:32pm    
Answer updated
Albin Abel 20-Mar-11 13:02pm    
Good effort
Sergey Alexandrovich Kryukov 20-Mar-11 13:26pm    
Griff, this is all good, my 5, but the key is: the number of input parameters is not counted correctly; there is one more, "this". Please see my answer.
--SA
Albin Abel 21-Mar-11 0:17am    
SAKryukov you have replied my comment. Is Original Griff will get notified?
To start with, the method first method will really have two parameters if you put static modifier:

C#
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":

C#
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
 
Share this answer
 
Comments
Albin Abel 21-Mar-11 0:18am    
Good Idea. my 5
Sergey Alexandrovich Kryukov 21-Mar-11 0:22am    
Thank you, Albin.
I think this is a right idea of explaining things to people with non-OOP background.
(It's apparent that OP did not get OOP yet.)
--SA
the first method takes the two parameter values, adds them, and returns the result. The second one doesn't need to return the value because it's setting a variable that's global to the class. You need to learn about "scope" regarding variables (google is your friend, and I've included a link at the end of this answer).

C#
class MyClass
{
    private int acctbal = 0;

    public void withdraw(int amount)
    {
        // this changes the variable acctbal 
        acctbal = acctbal - amount;
    }

    public int add(int a, int b)
    {
        // add the two values
        int c = a + b;
        // and return the result
        return c;
    }
}


http://www.blackwasp.co.uk/CSharpVariableScopes.aspx[^]
 
Share this answer
 
Comments
Albin Abel 20-Mar-11 13:03pm    
Good answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900