Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Return | Functions
C++ Intermediate | Mobile-Friendly
course content

Kursusindhold

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Return

Previously we used functions like action makers, we gave the parameters (or no), and the function did some actions we wanted.

However, the keyword void, which we used in previous examples, shouldn’t return a value. If you want the function to return any values, you have to specify the type of value you want to get instead of void and use the keyword return. For instance:

12345678910
// Declaration of the function int sum(int a, int b) { &nbsp;&nbsp;&nbsp;&nbsp;return a+b; } int main() { &nbsp;&nbsp;&nbsp;&nbsp;// Call the function to be executed &nbsp;&nbsp;&nbsp;&nbsp;cout << sum(4, 5); &nbsp;&nbsp;&nbsp;&nbsp;return 0; }
copy

Here the function sum() gets 2 integer values and returns their summary (sum) - integer value, so before the declaration, we use int.

The function firstly returns the value, and then we print it in the main function.

question mark

If we want our function to return the char value, we should specify

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 6

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

course content

Kursusindhold

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Return

Previously we used functions like action makers, we gave the parameters (or no), and the function did some actions we wanted.

However, the keyword void, which we used in previous examples, shouldn’t return a value. If you want the function to return any values, you have to specify the type of value you want to get instead of void and use the keyword return. For instance:

12345678910
// Declaration of the function int sum(int a, int b) { &nbsp;&nbsp;&nbsp;&nbsp;return a+b; } int main() { &nbsp;&nbsp;&nbsp;&nbsp;// Call the function to be executed &nbsp;&nbsp;&nbsp;&nbsp;cout << sum(4, 5); &nbsp;&nbsp;&nbsp;&nbsp;return 0; }
copy

Here the function sum() gets 2 integer values and returns their summary (sum) - integer value, so before the declaration, we use int.

The function firstly returns the value, and then we print it in the main function.

question mark

If we want our function to return the char value, we should specify

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 6
Vi beklager, at noget gik galt. Hvad skete der?
some-alt