Returning Function Results
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 ...
Get Programming in Objective-C, Sixth Edition 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.