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) { return a+b; } int main() { // Call the function to be executed cout << sum(4, 5); return 0; }
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.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Kysy minulta kysymyksiä tästä aiheesta
Tiivistä tämä luku
Näytä käytännön esimerkkejä
Awesome!
Completion rate improved to 2.94
Return
Pyyhkäise näyttääksesi valikon
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) { return a+b; } int main() { // Call the function to be executed cout << sum(4, 5); return 0; }
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.
Kiitos palautteestasi!