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

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

View Full Document Right Arrow Icon
    1 Functions that return values
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 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?
Background image of page 2
    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
Background image of page 3

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

View Full DocumentRight Arrow Icon
    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…
Background image of page 4
    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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
    6 Exercises Evaluate the following expressions: fabs(-1.23) pow(4, 3) pow(10, -2) sqrt(121.0) - sqrt(256.0)
Background image of page 6
    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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
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 Right Arrow Icon
Ask a homework question - tutors are online