# day05 - Functions that return values 1 Return values...

This preview shows pages 1–8. Sign up to view the full content.

1 Functions that return values

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

View Full Document
2 Return values return : To send a value out as the result of a function, which can be used in an expression. A return value is like the opposite of a parameter. Parameters pass information in from the caller to the function. Return values pass information out from a function to its caller. How would this be useful?
3 Including math.h gives you access to many useful functions to perform mathematical calculations. All return values of these functions are of type double . Mathematical functions function name Description cos( value ) cosine, in radians fabs( value ) absolute value log10( value ) logarithm base 10 pow( base , exponent ) base to the exponent power round( value ) nearest whole number sin( value ) sine, in radians sqrt( value ) square root

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

View Full Document
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 } Notice that the preceding calls are used in expressions; they can be printed, stored into a variable, etc…
5 Return values The functions do NOT print results to the console. Instead, each function evaluates to produce (or return ) a numeric result, which can be used in an expression.

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

View Full Document
6 Exercises Evaluate the following expressions: fabs(-1.23) pow(4, 3) pow(10, -2) sqrt(121.0) - sqrt(256.0)
7 Writing functions that return values Defining a function that returns a value: <type> <name> ( <parameters> ) { <statement(s)> ; } Returning a value from a function: return <expression> ; Example: // Returns the slope of the line between the given points. double

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/29/2009 for the course EE 142 taught by Professor Bensollimketkai during the Spring '09 term at Washington State University .

### Page1 / 22

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

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

View Full Document
Ask a homework question - tutors are online