When you call a method, each parameter receives a copy of the argument's value. The method can change its copy without touching the original. Most methods should work like this: values go in, a result comes out, and nothing else changes.
int minutes = 20;
int bigger = AddTen(minutes);
Console.WriteLine(minutes);
Console.WriteLine(bigger);
static int AddTen(int value)
{
value += 10;
return value;
}Check your prediction
20
30Scope: where a name exists
A variable exists from its declaration until the end of the block in which it was declared. Two methods can each have their own total with no connection between them. Names declared inside a method are invisible outside it, and that's what lets you read one method without having the whole program in your head.
int total = 100;
Console.WriteLine(CountSessions(3));
Console.WriteLine(total);
static int CountSessions(int days)
{
int total = 0;
for (int day = 1; day <= days; day++)
{
total += 2;
}
return total;
}Check your prediction
6
100Inside a static method, the outer total can't be seen at all. Delete the line int total = 0; and you get a compile error, rather than a method that silently changes the wrong variable. In a top-level program like these, keep your methods static. On Day 5, methods inside a class give static a second meaning, and most of those methods won't use it.
Defaults and named arguments
A parameter can have a default value, which makes the argument optional. A named argument, written name: value, says which parameter you mean. That makes a call easier to read when the numbers alone wouldn't tell you much, and lets you skip optional parameters.
Console.WriteLine(Plan(30));
Console.WriteLine(Plan(30, breakMinutes: 10));
Console.WriteLine(Plan(sessions: 3, minutes: 20));
static int Plan(int minutes, int sessions = 1, int breakMinutes = 0)
{
return (minutes + breakMinutes) * sessions;
}Check your prediction
30
40
60Sending back more than one value
A method returns one value. The out keyword gives a method a way to send back an extra one: the caller passes a variable, marked out at the call as well, and the method must assign it. You can declare that variable right inside the call.
You'll see out most often in a family of methods called TryParse. They answer the question "did that work?" with their bool return value and supply the converted result through out. You'll use them for real on Day 6.
if (TrySplit(95, out int hours))
{
Console.WriteLine(hours);
}
if (!TrySplit(-5, out int ignored))
{
Console.WriteLine($"Failed, so out is {ignored}");
}
static bool TrySplit(int minutes, out int wholeHours)
{
wholeHours = 0;
if (minutes < 0)
{
return false;
}
wholeHours = minutes / 60;
return true;
}Check your prediction
1
Failed, so out is 0C# also has ref, which lets a method change the caller's variable directly. It's rarely needed, and returning a new value is almost always clearer, so this course doesn't use it.
Your turn
A pace calculator
Write MinutesPerDay(int total, int days = 7), which returns total divided by days as a double. Print MinutesPerDay(350) and MinutesPerDay(350, 5), each with no digits after the decimal point using the format F0. Then, with days: 3 as a named argument, print MinutesPerDay(100, days: 3) with one digit after the decimal point.
Console.WriteLine(350 / 7);Hint 1: the idea
The default value goes in the declaration, not the call. The result must avoid whole-number division.
Hint 2: a first step
Write the method with two ordinary parameters and make one call work, then add = 7 to the declaration.
Show one solution
Console.WriteLine($"{MinutesPerDay(350):F0}");
Console.WriteLine($"{MinutesPerDay(350, 5):F0}");
Console.WriteLine($"{MinutesPerDay(100, days: 3):F1}");
static double MinutesPerDay(int total, int days = 7)
{
return (double)total / days;
}50
70
33.3days = 7 is a default, so the common case needs only one argument. The cast to double happens before the division, so 350 / 5 and 100 / 3 keep their fractions. Naming the argument, days: 3, makes the call readable without looking up the method.
What should MinutesPerDay(100, 0) do? Run it. Dividing a double by zero doesn't crash; it gives Infinity. Decide what your method should do instead, because it's your method that has to define that case.
Completion is saved in this browser only. It does not measure mastery.