As with methods, a function can return a value. The type of value returned with the `return`

statement must be consistent with the return type declared for the function. A function declaration that starts like this

float kmh_to_mph (float km_speed)

begins the definition of a function `kmh_to_mph`

, which takes one `float`

argument called `km_speed`

and returns a floating-point value. Similarly,

int gcd (int u, int v)

defines a function called `gcd`

with integer arguments `u`

and `v`

and returns an integer value.

Let’s rewrite the greatest common divisor algorithm used in Program 5.7 in function form. The two arguments to the function are the two numbers whose greatest common divisor (`gcd`

) you want to ...

Start Free Trial

No credit card required