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, dass vor der Erstellung einer Funktion der Typ der zurückgegebenen Daten festgelegt werden muss. In C++ gibt es außerdem spezielle void-Funktionen. Funktionen dieses Datentyps müssen nichts zurückgeben:

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

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

In Funktionen können mehrere Rückgabewerte vorhanden sein, wobei jeder nur unter bestimmten Bedingungen ausgeführt wird.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#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 zwei Rückgabewerte vorhanden sind, wird die zweite Rückgabeanweisung ignoriert:

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> int func() { int a = 50; int b = 6; return a; return b; } int main() { std::cout << func() << std::endl; //func calling }

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

Suggested prompts:

Can you explain what a void function is in more detail?

What happens if a function doesn't have a return statement?

Can you give an example of a function with multiple return statements?

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, dass vor der Erstellung einer Funktion der Typ der zurückgegebenen Daten festgelegt werden muss. In C++ gibt es außerdem spezielle void-Funktionen. Funktionen dieses Datentyps müssen nichts zurückgeben:

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

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

In Funktionen können mehrere Rückgabewerte vorhanden sein, wobei jeder nur unter bestimmten Bedingungen ausgeführt wird.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#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 zwei Rückgabewerte vorhanden sind, wird die zweite Rückgabeanweisung ignoriert:

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> int func() { int a = 50; int b = 6; return a; return b; } int main() { std::cout << func() << std::endl; //func calling }

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 3
some-alt