Basics of functions in C++


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);
}




Added on August 11, 2007 Comment
}