Functions
Description
Functions contain a block of code that can be called from other parts of the program. Creating a function allows you to reuse code, and makes your code more readable and easier to maintain.
Functions are declared with a name, a return type, and a list of parameters.
Example 1 - Basic Function
The function above is named "sayHi". It has no parameters, and it returns nothing (void). The function prints "Hi!" to the console when it is called, and then ends and execution returns to the calling function. Inside the function, theputs() function is called to print "Hi!" to the console. This transfers control away from the sayHi() function temporarily to print "Hi!", then returns control back to the sayHi() function after it is done printing. An example of calling this function is shown below:
Example 2 - Function with Parameters
void sayHiTo(std::string name) {
// Prints "Hi, <name>!" to the console.
printf("Hi, %s!\n", name.c_str());
}
std::string. The function returns nothing (void). The function uses its parameter name to print "Hi, <name>!" to the console when it is called, and then ends and execution returns to the calling function.
Example 3 - Function with Return Value
The function above is named "add". It has two parameters, "a" and "b", which are both of typeint. The function returns an int value, the sum of "a" and "b". An example of calling this function is shown below: