Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.

Similar presentations


Presentation on theme: "Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function."— Presentation transcript:

1 Topic 2A – Library Functions and Casting

2 CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function is called, it runs to completion. Control then returns to the calling function. Functions can be written by the programmer, or provided as part of the C toolkit. Functions take data as input, perform some task on, or with, that data, and provide data as output.

3 CISC 105 – Topic 2A Library Functions Library functions are functions that are implemented as part of the C toolkit. These functions are commonly used and are provided for the purpose of reuse; they do not have to be written over again by the programmer. (Don’t reinvent the wheel.) As library functions have been used by many programmers, they have already been tested. (Don’t build a broken wheel when you already have a working one.)

4 CISC 105 – Topic 2A Some Common Mathematical Library Functions pow(double x, double y) – returns x y sqrt(double x) – returns the square root of x log10(double x) – returns the base 10 logarithm of x log(double x) – returns the natural logarithm of x exp(double x) – returns e x

5 CISC 105 – Topic 2A Some Common Mathematical Library Functions ceil(double x) – returns the smallest integer not less than x floor(double x) – returns the largest integer not greater than x fabs(double x) – returns the absolute value of x abs(int x) – returns the absolute value of x

6 CISC 105 – Topic 2A Some Common Mathematical Library Functions sin(double x) – returns the sine of x (x is in radians) cos(double x) – returns the cosine of x (x is in radians) tan(double x) – returns the tangent of x (x is in radians)

7 CISC 105 – Topic 2A Return Types Each of the previous library functions returns the data type that is passed into it. Most of the previous functions take a double parameter and thus return a double value. abs(x) takes an integer argument and thus returns an integer.

8 CISC 105 – Topic 2A Casting Although each of the functions above take (and return) double data types, they can be used with float data types. This is done with a cast. A cast is the conversion of one data type to another data type. This can be done explicitly or implicitly.

9 CISC 105 – Topic 2A Implicit Casting When evaluating mixed type expressions, an implicit cast is performed, as mixed type expression cannot be directly evaluated. For instance, in the code fragment: float x = 1.0, y; int z = 2; y = x + z; The variable z is casted to a float (2.0) in order to add it to the float-type x.

10 CISC 105 – Topic 2A Implicit Casting Implicit casting is also performed when making function calls. If a function expects one data type and a different data type is provided, the provided data type is casted to the expected data type. In the previous library calls, if a float data type is given as an argument, it is first casted to a double data type and then passed to the function.

11 CISC 105 – Topic 2A Implicit Casting The same is true with the return type. The library functions return a double data type. However, if the result is assigned to a float, as in: float x = 4.0, y; y = sqrt(x); sqrt(x) evaluates to a double data type and is then assigned to a float type variable.

12 CISC 105 – Topic 2A Implicit Casting Therefore, the double type result is first cast to a float type and then assigned to the y variable. When casting “down”, i.e. from a double to a float, or a float to an int, any part of the value that “does not fit” into the new data type is simply dropped. The value is NOT rounded.

13 CISC 105 – Topic 2A Review What is the result of the following code fragment: int w, z, q; float x = 4.0, y; y = sqrt(x); w = y; z = x – w; q = sqrt(3); printf(“y=%f,w=%d,z=%d,q=%d”,y,w,z,q); y=2.0, w=2, z=2, q=1

14 CISC 105 – Topic 2A Explicit Casting Casting can also be performed explicitly. This is done with the following form. What is the result of the following code fragment? int a=9, b=4; float c,d; c = a/b; d = (float)a / (float)b; printf(“c=%.2f,d=%.2f”,c,d); c=2.00, d=2.25


Download ppt "Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function."

Similar presentations


Ads by Google