C# C# Basics Methods Method Parameters

Ksenija Klimova
Ksenija Klimova
6,163 Points

I need Help with the Challenge Return Values in C#.

I don't understand the problem of my code, because I get the desired answer in Terminal The result I received: Bummer: We made our own separate call to Multiply, but we didn't see the result we expected for the arguments we passed! Is your Multiply method multiplying its two parameters together and calling Console.WriteLine with the result?

Program.cs
using System;

class Program
{
    static double Multiply(double first, double second)
{
    return first * second;
}
    static void Main(string[] args)
    {
         Console. WriteLine(Multiply(2.5,2));
           Console. WriteLine(Multiply(6,7));

    }
}

2 Answers

Daniel Turato
PRO
Daniel Turato
Java Web Development Techdegree Graduate 30,112 Points

The challenge states that the result of the multiplication in the multiply method needs to be outputted from inside the method and not returned so there lies your problem. So all you have to do is this:

using System;

class Program
{
    static void Multiply(double first, double second)
    {
        Console.WriteLine(first * second);
    }
    static void Main(string[] args)
    {
         Multiply(2.5, 2);
         Multiply(6,7);

    }
}
Ksenija Klimova
Ksenija Klimova
6,163 Points

Thank you so much for the answer! Can you please explain me the difference between output and return value?

Daniel Turato
Daniel Turato
Java Web Development Techdegree Graduate 30,112 Points

Outputting a value is where you print a certain piece of data to the console, so the user/developer can see it. This is done via Console.WriteLine(). A return value is a value you specify on a method that needs to come out at the end of the process . So lets use the multiply example like above but instead with a return value:

static double Multiply() {}

static void Main(string[] args) {
 double var = Multiply();
}

So here in the above example, Multiply states in the method declaration that at some point in that method it needs to return a double data value. So once you call this method and it returns that value, you can then access the value returned by assigning it to another variable or passing it into another method ect. So let's say you don't want to return a value like in the challenge, in this situation you would use the void keyword.

jordansangalang
jordansangalang
5,042 Points

I have this exact code and it is still throwing an error. I copied and pasted your code here in case of any typos and I am still getting an error.

Daniel Turato
Daniel Turato
Java Web Development Techdegree Graduate 30,112 Points

Are you sure you copied the right piece of code? The correct code is the one marked as best answer , not the one in the answers comments

jordansangalang
jordansangalang
5,042 Points

Yes, I'm sure, because the code you pasted here was identical to my code. I restarted the quiz, pasted it again and passed on the first try. I think there was a bug of some sort. Not sure what series of events caused it though. For some reason the text editor was trying to multiply 5* 42

Ksenija Klimova
Ksenija Klimova
6,163 Points

Thank you for your answer, It was really helpful:)