O'Reilly logo

Essential C++ by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

  1. 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.

  2. The name of the function. foo() is a common name. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required