Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Return-Anweisung in Funktionen | Einführung in Funktionen
C++-Einführung

bookReturn-Anweisung in Funktionen

Die return-Anweisung beendet die Ausführung einer Funktion und gibt einen Wert eines vordefinierten Typs zurück.

function.h

function.h

copy
12345
int func() // int - predefined { int variable = 10; return variable; // variable = 10 }

Wenn der Typ falsch angegeben wird, verhält sich die Funktion unvorhersehbar.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }

Das bedeutet, vor der Erstellung einer Funktion muss der Typ der zurückgegebenen Daten angegeben werden. In C++ gibt es außerdem spezielle void-Funktionen. Funktionen dieses Datentyps müssen keinen Wert zurückgeben:

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

copy
123456789101112
#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }

Es kann mehrere Rückgaben innerhalb von Funktionen geben, und jede wird nur unter bestimmten Bedingungen ausgeführt.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }

Wenn es zwei Rückgaben gibt, wird die zweite Rückgabe ignoriert:

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
question mark

Was passiert, wenn eine return-Anweisung innerhalb einer Funktion ausgeführt wird?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 3.85

bookReturn-Anweisung in Funktionen

Swipe um das Menü anzuzeigen

Die return-Anweisung beendet die Ausführung einer Funktion und gibt einen Wert eines vordefinierten Typs zurück.

function.h

function.h

copy
12345
int func() // int - predefined { int variable = 10; return variable; // variable = 10 }

Wenn der Typ falsch angegeben wird, verhält sich die Funktion unvorhersehbar.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }

Das bedeutet, vor der Erstellung einer Funktion muss der Typ der zurückgegebenen Daten angegeben werden. In C++ gibt es außerdem spezielle void-Funktionen. Funktionen dieses Datentyps müssen keinen Wert zurückgeben:

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

copy
123456789101112
#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }

Es kann mehrere Rückgaben innerhalb von Funktionen geben, und jede wird nur unter bestimmten Bedingungen ausgeführt.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }

Wenn es zwei Rückgaben gibt, wird die zweite Rückgabe ignoriert:

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
question mark

Was passiert, wenn eine return-Anweisung innerhalb einer Funktion ausgeführt wird?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 3
some-alt