2.1. How to Write a Function
In this section, we write a function that returns the Fibonacci element at a position specified by the user. For example, if the user asks, “What is the eighth Fibonacci element?” our function answers, “21.” How do we go about defining this function?
We must define the following four parts to our function:
The return type of the function. Our function returns the element value at the user-specified position. Its value is of type int, so our function’s return type is also of type int. A function that does not return a value has a return type of void. A function that prints a Fibonacci sequence to the terminal, for example, is likely to declare its return type void.
The name of the function. foo() is a common name. ...
Get Essential C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.