# Improve Function

• ### Question

• Hi all,

I have been tasked to improve the below function - but i cannot find out what improvements can be made i.e. does anyone see any potential issues with the below:

public int test(int CharV)

int Numb1 = 0;  int Numb2 = 1;  int Numb3 = 2;
while (CharV > Numb1)
{
Numb2 = CharV * Numb2;
CharV -= 1;
}
return Numb2;
}

Monday, August 27, 2018 11:29 PM

### All replies

• Hi all,

I am new to C# and been given this task, could anyone help me:

What does this function do and what improvements would you make to it?

public int test(int CharV)

int Numb1 = 0;  int Numb2 = 1;  int Numb3 = 2;
while (CharV > Numb1)
{
Numb2 = CharV * Numb2;
CharV -= 1;
}
return Numb2;
}

Monday, August 27, 2018 12:13 AM
• Direct answer will not be given to homework questions.

Add a Console.WriteLine() to dump the value of Numb2 and CharV before the "CharV -= 1; " line and you should be able to realize its purpose. (Hint: This roughly performs the same task as a button on scientific calculator)

As for what kind of improvement can do, I'll advise you to revisit what kind of iteration statements does C# support.

Also, beware negative CharV value can create infinate loop here. So you'd probably add some code to guard against this condition.
Monday, August 27, 2018 1:41 AM
• Hi,

It is a method used to calculate Num2 and return it.

Could you provide more details about what improvements you want to doing for?

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Monday, August 27, 2018 5:11 AM
• Ok, if I set CharV to 4 what would be the return value be of my function?

Also, what is purpose of "-=" ? i dont understand this line

Monday, August 27, 2018 8:53 AM
• This is logic for calculating factorial of CharV value.

Prabhakar

Monday, August 27, 2018 9:26 AM
• 4 * 3 * 2 * 1 = 24 answer

in each loop CharV  value is reduced by 1,

CharV  -= 1;  equivalent to CharV = CharV - 1;

Prabhakar

Monday, August 27, 2018 9:28 AM
• I don't see potential issues and the only improvement I see is in the way you've defined your Numb* variables. Doesn't matter really, but perhaps this is cleaner and maybe what your task is supposed to be (hope I'm not doing homework for you!):

```public int test(int CharV)
{
int Numb1 = 0, Numb2 = 1, Numb3 = 2;
while (CharV > Numb1)
{
Numb2 = CharV * Numb2;
CharV -= 1;
}
return Numb2;
}```

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com

Tuesday, August 28, 2018 1:09 AM
• Humm... Hope you understand that there is unspoken or explicitly stated rule in nearly all programming related forums that, when homework question is asked, while you may help them work around the corners, answers will not be directly given. If the asker have problem with their homework and unable to finish it by their own effort, they should let their teacher know.

Directly telling them answers will harm the community and the society in the long run by allowing incompetent student to graduate, and the karma will go back to ourselves. So the next time your colleague do some WTF-ty thing to your code, or your upper levels sending non-sense command to you, or anything that will cause you to faceplam or head-desk, don't complain. As you're the part of the world allowing to graduate with their incompetence undetected by their teacher. (Afterall, homework is a way to let teachers verify that their students have no problem understanding what have been taught in the lessons)

Tuesday, August 28, 2018 1:21 AM
• Of course I know not to provide answers to homework! I'm pretty sure that I've been answering questions on this Forum longer than you have.

I guess it should have been obvious to me that this was a homework "task" (hindsight) ... but at the time I thought that possibly the OP was just a beginner, learning on his/her own from a book or website. People *do* do that, you know.

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com

Tuesday, August 28, 2018 4:42 AM
• Humm... I'm replying to Prabhakar26 's single line direct answer and not you.

If you're using "Flat list view", try switch to "Threaded list view" on "Quick Access"->"My Settings" to see.

Wednesday, August 29, 2018 1:16 AM
• This will probably be my last reply for you.

Since you've exhibited behaviour of just ignoring any evidence (including a dozen webpage including Wikipedia and Stackflow that state what you think is wrong) that is opposing your argument, I decided it's not possible to have any constructive discussion with you. Therefore from now onward I'll not reply anything you write, and simply wait your posts got deleted when your account is banned.

Wednesday, August 29, 2018 1:35 AM
• Yes, cheong, I was using Flat list view ... I generally prefer that to Threaded.

I'm sorry ... since your reply came right after mine, I assumed it was directed at me. I see now that it wasn't. Because of the fact that users *can* use either view, it's sometimes a good idea to indicate who you're replying to (depending on the context of your reply ... it's not always needed).

Sorry for being a bit "snippy" ...

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com

Wednesday, August 29, 2018 5:04 AM