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

Conteúdo do Curso

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

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

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.

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

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt