Zie Scherp Scherper · Methoden
Labo methoden, week 1
Eerst even herhalen, dan samen een opwarmer
Program.cs
▶
1
class
Program
2
{
3
static
void
ToonBegroeting
()
4
{
5
Console.
WriteLine
(
"Dag!"
);
6
}
7
8
static
void
Main
()
9
{
10
ToonBegroeting
();
11
}
12
}
Een methode staat in class Program, naast Main, en is static
Main roept ze op: haar naam en haakjes
>
UITVOER
Dag!
Program.cs
1
class
Program
2
{
3
static
void
ToonBegroeting
()
4
{
5
Console.
WriteLine
(
"Dag!"
);
6
}
7
8
static
int
Som
(
int
a,
int
b)
9
{
10
return
a + b;
11
}
12
13
static
void
Main
()
14
{
15
ToonBegroeting
();
16
}
17
}
void: ze geeft niets terug
int: ze geeft een int terug
return geeft het antwoord terug en stopt de methode
Program.cs
1
class
Program
2
{
3
static
void
ToonBegroeting
()
4
{
5
Console.
WriteLine
(
"Dag!"
);
6
}
7
8
static
int
Som
(
int
a,
int
b)
9
{
10
return
a + b;
11
}
12
13
static
void
Main
()
14
{
15
ToonBegroeting
();
16
int
totaal =
Som
(
3
,
4
);
17
}
18
}
Vang het antwoord op in een variabele van hetzelfde type
By value: een kopie
✱
✱
01
Een methode krijgt een
kopie
van wat je meegeeft
✱
✱
02
Past ze die kopie aan, dan blijft je
eigen variabele
zoals ze was
✱
✱
03
Het nieuwe resultaat nodig? Geef het terug met
return
en vang het op
Opwarmer: Temperatuur
✱
✱
01
Zet graden
Celsius
om naar
Fahrenheit
✱
✱
02
Toon een temperatuur met een
woord
erbij
✱
✱
03
Koud onder 10, fris onder 20, anders warm
Program.cs
1
class
Program
2
{
3
4
static
void
Main
()
5
{
6
}
7
}
Methoden komen hier, naast Main
Program.cs
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
void
Main
()
9
{
10
}
11
}
Dit geeft ze terug
Dit heeft ze nodig
Hier stopt ze en geeft ze het antwoord
Program.cs
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
void
Main
()
9
{
10
NaarFahrenheit
(
20
);
11
}
12
}
Main roept
NaarFahrenheit(20)
op. Wat verschijnt er op het scherm?
A
68
B
20
C
Niets
✓
5
4
3
2
1
Denk even na
Antwoord
Niets: het antwoord wordt nergens opgevangen
Program.cs
▶
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
void
Main
()
9
{
10
int
f =
NaarFahrenheit
(
20
);
11
}
12
}
Ze geeft een double, geen int
FOUT
error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
Program.cs
▶
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
void
Main
()
9
{
10
double
f =
NaarFahrenheit
(
20
);
11
Console.
WriteLine
(f);
12
}
13
}
Een double vangt het antwoord op
>
UITVOER
68
Program.cs
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
{ … }
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
string
BeschrijfWeer
(
double
celsius)
9
{
10
if
(celsius <
10
)
11
{
12
return
"koud"
;
13
}
14
else
if
(celsius <
20
)
15
{
16
return
"fris"
;
17
}
18
else
19
{
20
return
"warm"
;
21
}
22
}
23
24
static
void
Main
()
{ … }
25
{
26
double
f =
NaarFahrenheit
(
20
);
27
Console.
WriteLine
(f);
28
}
29
}
Ze geeft een woord terug: een string
Elk pad eindigt met een return
Program.cs
▶
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
{ … }
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
string
BeschrijfWeer
(
double
celsius)
{ … }
9
{
10
if
(celsius <
10
)
11
{
12
return
"koud"
;
13
}
14
else
if
(celsius <
20
)
15
{
16
return
"fris"
;
17
}
18
else
19
{
20
return
"warm"
;
21
}
22
}
23
24
static
void
ToonTemperatuur
(
double
celsius)
25
{
26
double
fahrenheit =
NaarFahrenheit
(celsius);
27
string
weer =
BeschrijfWeer
(celsius);
28
Console.
WriteLine
(
$"
{celsius}
°C is
{fahrenheit}
°F, dat is
{weer}
"
);
29
}
30
31
static
void
Main
()
32
{
33
ToonTemperatuur
(
20
);
34
}
35
}
Methoden roepen elkaar op
Ook BeschrijfWeer doet haar deel
Enkel deze heet Toon..., dus enkel hier WriteLine
>
UITVOER
20 °C is 68 °F, dat is warm
Program.cs
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
{ … }
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
string
BeschrijfWeer
(
double
celsius)
{ … }
9
{
10
if
(celsius <
10
)
11
{
12
return
"koud"
;
13
}
14
else
if
(celsius <
20
)
15
{
16
return
"fris"
;
17
}
18
else
19
{
20
return
"warm"
;
21
}
22
}
23
24
static
double
Verwarm
(
double
celsius)
25
{
26
celsius = celsius +
5
;
27
return
celsius;
28
}
29
30
static
void
ToonTemperatuur
(
double
celsius)
{ … }
31
{
32
double
fahrenheit =
NaarFahrenheit
(celsius);
33
string
weer =
BeschrijfWeer
(celsius);
34
Console.
WriteLine
(
$"
{celsius}
°C is
{fahrenheit}
°F, dat is
{weer}
"
);
35
}
36
37
static
void
Main
()
38
{
39
double
temp =
12
;
40
Verwarm
(temp);
41
ToonTemperatuur
(temp);
42
}
43
}
Wat toont dit programma?
A
12 °C is 53,6 °F, dat is fris
✓
B
17 °C is 62,6 °F, dat is fris
5
4
3
2
1
Denk even na
Antwoord
12 °C: Verwarm kreeg een kopie van temp
Program.cs
▶
1
class
Program
2
{
3
static
double
NaarFahrenheit
(
double
celsius)
{ … }
4
{
5
return
celsius *
1.8
+
32
;
6
}
7
8
static
string
BeschrijfWeer
(
double
celsius)
{ … }
9
{
10
if
(celsius <
10
)
11
{
12
return
"koud"
;
13
}
14
else
if
(celsius <
20
)
15
{
16
return
"fris"
;
17
}
18
else
19
{
20
return
"warm"
;
21
}
22
}
23
24
static
double
Verwarm
(
double
celsius)
{ … }
25
{
26
celsius = celsius +
5
;
27
return
celsius;
28
}
29
30
static
void
ToonTemperatuur
(
double
celsius)
{ … }
31
{
32
double
fahrenheit =
NaarFahrenheit
(celsius);
33
string
weer =
BeschrijfWeer
(celsius);
34
Console.
WriteLine
(
$"
{celsius}
°C is
{fahrenheit}
°F, dat is
{weer}
"
);
35
}
36
37
static
void
Main
()
38
{
39
double
temp =
12
;
40
temp =
Verwarm
(temp);
41
ToonTemperatuur
(temp);
42
}
43
}
Nu vang je het antwoord op
>
UITVOER
17 °C is 62,6 °F, dat is fris