Absolute Value Function in C • The abs function calculates the absolute value of its argument. It’s the C analogue of the mathematical function a(y) = | y | (the absolute value function) a= abs(y);
Function Call in Programming In programming, the use of 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.
Math Function vs Programming Function 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”).
C Standard Library • 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.
Math Library Functions • 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
sqrt() The square root function, sqrt(x), calculates the non-negative square root of x for x >= 0.0 sqrt(2.25) is 1.5 sqrt(25) is 5.0 The function sqrt is of the type double and has only one parameter.
fabs() • fabs calculates the absolute value of a float argument. fabs(2.25) is 2.25 fabs(-25.0) is 25.0 The function fabs is of the type double and has only one parameter.
floor() The floor function, floor, calculates the largest whole number that is not greater than x. floor(48.79) is 48.0 floor(48.03) is 48.0 floor(47.79) is 47.0 The function floor is of the type double and has only one parameter.
ceil() The ceil function, ceil, calculates the smallest whole number that is not less than x. ceil(48.79) is 49 ceil(48.03) is 49 ceil(47.79) is 48 The function ceil is of the type double and has only one parameter.