C++ FAQs
Authors: Cline M. Lomow G. Girou M.
Published year: 2005
Pages: 20-22/566
Buy this book on amazon.com >>

FAQ 2.03 What are the basics of functions?

graphics/new_icon.gif

Functions are one of the important ways to decompose software into smaller, manageable chunks . Functions can have return values (for example, a function that computed a value might return that value), or they can return nothing. If they return nothing, the return type is stated as void and the function is sometimes called a procedure.

In the following example, function f() takes no parameters and returns nothing (that is, its return type is void ), and function g() takes two parameters of type int and returns a value of type float .

void f()
{
  // ...
}

float g(int x, int y)
{
  float sum = x + y;
  float avg = sum / 2.0;
  return avg;
}

int main()
{
  f();
  float z = g(2, 3);
}

FAQ 2.04 What are the basics of default parameters?

graphics/new_icon.gif

C++ allows functions to have default parameters. This is useful when a parameter should have a specified value when the caller does not supply a value. For example, suppose that the value 42 should be passed to the function f() when the caller does not supply a value. In that case, it would make the calling code somewhat simpler if this parameter value were supplied as a default value:

void f(int x=42);

<-- 1

void f(int x)

<-- 2

{
  //...
}

int main()
{
  f(29);

<-- 3

f();

<-- 4

}

(1) Declare the default parameter(s) in the function declaration

(2) Don't repeat the default parameter(s) in the function definition

(3) Passes 29 to f()

(4) Passes 42 to f()

FAQ 2.05 What are the basics of local (auto) objects?

graphics/new_icon.gif

C++ extends the variable declaration syntax from built-in types (e.g., int i; ) to objects of user -defined types. The syntax is the same: TypeName VariableName . For example, if the header file "Car.hpp" defines a user-defined type called Car , objects ( variables ) of class (type) Car can be created:

#include "Car.hpp"

<-- 1

void f()
{
  Car a;

<-- 1

a.startEngine();

<-- 2

a.tuneRadioTo("AM", 770);

<-- 3

}

<-- 4

int main()
{
  f();
}

(1) Define class Car

(1) 1: Create an object

(2) 2: Call a member function

(3) 3: Call another member function

(4) 4: Destroy the object

When control flows over the line labeled 1: Create an object, the runtime system creates a local (auto) object of class Car . The object is called a and can be accessed from the point where it is created to the } labeled 4: Destroy the object.

When control flows over the line labeled 2: Call a member function, the startEngine() member function (a.k.a. method) is called for object a . The compiler knows that a is of class Car so there is no need to indicate that the proper startEngine() member function is the one from the Car class. For example, there could be other classes that also have a startEngine() member function ( Airplane , LawnMower , and so on), but the compiler will never get confused and call a member function from the wrong class.

When control flows over the line labeled 3: Call another member function, the tuneRadioTo() member function is called for object a . This line shows how parameters can be passed to member functions.

When control flows over the line labeled 4: Destroy the object, object a is automatically destroyed . If the Car class has some special cleanup activities that need to take place when an object goes away, the writer of the class would include a destructor in the class and the runtime system would automagically call the destructor (dtor) when the object goes away; see FAQ 20.03. Local objects such as a are sometimes called automatic objects or stack objects, and they are said to go out of scope at the } line.

UML uses the following notation to show a class Car that contains member functions startEngine() and tuneRadioTo() :

graphics/02fig01.gif

C++ FAQs
Authors: Cline M. Lomow G. Girou M.
Published year: 2005
Pages: 20-22/566
Buy this book on amazon.com >>

Similar books on Amazon