Return-Instructie in Functies
De return-instructie beëindigt de uitvoering van een functie en retourneert een waarde van een vooraf gedefinieerd type.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Als het type onjuist is gespecificeerd, zal de functie onvoorspelbaar gedrag vertonen.
main.cpp
12345678910111213#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }
Dat wil zeggen, voordat een functie wordt aangemaakt, moet het type gegevens dat deze retourneert gespecificeerd worden. Daarnaast zijn er in C++ speciale void-functies. Functies van dit gegevenstype hoeven niets te retourneren:
first_example.cpp
second_example.cpp
123456789101112#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }
Er kunnen meerdere return-instructies binnen functies voorkomen, en elk zal alleen worden uitgevoerd onder bepaalde voorwaarden.
main.cpp
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 }
Als er twee return-instructies zijn, wordt de tweede return genegeerd:
main.cpp
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 }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
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
Return-Instructie in Functies
Veeg om het menu te tonen
De return-instructie beëindigt de uitvoering van een functie en retourneert een waarde van een vooraf gedefinieerd type.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Als het type onjuist is gespecificeerd, zal de functie onvoorspelbaar gedrag vertonen.
main.cpp
12345678910111213#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }
Dat wil zeggen, voordat een functie wordt aangemaakt, moet het type gegevens dat deze retourneert gespecificeerd worden. Daarnaast zijn er in C++ speciale void-functies. Functies van dit gegevenstype hoeven niets te retourneren:
first_example.cpp
second_example.cpp
123456789101112#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }
Er kunnen meerdere return-instructies binnen functies voorkomen, en elk zal alleen worden uitgevoerd onder bepaalde voorwaarden.
main.cpp
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 }
Als er twee return-instructies zijn, wordt de tweede return genegeerd:
main.cpp
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 }
Bedankt voor je feedback!