# Python code
def greet(name):
return "Hello, " + name + "!"
print(greet("Alice"))
print(greet("Bob"))
C++ Equivalent
// C++ code
#include <iostream>
#include <string>
using namespace std;
string greet(string name) {
return "Hello, " + name + "!";
}
int main() {
cout << greet("Alice") << endl;
cout << greet("Bob") << endl;
return 0;
}
- Python code defines a function named
greet
that takes a parameter name
and returns a greeting message. - The
print
statements call the greet
function with different names and print the result. - The C++ equivalent code includes the necessary header files for input/output and string manipulation.
- The
greet
function in C++ uses the string
data type and the +
operator for string concatenation. - In the
main
function, cout
is used to print the results of calling the greet
function with different names. - The program ends with a
return 0;
statement to indicate successful execution.
```