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

Kursinhalt

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 6

Fragen Sie AI

expand
ChatGPT

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

course content

Kursinhalt

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 6
Wir sind enttäuscht, dass etwas schief gelaufen ist. Was ist passiert?
some-alt