Listen
#include <iostream> #include <string> // Function prototype (declaration) int add(int a, int b); void greet(std::string name); int main() { // Calling a function and storing the returned value int sum = add(10, 5); std::cout << "Sum of 10 and 5 is: " << sum << std::endl; // Calling a function that returns void greet("John"); return 0; } // Function definition int add(int a, int b) { return a + b; } void greet(std::string name) { std::cout << "Hello, " << name << "!" << std::endl; }
This code block demonstrates a simple example of C++ functions. The program has two functions: add
and greet
. The add
function takes two integer arguments and returns their sum, while the greet
function takes a string argument and prints a greeting message.
The function prototypes are declared before the main
function, and the function definitions are provided after the main
function. The main
function calls both functions and demonstrates how to handle the return values and pass arguments to functions.