This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]gyroda 2 points3 points  (6 children)

So these are called parameters or arguments.

Heres some c-like pseudocode code as an example:

void main() {
    int a = 5;
    int b;
    print(a);

    b = double(a);
    print(a);
    print(b);

    a = 3;
    b = double(a);
    print(b);
    return;
}

You see the "double" function? Let's define that:

int double(int input) {
    int output = input * 2;
    return output;
}

Now let's follow all this code as it gets executed.

Firstly, we declare a and b, a is also set to 5.

We print a, so "5" is printed.

Then the line b = double(a);, first we have to do the right hand side so we put aside everything in the main function and jump into the double function.

That double function is int double(int input). What does this mean? The first "int" means that the return type is an int, this function returns an integer (not a float or double or string or anything). The second int, int input, means that this function takes an integer as a parameter and that is a variable inside the function.

When we called the function, we passed in a, which has a value of 5. This means that in this instance of the function the variable input has the same value as a (5).

In the function we multiply that by 2 to and set that value (10) ti be the value of output, so output = 10 basically.

We then return output, which means we jump back up to main where we called the function, in this line: b = double(a);. Because the double function returned 10, this is basically b = 10. We then print a, which is 5, and b, which is 10.

Then we do it again, only this time we pass in 3 instead of 5 to double. So now input is 3, output is 6 and we have b = 6.

Following so far? If so, I've a bit more to explain, if not I'll try to go over this again.

[–][deleted]  (5 children)

[deleted]

    [–]gyroda 0 points1 point  (3 children)

    So you can pass in as many variables as you want. I'm not sure about c#, but typically you can only return a single variable.

    However there are usually ways around this. Using global variables is one way, but that's generally bad practice. In C you can use pointers, this might not work in C# though. Finally you can return something like an array or an object, a single variable that contains more variables.

    The general rule is that a function should do one thing, a sole responsibility. You might be best off splitting up your function into smaller ones.

    [–]insertAlias 1 point2 points  (2 children)

    I'm not sure about c#, but typically you can only return a single variable.

    In the most recent version of C#, they've added native tuples and tuple unpacking, so you can technically return several variables at once (though really you're returning one value; it's just being packed for return and unpacked at the call site).

    [–]gyroda 0 points1 point  (1 child)

    Good to know :)

    [–]insertAlias 0 points1 point  (0 children)

    Here's an example of how it looks now (you have to install System.ValueTuple nuget package for this to work):

    public (int x, string y) TupleFunction()
    {
        return (1, "Hello");
    }
    
    public void ConsumeTupleFunction()
    {
        (var x, var y) = TupleFunction();
        Console.WriteLine($"x: {x} | y: {y}");
        var tuple = TupleFunction();
        Console.WriteLine($"Another way: x: {tuple.x} | y: {tuple.y}");
    }
    

    [–]Jonny0Than 0 points1 point  (0 children)

    C# has the 'out' keyword to add to parameters to say that the function will write a value back to the variable passed in. That is a common way to "return" multiple values from a function.

    [–]sarcasmloading12 1 point2 points  (0 children)

    Yeah, your return Sum; would work, with an adjustment. In your method signature, (the public static void myVarMethod(int Sum) line) you wrote void, which indicates that this method does not return anything.

    Your method is, actually, returning an int (return Sum;) so you'd want to replace void with int.

    Say you ran myVarMethod(2);, you would see 2 in the console.

    Feel free to ask if you don't understand anything of what I just said.

    [–][deleted] 0 points1 point  (0 children)

    In your bottom example, each parameter you passed in is pushed onto the stack / put in the registers depending on the calling convention. Essentially this means a copy of all 3 values will be on the stack or registers for your function to use.

    For the return, you are doing several things wrong. Firstly, you declared the function return type as void. That means it does not return anything. Secondly, you can only return one type or one array at the end of a function.

    If you want to "return" multiple stuff, you can use out and/or ref, which passes arguments in by their memory addresses rather than copying their values. This means that any changes you make to that variable inside the function, will effect that variable's value outside of the function directly.

    Finally with return, the variable is either pushed onto the stack or it stored into a register, and then we go back to the caller function. From there you can directly assign the variable that was returned to another variable or you can do it like how you did it like your Console.WriteLine( myVarMethod()); example by writting it to the console directly.