Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) function

 

This page is about two different functions:

  1. C++98 function (general): a piece of callable code
  2. Boost boost::function: the Boost.Function library

 

 

 

 

 

C++98 function (general)

 

A function is a callable piece of code that performs a specific general task with as little information as possible (Why would you want this? Go to this page to view the purpose of using functions).

 

A function declaration states what a function needs and returns. A function definition states how a function uses its arguments and calculates what to return.

 

#include <iostream>

//SayHello is a function that takes no arguments and returns nothing
void SayHello()
{
  std::cout << "Hello world" << std::endl;
}

//main is a special function: this form of main takes no arguments
//and returns the program's error code
int main()
{
  SayHello();
}

 

A function that accompanies a class (and is non-friend) is called a free function.

 

Consider using proper function design. Avoid writing long functions [1-2].

 

 

 

 

 

 

Boost boost::function: the Boost.Function library

 

boost::function is part of the Boost.Function library for working with functions in general, including member functions.

 

 

 

 

 

Boost boost::function example 1

 

Below are two pieces of near-identical code, the first using function pointers to global functions, the second using boost::function.

 

#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>

int f1(const int i) { return i + 1; }
int f2(const int i) { return i + 2; }
int f3(const int i) { return i + 3; }

int main()
{
  typedef int(*Function)(const int);
  std::vector<Function> v;
  v.push_back(f1);
  v.push_back(f2);
  v.push_back(f3);
  std::random_shuffle(v.begin(),v.end());

  int x = 0;
  BOOST_FOREACH(const Function& f,v)
  {
    x = f(x);
    std::cout << x << '\n';
  }
}

 

 

 

#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/function.hpp>

int f1(const int i) { return i + 1; }
int f2(const int i) { return i + 2; }
int f3(const int i) { return i + 3; }

int main()
{
  std::vector<boost::function<int(const int)> > v;
  v.push_back(f1);
  v.push_back(f2);
  v.push_back(f3);
  std::random_shuffle(v.begin(),v.end());

  int x = 0;
  BOOST_FOREACH(const boost::function<int(const int)>& f,v)
  {
    x = f(x);
    std::cout << x << '\n';
  }
}

 

Possible screen output:

 

1
4
6

 

 

 

 

 

Boost boost::function example 2

 

#include <iostream>
#include <boost/function.hpp>

struct Speaker
{
  Speaker(const std::string hello_message, const std::string bye_message)
    : m_hello_message(hello_message),
      m_bye_message(bye_message)
  {

  }

  void SayHello() const { std::cout << m_hello_message << '\n'; }
  void SayBye() const { std::cout << m_bye_message << '\n'; }

  const std::string m_hello_message;
  const std::string m_bye_message;
};

int main()
{
  const Speaker s1("Hello!","Bye!");
  const Speaker s2("HELLO!","BYE!");

  const boost::function<void (const Speaker*)> say_hello_function = &Speaker::SayHello;
  const boost::function<void (const Speaker*)> say_bye_function = &Speaker::SayBye;

  say_hello_function(&s1);
  say_bye_function(&s1);

  say_hello_function(&s2);
  say_bye_function(&s2);
}

 

Screen output:

 

Hello!
Bye!
HELLO!
BYE!

 

 

 

 

 

External links

 

 

 

 

 

 

References

 

  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 20: 'Avoid long functions. Avoid deep nesting'
  2. Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. AV Rule 1: 'Any one function (or method) will contain no more than 200 logical source lines of code.'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict