Modern C++: The Good Parts/At last, functions

Functions are so important to programming that teaching control structures has been rather tedious without them, so with great relief let's see how to make our own.


#include <iostream>
#include <string>

// Calls to this function evaluate to float.
float readFloat()
	std::string temp;
	std::cin >> temp;
	return std::stof(temp);

// This one returns char.
char readChar()
	std::string temp;
	std::cin >> temp;
	return temp[0];

// Another function returning float.
float evaluate(float a, char oper, float b)
	switch (oper)
	case '+':
		return a + b;
	case '-':
		return a - b;
	case '*':
		return a * b;
	case '/':
		return a / b;

int main()
	float a, b;
	char oper;

	// The ultimate truism, this never stops being true.
		std::cout << "Enter two numbers and an operator (+ - * /).\nOr press Ctrl+C to exit.\n";

		// Parse two floats from standard input.
		a = readFloat();
		b = readFloat();
		// And a character.
		oper = readChar();

		std::cout << a << " " << oper << " " << b << " = " << evaluate(a, oper, b) << "\n";


Woohoo, the code just got much easier to read, once you understand functions.

As the comments kind of explain, each function has a return type before its name. A function's return type will be the type of any calls to that function. A function call is an expression which causes a function to be run, such as std::stof(temp). When the control flows over a return statement, the containing function exits. The function's caller then receives the return value as the value of the function call and continues from that point.

return can stand in for break in a switch. The point is that control doesn't "fall through" from one case to another. C++ does allow that to happen, but it can be very confusing and it's rarely useful.

By now, you've probably noticed that main has a return type of int. It's the only non-void function that you don't need to return from; by default, it returns 0 when control reaches the bottom. 0 indicates that the program was successful, or ran without problems; any other value tells whoever ran the program that something went wrong. These values are called "exit codes", but you won't need to specify one anytime soon.

Functions also have parameters, the variables declared between their parentheses. Any function call must provide a value for each of the function's parameters; these values are called arguments.

Finally, a function must appear in the source before any call to it. However, C++ offers an important loophole to this that we'll cover later.


under construction


return type
the type of value that any call to a specific function will be resolved to.
function call
an expression which causes a function to be run.
the function which contains a specific function call.
return value
the value that a specific function call will be resolved to.
a return type meaning that nothing is returned.
a variable in a function which is assigned by each call to that function.
the value of a parameter for a specific call to a function.
Modern C++: The Good Parts
 ← Getting loopy At last, functions