Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Return Instructie in Functies | Introductie tot Functies
C++ Introductie
course content

Cursusinhoud

C++ Introductie

C++ Introductie

1. Aan de Slag
2. Inleiding tot Operatoren
3. Variabelen en Gegevenstypen
4. Introductie tot Programmastroom
5. Introductie tot Functies

book
Return Instructie in Functies

De return-instructie beëindigt de uitvoering van een functie en retourneert een waarde van een vooraf gedefinieerd type.

h

function

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

Als het type onjuist is gespecificeerd, zal de functie onvoorspelbaar gedrag vertonen.

cpp

main

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; }

Dat wil zeggen, voordat een functie wordt gemaakt, moet het type gegevens dat het retourneert worden gespecificeerd. Ook zijn er in C++ speciale void functies. Functies van dit gegevenstype mogen niets retourneren:

cpp

first_example

cpp

second_example

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

Er kunnen meerdere returns binnen functies zijn, en elk zal alleen onder bepaalde voorwaarden worden uitgevoerd.

cpp

main

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 }

Als er twee returns zijn, wordt de tweede returnfunctie genegeerd:

cpp

main

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 }

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 3

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

course content

Cursusinhoud

C++ Introductie

C++ Introductie

1. Aan de Slag
2. Inleiding tot Operatoren
3. Variabelen en Gegevenstypen
4. Introductie tot Programmastroom
5. Introductie tot Functies

book
Return Instructie in Functies

De return-instructie beëindigt de uitvoering van een functie en retourneert een waarde van een vooraf gedefinieerd type.

h

function

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

Als het type onjuist is gespecificeerd, zal de functie onvoorspelbaar gedrag vertonen.

cpp

main

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; }

Dat wil zeggen, voordat een functie wordt gemaakt, moet het type gegevens dat het retourneert worden gespecificeerd. Ook zijn er in C++ speciale void functies. Functies van dit gegevenstype mogen niets retourneren:

cpp

first_example

cpp

second_example

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

Er kunnen meerdere returns binnen functies zijn, en elk zal alleen onder bepaalde voorwaarden worden uitgevoerd.

cpp

main

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 }

Als er twee returns zijn, wordt de tweede returnfunctie genegeerd:

cpp

main

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 }

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 3
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt