day05 - Return values Functions that return values return:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Functions that return values 2 Return values s return : To send a value out as the result of a function, which can be used in an expression. s A return value is like the opposite of a parameter. s Parameters pass information in from the caller to the function. s Return values pass information out from a function to its caller. s How would this be useful? 3 s Including math.h gives you access to many useful functions to perform mathematical calculations. s All return values of these functions are of type double . Mathematical functions nearest whole number round( value ) square root sqrt( value ) sine, in radians sin( value ) base to the exponent power pow( base , exponent ) logarithm base 10 log10( value ) absolute value fabs( value ) cosine, in radians cos( value ) Description function name 4 Using the math.h functions #include <stdio.h> #include <math.h> int main() { double square_root = sqrt(121.0) ; printf("%lf\n", square_root); // 11.0 double absolute_value = fabs(-50) ; printf("%lf\n", absolute_value); // 50.0 printf("%lf\n", pow(3, 2) + 3); // 12.0 } b Notice that the preceding calls are used in expressions; they can be printed, stored into a variable, etc… 5 Return values s The functions do NOT print results to the console. s Instead, each function evaluates to produce (or return ) a numeric result, which can be used in an expression. main fabs -42 round 2.71 42.0 3.0 6 Exercises s Evaluate the following expressions: b fabs(-1.23) b pow(4, 3) b pow(10, -2) b sqrt(121.0) - sqrt(256.0)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 7 Writing functions that return values s Defining a function that returns a value: <type> <name> ( <parameters> ) { <statement(s)> ; } s Returning a value from a function: return <expression> ; s Example: // Returns the slope of the line between the given points. double
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

day05 - Return values Functions that return values return:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online