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

Kurssisisältö

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 6

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

course content

Kurssisisältö

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 6
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt