2.5. Declaring a Function Inline

Recall that fibon_elem() returns the Fibonacci element at a user-specified position within the sequence. In our original implementation, it calculates the sequence up to the requested position with each invocation. It also tests whether the requested position is reasonable. We can simplify its implementation by factoring subtasks into separate functions:

 bool is_size_ok( int size ) { const int max_size = 1024; if ( size <= 0 || size > max_size ) { cerr << "Oops: requested size is not supported : " << size << " -- can't fulfill request.\n"; return false; } return true; } // calculate up to size elements of Fibonacci sequence // return address of static container holding elements const vector<int> *fibon_seq( ...

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.