0
What is return? When and why do we need to use this?
3 Respuestas
+ 7
return is used to return somthing back to the method you called it from
+ 1
A function with a return statement returns some sort of value that can be used by the thing calling the function, in statically typed languages (C++, Java, C# ect) you need to specify what type that will be normally near the function name, example in C++
include <iostream>
using namespace std;
int add(int num) { //int at the start tells us the function will return an int
return num + 2; // anything that uses this function variable, another function ect will receive the result of num + 2
}
int main() {
cout << add(3) << "\n"; // add gives the result of num (3) + 2 to cout which then prints it out
return 0;
}
+ 1
to return something or to quit