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

Kursinnhold

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 6

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

course content

Kursinnhold

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 6
Vi beklager at noe gikk galt. Hva skjedde?
some-alt