Skip to content

Commit d51a4e4

Browse files
authored
Update 3-exercise-create-methods-return-numbers.md
1 parent 1cc3f49 commit d51a4e4

File tree

1 file changed

+90
-24
lines changed

1 file changed

+90
-24
lines changed

learn-pr/wwl-language/create-c-sharp-methods-return-values/includes/3-exercise-create-methods-return-numbers.md

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,55 @@
33

44
You may often need to return numbers from methods and use the results for other tasks. In this brief exercise, you'll practice returning `int` and `double` data types, and capturing the return values.
55

6-
## Create a method that returns an integer
6+
## Create a method that returns an integer
7+
78
Suppose you're visiting Vietnam and want to create a brief program that converts currency. You can assume the current exchange rate is `1 USD = 23500 VND`. In this task, you'll write a method that converts USD to VND.
89

910
1. In the Visual Studio Code Editor, delete any existing code from the previous exercises.
1011

1112
1. Enter the following code into the Visual Studio Code Editor:
12-
```c# double usd = 23.73; int vnd = UsdToVnd(usd);
13-
Console.WriteLine($"${usd} USD = ${vnd} VND");
14-
int UsdToVnd(double usd) {
15-
} ```
16-
In this step, you initialize two variables to store the USD and VND values. Notice that `vnd` is initialized to the result of the method `UsdToVnd`. The method returns an integer value since VND is typically represented in whole numbers. To display the results of the currency conversion, `Console.WriteLine` is used.
13+
14+
```c#
15+
double usd = 23.73;
16+
int vnd = UsdToVnd(usd);
17+
18+
Console.WriteLine($"${usd} USD = ${vnd} VND");
19+
20+
int UsdToVnd(double usd)
21+
{
22+
23+
}
24+
```
25+
26+
In this step, you initialize two variables to store the USD and VND values. Notice that `vnd` is initialized to the result of the method `UsdToVnd`. The method returns an integer value since VND is typically represented in whole numbers. To display the results of the currency conversion, `Console.WriteLine` is used.
1727

1828
1. Next, you'll add code to perform the conversion. Update the `UsdToVnd` method with the following code:
19-
```c# int UsdToVnd(double usd) { int rate = 23500; return (int) (rate * usd); } ```
20-
If you omit the cast from the return result, you'll see the following error:
21-
```output Cannot implicitly convert type 'double' to 'int'. ```
22-
This happens because the compiler attempts to cast the value returned to match the data type specified in the method signature. However, implicit casting is only available when there's no data loss occurring as a result of the conversion. The return value must always match the data type specified in the method signature, so in this case, you must cast the result.
29+
30+
```c#
31+
int UsdToVnd(double usd)
32+
{
33+
int rate = 23500;
34+
return (int) (rate * usd);
35+
}
36+
```
37+
38+
If you omit the cast from the return result, you'll see the following error:
39+
40+
```output
41+
Cannot implicitly convert type 'double' to 'int'.
42+
```
43+
44+
This happens because the compiler attempts to cast the value returned to match the data type specified in the method signature. However, implicit casting is only available when there's no data loss occurring as a result of the conversion. The return value must always match the data type specified in the method signature, so in this case, you must cast the result.
2345

2446
1. If necessary, open Visual Studio Code's Integrated Terminal panel.
2547

2648
1. At the Terminal command prompt, enter **dotnet run** and compare your output with the following:
27-
``` $23.73 USD = $557655 VND ```
28-
If your code displays unexpected results, you'll need to review your code to find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results.
49+
50+
```
51+
$23.73 USD = $557655 VND
52+
```
53+
54+
If your code displays unexpected results, you'll need to review your code to find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results.
2955

3056
### Create a method that returns a double
3157

@@ -34,33 +60,73 @@ Next, you'll create a method to convert VND back to USD.
3460
1. Create a new blank code line at the end of the `UsdToVnd` method.
3561

3662
1. Enter the following code:
37-
```c# double VndToUsd(int vnd) {
38-
} ```
63+
64+
```c#
65+
double VndToUsd(int vnd)
66+
{
67+
68+
}
69+
```
3970

4071
1. Update the `VndToUsd` method with the following code:
41-
```c# double VndToUsd(int vnd) { double rate = 23500; return vnd / rate; } ```
42-
In this case, you need `rate` to be a `double` or else the compiler uses integer division and return a truncated `int` value. USD needs to be represented by a decimal number. If you set `rate` to an `int` instead of `double`, you'll notice that the compiler doesn't present you with any errors. This happens because the value of `vnd / rate` is implicitly casted to the `double` data type specified in the method signature. When creating methods that return numeric values, it's important to consider the data types in the operations your method performs.
72+
73+
```c#
74+
double VndToUsd(int vnd)
75+
{
76+
double rate = 23500;
77+
return vnd / rate;
78+
}
79+
```
80+
81+
In this case, you need `rate` to be a `double` or else the compiler uses integer division and return a truncated `int` value. USD needs to be represented by a decimal number.
82+
83+
If you set `rate` to an `int` instead of `double`, you'll notice that the compiler doesn't present you with any errors. This happens because the value of `vnd / rate` is implicitly casted to the `double` data type specified in the method signature. When creating methods that return numeric values, it's important to consider the data types in the operations your method performs.
4384

4485
1. Locate the call to `Console.WriteLine` and append a new blank code line. Then enter the following code to call our new method and print the output:
45-
```c# Console.WriteLine($"${vnd} VND = ${VndToUsd(vnd)} USD"); ```
86+
87+
```c#
88+
Console.WriteLine($"${vnd} VND = ${VndToUsd(vnd)} USD");
89+
```
4690

4791
## Check your work
4892

4993
In this task, you'll run our application from the Integrated Terminal and verify your code is working correctly. Let's get started.
5094

5195
1. Compare your code to the following to ensure it's correct:
52-
```c# double usd = 23.73; int vnd = UsdToVnd(usd);
53-
Console.WriteLine($"${usd} USD = ${vnd} VND"); Console.WriteLine($"${vnd} VND = ${VndToUsd(vnd)} USD");
54-
int UsdToVnd(double usd) { int rate = 23500; return (int) (rate * usd); }
55-
double VndToUsd(int vnd) { int rate = 23500; return vnd / rate; } ```
96+
97+
```c#
98+
double usd = 23.73;
99+
int vnd = UsdToVnd(usd);
100+
101+
Console.WriteLine($"${usd} USD = ${vnd} VND");
102+
Console.WriteLine($"${vnd} VND = ${VndToUsd(vnd)} USD");
103+
104+
int UsdToVnd(double usd)
105+
{
106+
int rate = 23500;
107+
return (int) (rate * usd);
108+
}
109+
110+
double VndToUsd(int vnd)
111+
{
112+
int rate = 23500;
113+
return vnd / rate;
114+
}
115+
```
56116

57117
1. Save your work using <kbd>Ctrl</kbd> + <kbd>S</kbd> or using the Visual Studio Code File menu.
58118

59119
1. If necessary, open Visual Studio Code's Integrated Terminal panel.
60-
In the EXPLORER panel, to open a Terminal at your TestProject folder location, right-click **TestProject**, and then select **Open in Integrated Terminal**.
120+
121+
In the EXPLORER panel, to open a Terminal at your TestProject folder location, right-click **TestProject**, and then select **Open in Integrated Terminal**.
61122

62123
1. At the Terminal command prompt, enter **dotnet run**
63124

64125
1. Verify that your code produces the following output:
65-
```Output $23.73 USD = $557655 VND $557655 VND = $23.73 USD ```
66-
If your code displays different results, you'll need to review your code to find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results.
126+
127+
```Output
128+
$23.73 USD = $557655 VND
129+
$557655 VND = $23.73 USD
130+
```
131+
132+
If your code displays different results, you'll need to review your code to find your error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results.

0 commit comments

Comments
 (0)