Skip to content
Snippets Groups Projects
Commit 048986d6 authored by Uwe Wienkop's avatar Uwe Wienkop
Browse files

Woche 05: Funktionen, byValue, byReference, ref/out

parent a15ba3ea
No related branches found
No related tags found
No related merge requests found
...@@ -6,19 +6,19 @@ namespace _04_Do_While_Schleife ...@@ -6,19 +6,19 @@ namespace _04_Do_While_Schleife
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
bool falscheEingabe; bool eingabeNochNichtRichtig;
int zahl; int zahl;
do do
{ {
Console.Write("Geben Sie eine Zahl zwischen 10..20 ein: "); Console.Write("Geben Sie eine Zahl zwischen 10..20 ein: ");
zahl = Convert.ToInt32(Console.ReadLine()); zahl = Convert.ToInt32(Console.ReadLine());
falscheEingabe = zahl < 10 || zahl > 20; eingabeNochNichtRichtig = zahl < 10 || zahl > 20;
if (falscheEingabe) if (eingabeNochNichtRichtig)
Console.WriteLine("Falsche Eingabe! Nur Zahlen von 10..20!"); Console.WriteLine("Falsche Eingabe! Nur Zahlen von 10..20!");
} while (falscheEingabe); } while (eingabeNochNichtRichtig);
////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////
......
...@@ -39,7 +39,7 @@ namespace _04_Funktionen_2 ...@@ -39,7 +39,7 @@ namespace _04_Funktionen_2
Incr_ByVal(20); // Incr(20) Incr_ByVal(20); // Incr(20)
erg=Incr_ByVal(zahl); // Incr(10) erg=Incr_ByVal(zahl); // Incr(10)
Incr_ByVal(zahl * zahl); // Incr(100) Incr_ByVal(zahl * zahl); // Incr(100)
//Incr(Convert.ToInt32(Console.ReadLine())); // Eingabe: "123" --> Incr(123) //Incr_ByVal(Convert.ToInt32(Console.ReadLine())); // Eingabe: "123" --> Incr(123)
Fakultaet(3); Fakultaet(3);
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_05_Datentyp_Char</RootNamespace>
</PropertyGroup>
</Project>
using System;
namespace _05_Datentyp_Char
{
class Program
{
static void Main(string[] args)
{
int x = 21;
double d = 3.14;
bool b = true;
string s = "Hallo"; // DOPPELTE Anführungszeichen
char c = 'A'; // EINFACHE Anführungszeichen
Console.WriteLine("Gib ein Zeichen ein: ");
char key=' ';
do
{
if (Console.KeyAvailable) // Ist (irgendeine) Taste gedrückt worden?
{
key = Console.ReadKey(true).KeyChar; // gedrücktes Zeichen
if (key== 'a')
Console.WriteLine("Auto nach links");
else if (key== 'd')
Console.WriteLine("Auto nach rechts");
}
} while (key != 'x');
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_05_Uebung_Mo</RootNamespace>
</PropertyGroup>
</Project>
using System;
namespace _05_Uebung_Mo
{
class Program
{
static void powers(double x, out double xx, ref double xxx)
// ref xx = 100.000
// ref xxx = 200.000
{
xx = x * x; // Speichere an der Stelle 100.000 den Wert von x*x
xxx = x * x * x; // Speichere an der Stelle 200.000 den Wert von x*x*x
}
static (double,double) powers_Tupel(double x)
{
return (x * x, x * x * x);
}
static void Main(string[] args)
{
double x2, x3=0; // Annahme: Speicheradr. von x2 = 100.000
// Annahme: Speicheradr. von x3 = 200.000
powers(2+1, out x2, ref x3); // powers(3, 100.000, 200.000)
Console.WriteLine($"x2 = {x2}, x3 = {x3}");
// Übergabe per ref
// Übergebene Variable muss vor Verwendung initialisiert sein
// Veränderung in der aufgerufenen Funktion optional
// Zusicherung an die aufgerufene Funktion, dass der Var.Wert
// sinnvoll ist
// Übergabe per out
// Übergebene Variable muss nicht vor der Verwendung initialisiert sein
// Veränderung in der aufgerufenen Funktion wird sichergestellt
// Keine Zusicherung an die aufgerufene Funktion, dass der Var.Wert
// sinnvoll ist
(x2, x3) = powers_Tupel(3);
}
}
}
...@@ -37,7 +37,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Dreiecke_Mo_Ubg", "04 Dr ...@@ -37,7 +37,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Dreiecke_Mo_Ubg", "04 Dr
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Uebung_Di", "04 Uebung_Di\04 Uebung_Di.csproj", "{F17A5B61-A944-4719-8CBA-BBD034A73B96}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Uebung_Di", "04 Uebung_Di\04 Uebung_Di.csproj", "{F17A5B61-A944-4719-8CBA-BBD034A73B96}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "04 Funktionen_2", "04 Funktionen_2\04 Funktionen_2.csproj", "{258D6D4B-BECB-44FA-A15A-2C310EBD9477}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Funktionen_2", "04 Funktionen_2\04 Funktionen_2.csproj", "{258D6D4B-BECB-44FA-A15A-2C310EBD9477}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "05 Uebung_Mo", "05 Uebung_Mo\05 Uebung_Mo.csproj", "{BCD60EC3-A134-48B8-8A40-093C3702D748}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "05 Datentyp_Char", "05 Datentyp_Char\05 Datentyp_Char.csproj", "{30CD32DF-7134-492E-8F48-BB5A8B473B51}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
...@@ -117,6 +121,14 @@ Global ...@@ -117,6 +121,14 @@ Global
{258D6D4B-BECB-44FA-A15A-2C310EBD9477}.Debug|Any CPU.Build.0 = Debug|Any CPU {258D6D4B-BECB-44FA-A15A-2C310EBD9477}.Debug|Any CPU.Build.0 = Debug|Any CPU
{258D6D4B-BECB-44FA-A15A-2C310EBD9477}.Release|Any CPU.ActiveCfg = Release|Any CPU {258D6D4B-BECB-44FA-A15A-2C310EBD9477}.Release|Any CPU.ActiveCfg = Release|Any CPU
{258D6D4B-BECB-44FA-A15A-2C310EBD9477}.Release|Any CPU.Build.0 = Release|Any CPU {258D6D4B-BECB-44FA-A15A-2C310EBD9477}.Release|Any CPU.Build.0 = Release|Any CPU
{BCD60EC3-A134-48B8-8A40-093C3702D748}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCD60EC3-A134-48B8-8A40-093C3702D748}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCD60EC3-A134-48B8-8A40-093C3702D748}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCD60EC3-A134-48B8-8A40-093C3702D748}.Release|Any CPU.Build.0 = Release|Any CPU
{30CD32DF-7134-492E-8F48-BB5A8B473B51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30CD32DF-7134-492E-8F48-BB5A8B473B51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30CD32DF-7134-492E-8F48-BB5A8B473B51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30CD32DF-7134-492E-8F48-BB5A8B473B51}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment