SlideShare a Scribd company logo
1 of 36
1. Standard Library Functions Outline
2. Functions in Mathematics #1
3. Functions in Mathematics #2
4. Functions in Mathematics #3
5. Function Argument
6. Absolute Value Function in C #1
7. Absolute Value Function in C #2
8. Absolute Value Function in C #3
9. A Quick Look at abs
10. Function Call in Programming
11. Math Function vs Programming
Function
12. C Standard Library
13. C Standard Library Function
Examples
14. Is the Standard Library Enough?
15. Math: Domain & Range #1
16. Math: Domain & Range #2
17. Math: Domain & Range #3
18. Programming: Argument Type
19. Argument Type Mismatch
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 1
20. Programming: Return Type
21. More on Function Arguments
22. Function Argument Example Part 1
23. Function Argument Example Part 2
24. Function Argument Example Part 3
25. Using the C Standard Math Library
26. Function Call in Assignment
27. Function Call in printf
28. Function Call as Argument
29. Function Call in Initialization
30. Function Use Example Part 1
31. Function Use Example Part 2
32. Function Use Example Part 3
33. Function Use Example Part 4
34. Evaluation of Functions in Expressions
35. Evaluation Example #1
36. Evaluation Example #2
“A relationship between two variables, typically x and y,
is called a function, if there is a rule that assigns to
each value of x one and only one value of y.”
http://www.themathpage.com/aPreCalc/functions.htm
So, for example, if we have a function
f(x) = x + 1
then we know that …
f(-2.5) = -2.5 + 1 = -1.5
f(-2) = -2 + 1 = -1
f(-1) = -1 + 1 = 0
f(0) = 0 + 1 = +1
f(+1) = +1 + 1 = +2
f(+2) = +2 + 1 = +3
f(+2.5) = +2.5 + 1 = +3.5
…
CS1313: Standard Library Functions Lesson
CS1313 Spring 2009 2
For example, if we have a function
f(x) = x + 1
then we know that …
f(-2.5) = -2.5 + 1 = -1.5
f(-2) = -2 + 1 = -1
f(-1) = -1 + 1 = 0
f(0) = 0 + 1 = +1
f(+1) = +1 + 1 = +2
f(+2) = +2 + 1 = +3
f(+2.5) = +2.5 + 1 = +3.5
…
CS1313: Standard Library Functions Lesson
CS1313 Spring 2009 3
Likewise, if we have a function
a(y) = | y |
then we know that …
a(-2.5) = | -2.5 | = +2.5
a(-2) = | -2 | = +2
a(-1) = | -1 | = +1
a(0) = | 0 | = 0
a(+1) = | +1 | = +1
a(+2) = | +2 | = +2
a(+2.5) = | +2.5 | = +2.5
…
CS1313: Standard Library Functions Lesson
CS1313 Spring 2009 4
f(x) = x + 1
a(y) = | y |
We refer to the thing inside the parentheses
immediately after the name of the function as the
argument (also known as the parameter) of the
function.
In the examples above:
 the argument of the function named f is x;
 the argument of the function named a is y.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 5
In my_number.c, we saw this:...
else if (abs(users_number –
computers_number) <=
close_distance) {
printf("Close, but no cigar.n");
} /* if (abs(...) <= close_distance) */...
So, what does abs do?
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) that we just looked
at.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 6
…
fabs(-2.5) returns 2.5
abs(-2) returns 2
abs(-1) returns 1
abs(0) returns 0
abs(1) returns 1
abs(2) returns 2
fabs(2.5) returns 2.5
…
CS1313: Standard Library Functions Lesson
CS1313 Spring 2009 7
CS1313: Standard Library Functions Lesson
CS1313 Spring 2009 8
We say “abs of -2 evaluates to 2” or “abs of -2
returns 2.”
Note that the function named abs calculates the
absolute value of an int argument, and fabs
calculates the absolute value of a float argument.
% cat abstest.c
#include <stdio.h>
int main ()
{ /* main */
const int program_success_code = 0;
printf("fabs(-2.5) = %fn", fabs(-2.5));
printf(" abs(-2) = %dn", abs(-2));
printf(" abs(-1) = %dn", abs(-1));
printf(" abs( 0) = %dn", abs( 0));
printf(" abs( 1) = %dn", abs( 1));
printf(" abs( 2) = %dn", abs( 2));
printf("fabs( 2.5) = %fn", fabs( 2.5));
return program_success_code;
} /* main */
% gcc -o abstest abstest.c
% abstest
fabs(-2.5) = 2.500000
abs(-2) = 2
abs(-1) = 1
abs( 0) = 0
abs( 1) = 1
abs( 2) = 2
fabs( 2.5) = 2.500000
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 9
Jargon: In programming, the use of a function in
an expression is referred to as an invocation,
or more colloquially as a call.
We say that the statement
printf("%dn", 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.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 10
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”). More on this later.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 11
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.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 12
Function
Name
Math
Name Value Example
abs(x) absolute value |x| abs(-1) returns 1
sqrt(x) square root x0.5 sqrt(2.0) returns 1.414…
exp(x) exponential ex exp(1.0) returns 2.718…
log(x) natural logarithm ln x log(2.718…) returns 1.0
log10(x) common logarithm log x log10(100.0) returns 2.0
sin(x) sine sin x sin(3.14…) returns 0.0
cos(x) cosine cos x cos(3.14…) returns -1.0
tan(x) tangent tan x tan(3.14…) returns 0.0
ceil(x) ceiling ┌ x ┐ ceil(2.5) returns 3.0
floor(x) floor
└ x ┘
floor(2.5) returns 2.0
CS1313: Standard Library Functions Lesson
CS1313 Spring 2009 13
It turns out that the set of C Standard Library
functions is grossly insufficient for most real
world tasks, so in C, and in most
programming languages, there are ways for
programmers to develop their own user-defined
functions.
We’ll learn more about user-defined functions in a
future lesson.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 14
In mathematics:
 The domain of a function is the set of numbers
that can be used for the argument(s) of that
function.
 The range is the set of numbers that can be
the result of that function.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 15
For example, in the case of the function
f(x) = x + 1
we define the domain of the function f to be the
set of real numbers (sometimes denoted R),
which means that the x in f(x) can be any real
number.
Similarly, we define the range of the function f to
be the set of real numbers, because for every
real number y there is some real number x such
that f(x) = y.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 16
On the other hand, for a function
q(x) = 1 / (x − 1)
the domain cannot include 1, because
q(1) = 1 / (1 – 1) = 1 / 0
which is undefined. So the domain might be R −
{1} (the set of all real numbers except 1).
In that case, the range of q would be R – {0}
(the set of all real numbers except 0), because
there’s no real number y such that 1/y is 0.
(Note: if you’ve taken calculus, you’ve seen that, as y
gets arbitrarily large, 1/y approaches 0 as a limit – but
“gets arbitrarily large” is not a real number, and
neither is “approaches 0 as a limit.”)
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 17
Programming has concepts that are analogous to
the mathematical domain and range:
argument type and return type.
For a given function in C, the argument type –
which corresponds to the domain in
mathematics – is the data type that C expects
for an argument of that function.
For example:
 the argument type of abs is int;
 the argument type of fabs is float.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 18
An argument type mismatch is when you pass
an argument of a particular data type to a function
that expects a different data type.
Some implementations of C WON’T check for you
whether the data type of the argument you pass is
correct. If you pass the wrong data type, you can
get a bogus answer.
This problem is more likely to come up when you
pass a float where the function expects an int.
In the reverse case, typically C simply promotes
the int to a float.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 19
Just as the programming concept of argument
type is analogous to the mathematical concept
of domain, so too the programming concept of
return type is analogous to the mathematical
concept of range.
The return type of a C function – which
corresponds to the range in mathematics – is
the data type of the value that the function
returns.
The return value is guaranteed to have that
data type, and the compiler gets upset – or you
get a bogus result – if you use the return value
inappropriately.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 20
In mathematics, a function argument can be:
 a number:
f(5) = 5 + 1 = 6
 a variable:
f(z) = z + 1
 an arithmetic expression:
f(5 + 7) = (5 + 7) + 1 = 12 + 1 = 13
 another function:
f(a(w)) = |w| + 1
 any combination of these; i.e., any general
expression whose value is in the domain of the
function:
f(3a(5w + 7)) = 3 (|5w + 7|) + 1
Likewise, in C the argument of a function can be any
non-empty expression that evaluates to an
appropriate data type, including an expression
containing a function call. CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 21
#include <stdio.h>
#include <math.h>
int main ()
{ /* main */
const float pi = 3.1415926;
const int program_success_code = 0;
float angle_in_radians;
printf("cos(%10.7f) = %10.7fn",
1.5707963, cos(1.5707963));
printf("cos(%10.7f) = %10.7fn", pi, cos(pi));
printf("Enter an angle in radians:n");
scanf("%f", &angle_in_radians);
printf("cos(%10.7f) = %10.7fn",
angle_in_radians, cos(angle_in_radians));
printf("fabs(cos(%10.7f)) = %10.7fn",
angle_in_radians,
fabs(cos(angle_in_radians)));
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 22
printf("cos(fabs(%10.7f)) = %10.7fn",
angle_in_radians,
cos(fabs(angle_in_radians)));
printf("fabs(cos(2.0 * %10.7f)) = %10.7fn",
angle_in_radians,
fabs(cos(2.0 * angle_in_radians)));
printf("fabs(2.0 * cos(%10.7f)) = %10.7fn",
angle_in_radians,
fabs(2.0 * cos(angle_in_radians)));
printf("fabs(2.0 * ");
printf("cos(1.0 / 5.0 * %10.7f)) = %10.7fn",
angle_in_radians,
fabs(2.0 *
cos(1.0 / 5.0 * angle_in_radians)));
return program_success_code;
} /* main */
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 23
% gcc -o funcargs funcargs.c -lm
% funcargs
cos( 1.5707963) = 0.0000000
cos( 3.1415925) = -1.0000000
Enter an angle in radians:
-3.1415925
cos(-3.1415925) = -1.0000000
fabs(cos(-3.1415925)) = 1.0000000
cos(fabs(-3.1415925)) = -1.0000000
fabs(cos(2.0 * -3.1415925)) = 1.0000000
fabs(2.0 * cos(-3.1415925)) = 2.0000000
fabs(2.0 * cos(1.0 / 5.0 * -3.1415925)) = 1.6180340
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 24
If you’re going to use functions like cos that are from the
part of the C standard library that has to do with math,
then you need to do two things:
1. In your source code, immediately below the
#include <stdio.h>
you must also put
#include <math.h>
2. When you compile, you must append -lm to the end
of your compile command:
gcc -o funcargs funcargs.c –lm
(Note that this is hyphen ell em, NOT hyphen one
em.)
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 25
Function calls are used in expressions in exactly the
same ways that variables and constants are used. For
example, a function call can be used on the right side
of an assignment or initialization:
float theta = 3.1415926 / 4.0;
float cos_theta;
…
cos_theta = cos(theta);
length_of_c_for_any_triangle =
a * a + b * b –
2 * a * b * cos(theta);
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 26
A function call can also be used in an expression
in a printf statement:
printf("%fn", 2.0);
printf("%fn", pow(cos(theta), 2.0));
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 27
Since any expression can be used as some
function’s argument, a function call can also be
used as an argument to another
function:
const float pi = 3.1415926;
printf("%fn",
1 + cos(asin(sqrt(2.0)/2.0) + pi));
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 28
Most function calls can be used in initialization,
as long as its arguments are literal
constants:
float cos_theta = cos(3.1415926);
This is true both in variable initialization and in
named constant initialization:
const float cos_pi = cos(3.1415926);
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 29
#include <stdio.h>
#include <math.h>
int main ()
{ /* main */
const float pi = 3.1415926;
const float cos_pi = cos(3.1415926);
const float sin_pi = sin(pi);
const int program_success_code = 0;
float phi = 3.1415926 / 4.0;
float cos_phi = cos(phi);
float theta, sin_theta;
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 30
theta = 3.0 * pi / 4;
sin_theta = sin(theta);
printf("2.0 = %fn", 2.0);
printf("pi = %fn", pi);
printf("theta = %fn", theta);
printf("cos(pi) = %fn", cos(pi));
printf("cos_pi = %fn", cos_pi);
printf("sin(pi) = %fn", sin(pi));
printf("sin_pi = %fn", sin_pi);
printf("sin(theta) = %fn", sin(theta));
printf("sin_theta = %fn", sin_theta);
printf("sin(theta)^(1.0/3.0) = %fn",
pow(sin(theta), (1.0/3.0)));
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 31
printf("1 + sin(acos(1.0)) = %fn",
1 + sin(acos(1.0)));
printf("sin(acos(1.0)) = %fn",
sin(acos(1.0)));
printf("sqrt(2.0) = %fn", sqrt(2.0));
printf("sqrt(2.0) / 2 = %fn", sqrt(2.0) / 2);
printf("acos(sqrt(2.0)/2.0) = %fn",
acos(sqrt(2.0)/2.0));
printf("sin(acos(sqrt(2.0)/2.0)) = %fn",
sin(acos(sqrt(2.0)/2.0)));
return program_success_code;
} /* main */
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 32
% gcc -o funcuse funcuse.c -lm
% funcuse
2.0 = 2.000000
pi = 3.141593
theta = 2.356194
cos(pi) = -1.000000
cos_pi = -1.000000
sin(pi) = 0.000000
sin_pi = 0.000000
sin(theta) = 0.707107
sin_theta = 0.707107
sin(theta)^(1.0/3.0) = 0.890899
1 + sin(acos(1.0)) = 1.000000
sin(acos(1.0)) = 0.000000
sqrt(2.0) = 1.414214
sqrt(2.0) / 2 = 0.707107
acos(sqrt(2.0)/2.0) = 0.785398
sin(acos(sqrt(2.0)/2.0)) = 0.707107
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 33
When a function call appears in an expression – for
example, on the right hand side of an assignment
statement – the function is evaluated just before
its value is needed, in accordance with the rules of
precedence order.
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 34
For example, suppose that x and y are float
variables, and that y has already been assigned
the value -10.0.
Consider this assignment statement:
x = 1 + 2.0 * 8.0 + fabs(y) / 4.0;
CS1313: Standard Library Functions
Lesson
CS1313 Spring 2009 35
x = 1 + 2.0 * 8.0 + fabs(y) / 4.0;
x = 1 + 16.0 + fabs(y) / 4.0;
x = 1 + 16.0 + fabs(-10.0) / 4.0;
x = 1 + 16.0 + 10.0 / 4.0;
x = 1 + 16.0 + 2.5;
x = 1.0 + 16.0 + 2.5;
x = 17.0 + 2.5;
x = 19.5;
CS1313: Standard Library Functions Lesson
CS1313 Spring 2009 36

More Related Content

What's hot

Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1Philip Schwarz
 
Electrical Engineering Exam Help
Electrical Engineering Exam HelpElectrical Engineering Exam Help
Electrical Engineering Exam HelpLive Exam Helper
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2IIUM
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsPhilip Schwarz
 
Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicosKmilo Bolaños
 
Bostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functionsBostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functionsSarah Sue Calbio
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical ComputingNaveed Rehman
 

What's hot (19)

Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Electrical Engineering Exam Help
Electrical Engineering Exam HelpElectrical Engineering Exam Help
Electrical Engineering Exam Help
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generators
 
Signals Processing Homework Help
Signals Processing Homework HelpSignals Processing Homework Help
Signals Processing Homework Help
 
Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicos
 
Programming with matlab session 6
Programming with matlab session 6Programming with matlab session 6
Programming with matlab session 6
 
Software Construction Assignment Help
Software Construction Assignment HelpSoftware Construction Assignment Help
Software Construction Assignment Help
 
Mechanical Engineering Assignment Help
Mechanical Engineering Assignment HelpMechanical Engineering Assignment Help
Mechanical Engineering Assignment Help
 
Goldie chapter 4 function
Goldie chapter 4 functionGoldie chapter 4 function
Goldie chapter 4 function
 
Bostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functionsBostock and Chandler chapter3 functions
Bostock and Chandler chapter3 functions
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 
Ch8a
Ch8aCh8a
Ch8a
 
Chemistry Assignment Help
Chemistry Assignment Help Chemistry Assignment Help
Chemistry Assignment Help
 
Ch9c
Ch9cCh9c
Ch9c
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Solution 3.
Solution 3.Solution 3.
Solution 3.
 

Viewers also liked

Trigonometry For Dummies by ItzelS
Trigonometry For Dummies by ItzelSTrigonometry For Dummies by ItzelS
Trigonometry For Dummies by ItzelSdaisyrock
 
Fcv hist zhu
Fcv hist zhuFcv hist zhu
Fcv hist zhuzukun
 
TRIGONOMETRY RATIOS
TRIGONOMETRY RATIOSTRIGONOMETRY RATIOS
TRIGONOMETRY RATIOSRoyB
 
Math & Beauty 2: Waves
Math & Beauty 2: WavesMath & Beauty 2: Waves
Math & Beauty 2: WavesAlbert Wenger
 
Fall protection الحماية من خطر السقوط
Fall protection الحماية من خطر السقوط Fall protection الحماية من خطر السقوط
Fall protection الحماية من خطر السقوط Mohamed Abd Elahleem
 
Confined spaces السلامة المهنية فى الاماكن المغلقة
Confined spaces السلامة المهنية فى الاماكن المغلقة Confined spaces السلامة المهنية فى الاماكن المغلقة
Confined spaces السلامة المهنية فى الاماكن المغلقة Mohamed Abd Elahleem
 
Machine hazards مخاطر المعدات والالات
Machine hazards مخاطر المعدات والالات Machine hazards مخاطر المعدات والالات
Machine hazards مخاطر المعدات والالات Mohamed Abd Elahleem
 
Power and hand tools السلامة المهنية فى استخدام العدد اليدوية
Power and hand tools السلامة المهنية فى استخدام العدد اليدويةPower and hand tools السلامة المهنية فى استخدام العدد اليدوية
Power and hand tools السلامة المهنية فى استخدام العدد اليدويةMohamed Abd Elahleem
 
Welding,cutting and brazing أعمال القطع واللحام
Welding,cutting and brazing أعمال القطع واللحام Welding,cutting and brazing أعمال القطع واللحام
Welding,cutting and brazing أعمال القطع واللحام Mohamed Abd Elahleem
 
Electrical safety السلامة المهنية عند التعامل مع الكهرباء
Electrical safety السلامة المهنية عند التعامل مع الكهرباءElectrical safety السلامة المهنية عند التعامل مع الكهرباء
Electrical safety السلامة المهنية عند التعامل مع الكهرباءMohamed Abd Elahleem
 
PPE مهمات الوقاية الشخصية
PPE مهمات الوقاية الشخصية PPE مهمات الوقاية الشخصية
PPE مهمات الوقاية الشخصية Mohamed Abd Elahleem
 
Introduction of trigonometry
Introduction of trigonometryIntroduction of trigonometry
Introduction of trigonometryPrashant Dwivedi
 
History of trigonometry clasical - animated
History of trigonometry   clasical - animatedHistory of trigonometry   clasical - animated
History of trigonometry clasical - animatedPhillip Murphy Bonaobra
 
Some applications of trigonometry
Some applications of trigonometrySome applications of trigonometry
Some applications of trigonometryDeepak Dalal
 
Real World Application of Trigonometry
Real World Application of TrigonometryReal World Application of Trigonometry
Real World Application of Trigonometryihatetheses
 
Maths ppt on some applications of trignometry
Maths ppt on some applications of trignometryMaths ppt on some applications of trignometry
Maths ppt on some applications of trignometryHarsh Mahajan
 

Viewers also liked (20)

Trig
TrigTrig
Trig
 
Trigonometry For Dummies by ItzelS
Trigonometry For Dummies by ItzelSTrigonometry For Dummies by ItzelS
Trigonometry For Dummies by ItzelS
 
Fcv hist zhu
Fcv hist zhuFcv hist zhu
Fcv hist zhu
 
TRIGONOMETRY RATIOS
TRIGONOMETRY RATIOSTRIGONOMETRY RATIOS
TRIGONOMETRY RATIOS
 
Math & Beauty 2: Waves
Math & Beauty 2: WavesMath & Beauty 2: Waves
Math & Beauty 2: Waves
 
Fall protection الحماية من خطر السقوط
Fall protection الحماية من خطر السقوط Fall protection الحماية من خطر السقوط
Fall protection الحماية من خطر السقوط
 
Confined spaces السلامة المهنية فى الاماكن المغلقة
Confined spaces السلامة المهنية فى الاماكن المغلقة Confined spaces السلامة المهنية فى الاماكن المغلقة
Confined spaces السلامة المهنية فى الاماكن المغلقة
 
العمل بأمان داخل المناطق المحصورة
العمل بأمان داخل المناطق المحصورةالعمل بأمان داخل المناطق المحصورة
العمل بأمان داخل المناطق المحصورة
 
Machine hazards مخاطر المعدات والالات
Machine hazards مخاطر المعدات والالات Machine hazards مخاطر المعدات والالات
Machine hazards مخاطر المعدات والالات
 
Power and hand tools السلامة المهنية فى استخدام العدد اليدوية
Power and hand tools السلامة المهنية فى استخدام العدد اليدويةPower and hand tools السلامة المهنية فى استخدام العدد اليدوية
Power and hand tools السلامة المهنية فى استخدام العدد اليدوية
 
Welding,cutting and brazing أعمال القطع واللحام
Welding,cutting and brazing أعمال القطع واللحام Welding,cutting and brazing أعمال القطع واللحام
Welding,cutting and brazing أعمال القطع واللحام
 
Electrical safety السلامة المهنية عند التعامل مع الكهرباء
Electrical safety السلامة المهنية عند التعامل مع الكهرباءElectrical safety السلامة المهنية عند التعامل مع الكهرباء
Electrical safety السلامة المهنية عند التعامل مع الكهرباء
 
PPE مهمات الوقاية الشخصية
PPE مهمات الوقاية الشخصية PPE مهمات الوقاية الشخصية
PPE مهمات الوقاية الشخصية
 
Introduction of trigonometry
Introduction of trigonometryIntroduction of trigonometry
Introduction of trigonometry
 
History of trigonometry clasical - animated
History of trigonometry   clasical - animatedHistory of trigonometry   clasical - animated
History of trigonometry clasical - animated
 
Some applications of trigonometry
Some applications of trigonometrySome applications of trigonometry
Some applications of trigonometry
 
Real World Application of Trigonometry
Real World Application of TrigonometryReal World Application of Trigonometry
Real World Application of Trigonometry
 
Trigonometry presentation
Trigonometry presentationTrigonometry presentation
Trigonometry presentation
 
Maths ppt on some applications of trignometry
Maths ppt on some applications of trignometryMaths ppt on some applications of trignometry
Maths ppt on some applications of trignometry
 
Mathematics
MathematicsMathematics
Mathematics
 

Similar to C Standard Library Functions Guide

Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocamlpramode_ce
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptWill Livengood
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2bilawalali74
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxagnesdcarey33086
 
Problemas resueltos de funciones lineales ccesa007
Problemas resueltos de  funciones lineales ccesa007Problemas resueltos de  funciones lineales ccesa007
Problemas resueltos de funciones lineales ccesa007Demetrio Ccesa Rayme
 
Introduction to functions
Introduction to functionsIntroduction to functions
Introduction to functionsElkin Guillen
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
Math130 ch09
Math130 ch09Math130 ch09
Math130 ch09Putrace
 

Similar to C Standard Library Functions Guide (20)

Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
Function
Function Function
Function
 
Algorithms DM
Algorithms DMAlgorithms DM
Algorithms DM
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Problemas resueltos de funciones lineales ccesa007
Problemas resueltos de  funciones lineales ccesa007Problemas resueltos de  funciones lineales ccesa007
Problemas resueltos de funciones lineales ccesa007
 
Note introductions of functions
Note introductions of functionsNote introductions of functions
Note introductions of functions
 
Introduction to functions
Introduction to functionsIntroduction to functions
Introduction to functions
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Matlab1
Matlab1Matlab1
Matlab1
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Math130 ch09
Math130 ch09Math130 ch09
Math130 ch09
 

More from teach4uin

Master pages
Master pagesMaster pages
Master pagesteach4uin
 
.Net framework
.Net framework.Net framework
.Net frameworkteach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languagesteach4uin
 
State management
State managementState management
State managementteach4uin
 
security configuration
security configurationsecurity configuration
security configurationteach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tagsteach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tagsteach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentationteach4uin
 
.Net overview
.Net overview.Net overview
.Net overviewteach4uin
 
storage clas
storage classtorage clas
storage clasteach4uin
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrolsteach4uin
 

More from teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
enums
enumsenums
enums
 
memory
memorymemory
memory
 
array
arrayarray
array
 
storage clas
storage classtorage clas
storage clas
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 

Recently uploaded

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 

C Standard Library Functions Guide

  • 1. 1. Standard Library Functions Outline 2. Functions in Mathematics #1 3. Functions in Mathematics #2 4. Functions in Mathematics #3 5. Function Argument 6. Absolute Value Function in C #1 7. Absolute Value Function in C #2 8. Absolute Value Function in C #3 9. A Quick Look at abs 10. Function Call in Programming 11. Math Function vs Programming Function 12. C Standard Library 13. C Standard Library Function Examples 14. Is the Standard Library Enough? 15. Math: Domain & Range #1 16. Math: Domain & Range #2 17. Math: Domain & Range #3 18. Programming: Argument Type 19. Argument Type Mismatch CS1313: Standard Library Functions Lesson CS1313 Spring 2009 1 20. Programming: Return Type 21. More on Function Arguments 22. Function Argument Example Part 1 23. Function Argument Example Part 2 24. Function Argument Example Part 3 25. Using the C Standard Math Library 26. Function Call in Assignment 27. Function Call in printf 28. Function Call as Argument 29. Function Call in Initialization 30. Function Use Example Part 1 31. Function Use Example Part 2 32. Function Use Example Part 3 33. Function Use Example Part 4 34. Evaluation of Functions in Expressions 35. Evaluation Example #1 36. Evaluation Example #2
  • 2. “A relationship between two variables, typically x and y, is called a function, if there is a rule that assigns to each value of x one and only one value of y.” http://www.themathpage.com/aPreCalc/functions.htm So, for example, if we have a function f(x) = x + 1 then we know that … f(-2.5) = -2.5 + 1 = -1.5 f(-2) = -2 + 1 = -1 f(-1) = -1 + 1 = 0 f(0) = 0 + 1 = +1 f(+1) = +1 + 1 = +2 f(+2) = +2 + 1 = +3 f(+2.5) = +2.5 + 1 = +3.5 … CS1313: Standard Library Functions Lesson CS1313 Spring 2009 2
  • 3. For example, if we have a function f(x) = x + 1 then we know that … f(-2.5) = -2.5 + 1 = -1.5 f(-2) = -2 + 1 = -1 f(-1) = -1 + 1 = 0 f(0) = 0 + 1 = +1 f(+1) = +1 + 1 = +2 f(+2) = +2 + 1 = +3 f(+2.5) = +2.5 + 1 = +3.5 … CS1313: Standard Library Functions Lesson CS1313 Spring 2009 3
  • 4. Likewise, if we have a function a(y) = | y | then we know that … a(-2.5) = | -2.5 | = +2.5 a(-2) = | -2 | = +2 a(-1) = | -1 | = +1 a(0) = | 0 | = 0 a(+1) = | +1 | = +1 a(+2) = | +2 | = +2 a(+2.5) = | +2.5 | = +2.5 … CS1313: Standard Library Functions Lesson CS1313 Spring 2009 4
  • 5. f(x) = x + 1 a(y) = | y | We refer to the thing inside the parentheses immediately after the name of the function as the argument (also known as the parameter) of the function. In the examples above:  the argument of the function named f is x;  the argument of the function named a is y. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 5
  • 6. In my_number.c, we saw this:... else if (abs(users_number – computers_number) <= close_distance) { printf("Close, but no cigar.n"); } /* if (abs(...) <= close_distance) */... So, what does abs do? 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) that we just looked at. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 6
  • 7. … fabs(-2.5) returns 2.5 abs(-2) returns 2 abs(-1) returns 1 abs(0) returns 0 abs(1) returns 1 abs(2) returns 2 fabs(2.5) returns 2.5 … CS1313: Standard Library Functions Lesson CS1313 Spring 2009 7
  • 8. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 8 We say “abs of -2 evaluates to 2” or “abs of -2 returns 2.” Note that the function named abs calculates the absolute value of an int argument, and fabs calculates the absolute value of a float argument.
  • 9. % cat abstest.c #include <stdio.h> int main () { /* main */ const int program_success_code = 0; printf("fabs(-2.5) = %fn", fabs(-2.5)); printf(" abs(-2) = %dn", abs(-2)); printf(" abs(-1) = %dn", abs(-1)); printf(" abs( 0) = %dn", abs( 0)); printf(" abs( 1) = %dn", abs( 1)); printf(" abs( 2) = %dn", abs( 2)); printf("fabs( 2.5) = %fn", fabs( 2.5)); return program_success_code; } /* main */ % gcc -o abstest abstest.c % abstest fabs(-2.5) = 2.500000 abs(-2) = 2 abs(-1) = 1 abs( 0) = 0 abs( 1) = 1 abs( 2) = 2 fabs( 2.5) = 2.500000 CS1313: Standard Library Functions Lesson CS1313 Spring 2009 9
  • 10. Jargon: In programming, the use of a function in an expression is referred to as an invocation, or more colloquially as a call. We say that the statement printf("%dn", 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. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 10
  • 11. 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”). More on this later. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 11
  • 12. 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. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 12
  • 13. Function Name Math Name Value Example abs(x) absolute value |x| abs(-1) returns 1 sqrt(x) square root x0.5 sqrt(2.0) returns 1.414… exp(x) exponential ex exp(1.0) returns 2.718… log(x) natural logarithm ln x log(2.718…) returns 1.0 log10(x) common logarithm log x log10(100.0) returns 2.0 sin(x) sine sin x sin(3.14…) returns 0.0 cos(x) cosine cos x cos(3.14…) returns -1.0 tan(x) tangent tan x tan(3.14…) returns 0.0 ceil(x) ceiling ┌ x ┐ ceil(2.5) returns 3.0 floor(x) floor └ x ┘ floor(2.5) returns 2.0 CS1313: Standard Library Functions Lesson CS1313 Spring 2009 13
  • 14. It turns out that the set of C Standard Library functions is grossly insufficient for most real world tasks, so in C, and in most programming languages, there are ways for programmers to develop their own user-defined functions. We’ll learn more about user-defined functions in a future lesson. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 14
  • 15. In mathematics:  The domain of a function is the set of numbers that can be used for the argument(s) of that function.  The range is the set of numbers that can be the result of that function. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 15
  • 16. For example, in the case of the function f(x) = x + 1 we define the domain of the function f to be the set of real numbers (sometimes denoted R), which means that the x in f(x) can be any real number. Similarly, we define the range of the function f to be the set of real numbers, because for every real number y there is some real number x such that f(x) = y. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 16
  • 17. On the other hand, for a function q(x) = 1 / (x − 1) the domain cannot include 1, because q(1) = 1 / (1 – 1) = 1 / 0 which is undefined. So the domain might be R − {1} (the set of all real numbers except 1). In that case, the range of q would be R – {0} (the set of all real numbers except 0), because there’s no real number y such that 1/y is 0. (Note: if you’ve taken calculus, you’ve seen that, as y gets arbitrarily large, 1/y approaches 0 as a limit – but “gets arbitrarily large” is not a real number, and neither is “approaches 0 as a limit.”) CS1313: Standard Library Functions Lesson CS1313 Spring 2009 17
  • 18. Programming has concepts that are analogous to the mathematical domain and range: argument type and return type. For a given function in C, the argument type – which corresponds to the domain in mathematics – is the data type that C expects for an argument of that function. For example:  the argument type of abs is int;  the argument type of fabs is float. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 18
  • 19. An argument type mismatch is when you pass an argument of a particular data type to a function that expects a different data type. Some implementations of C WON’T check for you whether the data type of the argument you pass is correct. If you pass the wrong data type, you can get a bogus answer. This problem is more likely to come up when you pass a float where the function expects an int. In the reverse case, typically C simply promotes the int to a float. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 19
  • 20. Just as the programming concept of argument type is analogous to the mathematical concept of domain, so too the programming concept of return type is analogous to the mathematical concept of range. The return type of a C function – which corresponds to the range in mathematics – is the data type of the value that the function returns. The return value is guaranteed to have that data type, and the compiler gets upset – or you get a bogus result – if you use the return value inappropriately. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 20
  • 21. In mathematics, a function argument can be:  a number: f(5) = 5 + 1 = 6  a variable: f(z) = z + 1  an arithmetic expression: f(5 + 7) = (5 + 7) + 1 = 12 + 1 = 13  another function: f(a(w)) = |w| + 1  any combination of these; i.e., any general expression whose value is in the domain of the function: f(3a(5w + 7)) = 3 (|5w + 7|) + 1 Likewise, in C the argument of a function can be any non-empty expression that evaluates to an appropriate data type, including an expression containing a function call. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 21
  • 22. #include <stdio.h> #include <math.h> int main () { /* main */ const float pi = 3.1415926; const int program_success_code = 0; float angle_in_radians; printf("cos(%10.7f) = %10.7fn", 1.5707963, cos(1.5707963)); printf("cos(%10.7f) = %10.7fn", pi, cos(pi)); printf("Enter an angle in radians:n"); scanf("%f", &angle_in_radians); printf("cos(%10.7f) = %10.7fn", angle_in_radians, cos(angle_in_radians)); printf("fabs(cos(%10.7f)) = %10.7fn", angle_in_radians, fabs(cos(angle_in_radians))); CS1313: Standard Library Functions Lesson CS1313 Spring 2009 22
  • 23. printf("cos(fabs(%10.7f)) = %10.7fn", angle_in_radians, cos(fabs(angle_in_radians))); printf("fabs(cos(2.0 * %10.7f)) = %10.7fn", angle_in_radians, fabs(cos(2.0 * angle_in_radians))); printf("fabs(2.0 * cos(%10.7f)) = %10.7fn", angle_in_radians, fabs(2.0 * cos(angle_in_radians))); printf("fabs(2.0 * "); printf("cos(1.0 / 5.0 * %10.7f)) = %10.7fn", angle_in_radians, fabs(2.0 * cos(1.0 / 5.0 * angle_in_radians))); return program_success_code; } /* main */ CS1313: Standard Library Functions Lesson CS1313 Spring 2009 23
  • 24. % gcc -o funcargs funcargs.c -lm % funcargs cos( 1.5707963) = 0.0000000 cos( 3.1415925) = -1.0000000 Enter an angle in radians: -3.1415925 cos(-3.1415925) = -1.0000000 fabs(cos(-3.1415925)) = 1.0000000 cos(fabs(-3.1415925)) = -1.0000000 fabs(cos(2.0 * -3.1415925)) = 1.0000000 fabs(2.0 * cos(-3.1415925)) = 2.0000000 fabs(2.0 * cos(1.0 / 5.0 * -3.1415925)) = 1.6180340 CS1313: Standard Library Functions Lesson CS1313 Spring 2009 24
  • 25. If you’re going to use functions like cos that are from the part of the C standard library that has to do with math, then you need to do two things: 1. In your source code, immediately below the #include <stdio.h> you must also put #include <math.h> 2. When you compile, you must append -lm to the end of your compile command: gcc -o funcargs funcargs.c –lm (Note that this is hyphen ell em, NOT hyphen one em.) CS1313: Standard Library Functions Lesson CS1313 Spring 2009 25
  • 26. Function calls are used in expressions in exactly the same ways that variables and constants are used. For example, a function call can be used on the right side of an assignment or initialization: float theta = 3.1415926 / 4.0; float cos_theta; … cos_theta = cos(theta); length_of_c_for_any_triangle = a * a + b * b – 2 * a * b * cos(theta); CS1313: Standard Library Functions Lesson CS1313 Spring 2009 26
  • 27. A function call can also be used in an expression in a printf statement: printf("%fn", 2.0); printf("%fn", pow(cos(theta), 2.0)); CS1313: Standard Library Functions Lesson CS1313 Spring 2009 27
  • 28. Since any expression can be used as some function’s argument, a function call can also be used as an argument to another function: const float pi = 3.1415926; printf("%fn", 1 + cos(asin(sqrt(2.0)/2.0) + pi)); CS1313: Standard Library Functions Lesson CS1313 Spring 2009 28
  • 29. Most function calls can be used in initialization, as long as its arguments are literal constants: float cos_theta = cos(3.1415926); This is true both in variable initialization and in named constant initialization: const float cos_pi = cos(3.1415926); CS1313: Standard Library Functions Lesson CS1313 Spring 2009 29
  • 30. #include <stdio.h> #include <math.h> int main () { /* main */ const float pi = 3.1415926; const float cos_pi = cos(3.1415926); const float sin_pi = sin(pi); const int program_success_code = 0; float phi = 3.1415926 / 4.0; float cos_phi = cos(phi); float theta, sin_theta; CS1313: Standard Library Functions Lesson CS1313 Spring 2009 30
  • 31. theta = 3.0 * pi / 4; sin_theta = sin(theta); printf("2.0 = %fn", 2.0); printf("pi = %fn", pi); printf("theta = %fn", theta); printf("cos(pi) = %fn", cos(pi)); printf("cos_pi = %fn", cos_pi); printf("sin(pi) = %fn", sin(pi)); printf("sin_pi = %fn", sin_pi); printf("sin(theta) = %fn", sin(theta)); printf("sin_theta = %fn", sin_theta); printf("sin(theta)^(1.0/3.0) = %fn", pow(sin(theta), (1.0/3.0))); CS1313: Standard Library Functions Lesson CS1313 Spring 2009 31
  • 32. printf("1 + sin(acos(1.0)) = %fn", 1 + sin(acos(1.0))); printf("sin(acos(1.0)) = %fn", sin(acos(1.0))); printf("sqrt(2.0) = %fn", sqrt(2.0)); printf("sqrt(2.0) / 2 = %fn", sqrt(2.0) / 2); printf("acos(sqrt(2.0)/2.0) = %fn", acos(sqrt(2.0)/2.0)); printf("sin(acos(sqrt(2.0)/2.0)) = %fn", sin(acos(sqrt(2.0)/2.0))); return program_success_code; } /* main */ CS1313: Standard Library Functions Lesson CS1313 Spring 2009 32
  • 33. % gcc -o funcuse funcuse.c -lm % funcuse 2.0 = 2.000000 pi = 3.141593 theta = 2.356194 cos(pi) = -1.000000 cos_pi = -1.000000 sin(pi) = 0.000000 sin_pi = 0.000000 sin(theta) = 0.707107 sin_theta = 0.707107 sin(theta)^(1.0/3.0) = 0.890899 1 + sin(acos(1.0)) = 1.000000 sin(acos(1.0)) = 0.000000 sqrt(2.0) = 1.414214 sqrt(2.0) / 2 = 0.707107 acos(sqrt(2.0)/2.0) = 0.785398 sin(acos(sqrt(2.0)/2.0)) = 0.707107 CS1313: Standard Library Functions Lesson CS1313 Spring 2009 33
  • 34. When a function call appears in an expression – for example, on the right hand side of an assignment statement – the function is evaluated just before its value is needed, in accordance with the rules of precedence order. CS1313: Standard Library Functions Lesson CS1313 Spring 2009 34
  • 35. For example, suppose that x and y are float variables, and that y has already been assigned the value -10.0. Consider this assignment statement: x = 1 + 2.0 * 8.0 + fabs(y) / 4.0; CS1313: Standard Library Functions Lesson CS1313 Spring 2009 35
  • 36. x = 1 + 2.0 * 8.0 + fabs(y) / 4.0; x = 1 + 16.0 + fabs(y) / 4.0; x = 1 + 16.0 + fabs(-10.0) / 4.0; x = 1 + 16.0 + 10.0 / 4.0; x = 1 + 16.0 + 2.5; x = 1.0 + 16.0 + 2.5; x = 17.0 + 2.5; x = 19.5; CS1313: Standard Library Functions Lesson CS1313 Spring 2009 36