<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Parameter bei Funktionen]]></title><description><![CDATA[<p>Hi,</p>
<p>ich lese im Moment das Buch &quot;C++ in 21 Tagen&quot; und dort steht auch:</p>
<pre><code>Die an eine Funktion übergebenen Parameter gelten ebenfalls als lokale Variablen und lassen sich genauso verwenden, als hätte man sie innerhalb des Funktionsrumpfes definiert. Listing 5.2 zeigt ein Beispiel für die Verwendung von Parametern und lokal definierten Variablen innerhalb einer Funktion.
</code></pre>
<p>Wieso aber funktioniert das hier:<br />
[cpp]#include &lt;cstdlib&gt;<br />
#include &lt;iostream&gt;</p>
<p>using namespace std;</p>
<p>void funktion(int*);</p>
<p>int main(int argc, char *argv[])<br />
{<br />
int zahlen[6];<br />
for(int x=0; x&lt;6; x++)<br />
{<br />
zahlen[x]=x;<br />
<div class="plugin-markdown"><input type="checkbox" id="checkbox3928" checked="true" /><label for="checkbox3928">&lt;&lt; endl;</label></div><br />
}<br />
cout &lt;&lt; endl;</p>
<p>funktion(zahlen);</p>
<p>for(int x=0; x&lt;6; x++)<br />
<div class="plugin-markdown"><input type="checkbox" id="checkbox3929" checked="true" /><label for="checkbox3929">&lt;&lt; endl;</label></div></p>
<p>system(&quot;PAUSE&quot;);<br />
return 0;<br />
}</p>
<p>void funktion(int *arr)<br />
{<br />
for(int x=0; x&lt;6; x++)<br />
arr[x]=9;<br />
}[cpp]</p>
<p>Ausgabe:</p>
<pre><code>1. Zahl: 0
2. Zahl: 1
3. Zahl: 2
4. Zahl: 3
5. Zahl: 4
6. Zahl: 5

1. Zahl: 9
2. Zahl: 9
3. Zahl: 9
4. Zahl: 9
5. Zahl: 9
6. Zahl: 9
</code></pre>
<p>Wieso wird auch der Array &quot;zahlen&quot; verändert?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/220007/parameter-bei-funktionen</link><generator>RSS for Node</generator><lastBuildDate>Wed, 07 Oct 2026 14:02:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/220007.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Aug 2008 19:02:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Parameter bei Funktionen on Sun, 10 Aug 2008 19:02:29 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich lese im Moment das Buch &quot;C++ in 21 Tagen&quot; und dort steht auch:</p>
<pre><code>Die an eine Funktion übergebenen Parameter gelten ebenfalls als lokale Variablen und lassen sich genauso verwenden, als hätte man sie innerhalb des Funktionsrumpfes definiert. Listing 5.2 zeigt ein Beispiel für die Verwendung von Parametern und lokal definierten Variablen innerhalb einer Funktion.
</code></pre>
<p>Wieso aber funktioniert das hier:<br />
[cpp]#include &lt;cstdlib&gt;<br />
#include &lt;iostream&gt;</p>
<p>using namespace std;</p>
<p>void funktion(int*);</p>
<p>int main(int argc, char *argv[])<br />
{<br />
int zahlen[6];<br />
for(int x=0; x&lt;6; x++)<br />
{<br />
zahlen[x]=x;<br />
<div class="plugin-markdown"><input type="checkbox" id="checkbox3928" checked="true" /><label for="checkbox3928">&lt;&lt; endl;</label></div><br />
}<br />
cout &lt;&lt; endl;</p>
<p>funktion(zahlen);</p>
<p>for(int x=0; x&lt;6; x++)<br />
<div class="plugin-markdown"><input type="checkbox" id="checkbox3929" checked="true" /><label for="checkbox3929">&lt;&lt; endl;</label></div></p>
<p>system(&quot;PAUSE&quot;);<br />
return 0;<br />
}</p>
<p>void funktion(int *arr)<br />
{<br />
for(int x=0; x&lt;6; x++)<br />
arr[x]=9;<br />
}[cpp]</p>
<p>Ausgabe:</p>
<pre><code>1. Zahl: 0
2. Zahl: 1
3. Zahl: 2
4. Zahl: 3
5. Zahl: 4
6. Zahl: 5

1. Zahl: 9
2. Zahl: 9
3. Zahl: 9
4. Zahl: 9
5. Zahl: 9
6. Zahl: 9
</code></pre>
<p>Wieso wird auch der Array &quot;zahlen&quot; verändert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562368</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562368</guid><dc:creator><![CDATA[Preca]]></dc:creator><pubDate>Sun, 10 Aug 2008 19:02:29 GMT</pubDate></item><item><title><![CDATA[Reply to Parameter bei Funktionen on Sun, 10 Aug 2008 19:03:25 GMT]]></title><description><![CDATA[<p>Entschuldigung, hier noch mal mit CPP-Tag</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

void funktion(int*);

int main(int argc, char *argv[])
{
int zahlen[6];
for(int x=0; x&lt;6; x++)
{
zahlen[x]=x;
cout &lt;&lt; x+1 &lt;&lt; &quot;. Zahl: &quot; &lt;&lt; zahlen[x] &lt;&lt; endl;
}
cout &lt;&lt; endl;

funktion(zahlen);

for(int x=0; x&lt;6; x++)
cout &lt;&lt; x+1 &lt;&lt; &quot;. Zahl: &quot; &lt;&lt; zahlen[x] &lt;&lt; endl;

system(&quot;PAUSE&quot;);
return 0;
}

void funktion(int *arr)
{
for(int x=0; x&lt;6; x++)
arr[x]=9;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1562369</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562369</guid><dc:creator><![CDATA[Preca]]></dc:creator><pubDate>Sun, 10 Aug 2008 19:03:25 GMT</pubDate></item><item><title><![CDATA[Reply to Parameter bei Funktionen on Sun, 10 Aug 2008 19:13:42 GMT]]></title><description><![CDATA[<p>Das ist eine gute Frage.</p>
<p>Wenn du aber mal die Funktionsdeklaration nochmal anschaust, wirst du feststellen, dass du da kein Array per copy überibgst, sondern lediglich einen Zeiger. Sprich auf das erste Element des Arrays. Wenn du jetzt mit dem Zeiger dort arbeitest, dann veränderst du nicht eine lokale Version des Arrays, sondern die aus der main Funktion.</p>
<p>Wenn du eine lokale Version haben willst, kommst du nich drum herum lokal ein Array zu erstellen und mithilfe des Zeigers des anderen Arrays zu kopieren. Stück für Stück.</p>
<p>Oder du nimmst std::vector, dort wird das Array, wenn nötig kopiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562373</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562373</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 10 Aug 2008 19:13:42 GMT</pubDate></item><item><title><![CDATA[Reply to Parameter bei Funktionen on Sun, 10 Aug 2008 19:20:08 GMT]]></title><description><![CDATA[<p>Danke drakon, ist einleuchtend <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1562378</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1562378</guid><dc:creator><![CDATA[Preca]]></dc:creator><pubDate>Sun, 10 Aug 2008 19:20:08 GMT</pubDate></item></channel></rss>