7 function call in programming lpu cse101 c

This preview shows page 7 - 13 out of 21 pages.

7
Image of page 7
Function Call in Programming ©LPU CSE101 C Programming In programming, the useof a function in an expression is referred to as a call . We say that the statement printf("%d\n", abs(-2) ); invokes or calls the function abs ; the statement passes an argument of -2 to the function; the function abs returns a value of 2. 8
Image of page 8
Math Function vs Programming Function ©LPU CSE101 C Programming An important distinction between a function in mathematics and a function in programming: a function in mathematics is simply a definition “this name means that expression”, while a function in programming is an action (“this name means execute that sequence of statements”). 9
Image of page 9
C Standard Library ©LPU CSE101 C Programming Every implementation of C comes with a standard library of predefined functions. Note that, in programming, a library is a collection of functions . The functions that are common to all versions of C are known as the C Standard Library . 10
Image of page 10
Math Library Functions ©LPU CSE101 C Programming Math library functions perform common mathematical calculations #include <math.h> Format for calling maths functions functionName( argument ); If multiple arguments, use comma-separated list Example: printf( "%.2f", sqrt( 900.0 ) ); Calls function sqrt , which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions 11
Image of page 11
Math Library Functions ©LPU CSE101 C Programming Function Description Example sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 exp( x ) exponential function e x exp( 1.0 ) is 2.718282 exp( 2.0 ) is 7.389056 log( x ) natural logarithm of x (base e ) log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 log10( x ) logarithm of x (base 10) log10( 1.0 ) is 0.0 log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0 fabs( x ) absolute value of x fabs( 5.0 ) is 5.0 fabs( 0.0 ) is 0.0 fabs( -5.0 ) is 5.0 ceil( x ) rounds x to the smallest integer that is greater than or equal to x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 floor( x )
Image of page 12
Image of page 13

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture