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

2021-10-26 Funktionen (By-val, By-ref, Rekursion)

parent ee41401e
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ namespace _04_Dreiecke_Mo_Ubg
{
static void Quadrat(int n)
{
for (int zeile = 1; zeile <= n; zeile++)
for (int zeile = 0; zeile < n; zeile++)
{
for (int spalte = 0; spalte < n; spalte++)
{
......@@ -28,7 +28,7 @@ namespace _04_Dreiecke_Mo_Ubg
}
static void SinPlot()
{
for (double x = 0.0; x < 2*Math.PI; x+=0.1)
for (double x = 0.0; x < 2*Math.PI; x+=0.15)
{
int anzLeerzeichen = (int)((Math.Sin(x)+1.0)*30.0);
for (int spalte = 0; spalte < anzLeerzeichen; spalte++)
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_04_Funktionen_2</RootNamespace>
</PropertyGroup>
</Project>
using System;
namespace _04_Funktionen_2
{
class Program
{
// Übergabemechanismus: CALL-BY-VALUE, Wertübergabe
static int Incr_ByVal(int meineZahl)
{
meineZahl++;
return meineZahl;
// RETURN-BY-VALUE
}
// Übergabemechanismus: CALL-BY-REFERENCE, Referenzübergabe
// Bei ref-Parametern wird kein Wert, sondern eine Referenz auf
// eine andere Variable (z.B. aus Main) übergeben!
static void Incr_ByRef(ref int meineZahl)
{
meineZahl++;
}
// Fakultät(1) --> 1
// Fakultät(n>1) --> n * Fakultät(n-1)
static int Fakultaet(int n)
{
if (n == 1)
return 1;
else
return n * Fakultaet(n - 1);
}
static void Main(string[] args)
{
int zahl = 10;
int erg;
Incr_ByVal(20); // Incr(20)
erg=Incr_ByVal(zahl); // Incr(10)
Incr_ByVal(zahl * zahl); // Incr(100)
//Incr(Convert.ToInt32(Console.ReadLine())); // Eingabe: "123" --> Incr(123)
Fakultaet(3);
// Incr_ByRef(ref 10); -- FEHLER: Muss Variable sein
Incr_ByRef(ref zahl); // ACHTUNG! Hier wird nicht der Wert von Zahl, sondern
// eine Referenz auf die Variable (der Speicherplatz) übergeben!!!
Console.WriteLine(zahl);
}
}
}
......@@ -12,15 +12,19 @@ namespace _04_IntroFunktionen
// ... liefern einen Return-Wert (void = kein Returnwert)
// Eine Funktion kann mehrere return-Anweisungen haben! return beendet die Funktion sofort!
static bool Primzahltest(int zahl)
static bool Primzahltest(int meineZahl)
{
if (zahl % 2 == 0)
if (meineZahl < 2)
return false;
if (meineZahl == 2)
return true;
if (meineZahl % 2 == 0)
return false;
int teilergrenze = (int)Math.Sqrt(zahl);
for (int teiler = 3; /* n == 0 && */ teiler <= teilergrenze; teiler += 2)
int teilergrenze = (int)Math.Sqrt(meineZahl);
for (int teiler = 3; teiler <= teilergrenze; teiler += 2)
{
if (zahl % teiler == 0)
if (meineZahl % teiler == 0)
return false;
}
return true;
......@@ -50,7 +54,11 @@ namespace _04_IntroFunktionen
//dings();
Console.WriteLine(zahlIstPrimzahl);
zahl = incr(Convert.ToInt32(Console.ReadLine()));
incr(zahl); // --> incr(11)
incr(zahl + 1); // --> incr(12)
incr(10); // --> incr(10)
zahl = incr(zahl);
zahl = incr(Convert.ToInt32(Console.ReadLine())); // --> incr(123)
Console.WriteLine(zahl*zahl);
Console.WriteLine(kreisflaeche(1.5));
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_04_Uebung_Di</RootNamespace>
</PropertyGroup>
</Project>
using System;
namespace _04_Uebung_Di
{
class Program
{
static void Quadrat(int n)
{
for (int zeile = 0; zeile < n; zeile++)
{
for (int i = 0; i < n; i++)
{
// Console.Write($"{zeile*n+i,3}");
Console.Write("X");
}
Console.WriteLine();
}
}
static void Dreieck(int n)
{
for (int zeile = 1; zeile <= n; zeile++)
{
for (int i = 0; i < zeile; i++)
{
Console.Write("X");
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
Quadrat(5);
Dreieck(10);
}
}
}
......@@ -33,7 +33,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Do_While_Schleife", "04
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 IntroFunktionen", "04 IntroFunktionen\04 IntroFunktionen.csproj", "{B22C3D1C-0375-451F-885F-51015FE6633C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "04 Dreiecke_Mo_Ubg", "04 Dreiecke_Mo_Ubg\04 Dreiecke_Mo_Ubg.csproj", "{40E4407D-F055-463C-96E6-63F948EF19E7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Dreiecke_Mo_Ubg", "04 Dreiecke_Mo_Ubg\04 Dreiecke_Mo_Ubg.csproj", "{40E4407D-F055-463C-96E6-63F948EF19E7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04 Uebung_Di", "04 Uebung_Di\04 Uebung_Di.csproj", "{F17A5B61-A944-4719-8CBA-BBD034A73B96}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "04 Funktionen_2", "04 Funktionen_2\04 Funktionen_2.csproj", "{258D6D4B-BECB-44FA-A15A-2C310EBD9477}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -105,6 +109,14 @@ Global
{40E4407D-F055-463C-96E6-63F948EF19E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40E4407D-F055-463C-96E6-63F948EF19E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40E4407D-F055-463C-96E6-63F948EF19E7}.Release|Any CPU.Build.0 = Release|Any CPU
{F17A5B61-A944-4719-8CBA-BBD034A73B96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F17A5B61-A944-4719-8CBA-BBD034A73B96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F17A5B61-A944-4719-8CBA-BBD034A73B96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F17A5B61-A944-4719-8CBA-BBD034A73B96}.Release|Any CPU.Build.0 = Release|Any CPU
{258D6D4B-BECB-44FA-A15A-2C310EBD9477}.Debug|Any CPU.ActiveCfg = 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.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment