Functions and scope
Give a calculation a name. Pass in a number, return the result, and use the same function again with a different input.
Simple example
Double three.
Start3 × 2
Result6
using System;
Console.WriteLine(DoubleValue(3));
static int DoubleValue(int value) => value * 2;
6
Advertisements
Everyday examples
Each bicycle has two wheels. A rack holds seven bicycles. How many wheels are there?
Start7 bikes × 2
Result14
using System;
Console.WriteLine(DoubleValue(7));
static int DoubleValue(int value) => value * 2;
14
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson