In this shot, we want to briefly illustrate how the return
keyword is used in a function in C++.
return
keyword?The return
keyword in C++ returns a value to a function. The return
keyword is declared inside a function to return a value from the given function.
#include <iostream>using namespace std;// creating a functionint myfunction(int x) {// declaring the return keyword for the functionreturn 10 + x;}int main() {cout << myfunction(5);return 0;}
myfunction()
, and pass an integer parameter value x
to the function.return
keyword for the function. This tells C++ that the expression right after the keyword should be returned whenever the function is called.myfunction
and pass an argument, 5
, to the function. The output is printed to the console.The return
keyword can also be used with two parameters.
#include <iostream>using namespace std;// ccreating a function having two parametersint myFunction(int x, int y) {// declaring the return keywordreturn x + y;}int main() {cout << myFunction(10, 5);return 0;}
myfunction
function with two parameter values, x
and y
.return
keyword.myfunction
and pass arguments to the function. The output is printed to the console.