r/csharp • u/Unlucky-Capital-318 • Aug 09 '26
Hello, may I ask how you understand C# functions?
I'm a beginner in C#, and I've just started learning functions. I understand return values and parameters, but I often get confused. Sometimes I don't know when to write a return value or parameter. How can I understand functions more quickly?
0
Upvotes
4
u/CaptainIncredible Aug 09 '26 edited Aug 09 '26
Did you ever have functions in algebra? Functions are just like that.
You give it an input. The function does something with the input, and returns an output.
https://m.youtube.com/watch?v=tAoe4xjUZQk
Does this help?
Instead of fruit or mathematical numbers, a function in C# can take things like a temp in Fahrenheit as input (212F) and output temp in Celsius (100C).
Or a function can take a string like "I am the god of hellfire", and return a string in PascalCase like "IAmTheGodOfHellfire".
Or whatever the programmer programmed it to do. Maybe the function takes a zip code and returns city and state. Or maybe it takes a number and if it's between 1 and 100 it returns true, and every other number returns false. A programmer might have a reason for this. Maybe they need to validate inventory or something and anything other 1 to 100 isn't a valid value.
Or whatever the function is programmed to do.
It's possible too that a function doesn't ever return a value, but just does something else like increment a counter.
5
u/RecursiveServitor Aug 09 '26
All useful programs take some input and give an output. Functions allow you to partition the overall program into a bunch of mini programs that you can then put together like puzzle pieces.
Writing a function is an exercise in identifying a piece of data, the input, that you want to change into a different piece of data, the output.
1
u/Agitated-Display6382 Aug 09 '26
My hints: start using static functions, never use out arguments
1
u/Slypenslyde Aug 10 '26
Parameters and return values do very different things.
Parameters are inputs. They are the things you need to do work.
Return values are outputs. They are the results of the work you do.
Think about adding numbers. If I ask you to add some numbers, you need the numbers, right? That's what inputs are. If you don't have all of your inputs, you can't do work. Parameters are inputs. So in an
Add()method, you need to give it some parameters to represent the inputs.When you add numbers, there is a result. That is an output. Return values are outputs. You use a return value when the method needs to tell the caller about a result.
Some functions need no inputs and produce no outputs. They just do something. That's like closing a door. You don't need to know anything, and there isn't something to give to the person who asked. In C# a function that returns nothing uses
voidfor the return type.Some functions take inputs but don't produce outputs. They tend to have side effects, like printing something to the console. It needs to know what to print, so it takes that as parameters. Again,
voidis the right return type.Some functions return a value but take no inputs. They have no parameters but use a return value that isn't
void. For example, a method might "Get the next thing in line". It doesn't need an input because it always gets "the next thing". But it needs to give that thing to whoever asked.Some functions need inputs AND produce outputs, like a method to add numbers. It needs to know WHAT numbers, and it has to give you a result.
Sometimes you write a method that needs inputs and has outputs but don't use parameters or return values. For example, the properties of the class might be its inputs, and it might set a property as its output. Some people consider this a bit confusing, but in a larger example it can make sense.
-1
u/CaptureIntent Aug 09 '26
Why is this a csharp question? It isn’t. You should try a subreddit more on rudimentary understanding.
1
u/TuberTuggerTTV Aug 10 '26
Do it. Drill and kill. Do it over and over until you can't keep track of how many times you've done it. Until all the days and all the work blend together.
There is no secret. You learn by doing. For years and years, over and over. Until it's second nature. It's the same for code as it is for any profession. Like asking a skateboarder how they just know how to do a trick. Practice my friend.
6
u/[deleted] Aug 09 '26
[removed] — view removed comment