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.

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
CS 201 Functions Debzani Deb.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
CHAPTER 8 OPERATORS AND EXPRESSIONS IN C++. OPERATORS The operations (specific task) are represented by operators and the objects of the operation(s)
Unit 3: Java Data Types Math class and String class.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
COMP102 Lab 091 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Week 2 - Wednesday.  What did we talk about last time?  C compilation model  Lab 1.
OPERATORS.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
The Math Class Methods Utilizing the Important Math Operations of Java!
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
Operators and Expressions. Introduction C operators can be classified into a number of categories 1.Arithmetic(+, -, *, /, %) 2.Relational (, >=, ==,
Functions Venkatesh Ramamoorthy 21-March Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout
Operators and Expressions
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Variables, Operators, and Expressions
Chapter 4 Mathematical Functions, Characters, and Strings
Mathematical Functions
Type casting Algorithm & flowchart
BIL 104E Introduction to Scientific and Engineering Computing
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 3 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Conversions of the type of the value of an expression
Chapter 4: Mathematical Functions, Characters, and Strings
Using Free Functions Chapter 3 Computing Fundamentals with C++
The Language Package.
Functions October 23, 2017.
Functions.
Chapter 5 Methods.
Chapter 6: User-Defined Functions I
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Module 14 Miscellaneous Topics
Chapter 4 Methods Introducing Methods Declaring Methods
Functions in C Math Library Functions Functions Function Definitions
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

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 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.

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.)

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

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

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)

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.

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.

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.

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.

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.

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.

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

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