Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 3 Function Basics Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
CSC 200 Lecture 04 2/8/06 Matt Kayala. Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Lecture 3 Using C++ Functions COSC1567 C++ Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Slide 1 Chapter 3 Function Basics. Slide 2 Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
CMSC 1041 Functions II Functions that return a value.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
C++ Programming Lecture 10 Functions – Part II
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall.
Random numbers in C++ Nobody knows what’s next....
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Functions.
CSE202: Lecture 13The Ohio State University1 Function Scope.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Introduction to programming in java Lecture 16 Random.
Chapter 9: Value-Returning Functions
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr.
Why they aren't really random
TMF1414 Introduction to Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Function Basics TBC=15 Dr. Xiao Qin.
Iterative Constructs Review
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
توابع در C++ قسمت اول اصول كامپيوتر 1.
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Functions October 23, 2017.
Iterative Constructs Review
CS150 Introduction to Computer Science 1
CS 144 Advanced C++ Programming January 31 Class Meeting
6 Functions.
Introduction to Functions
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Today’s Lecture Predefined Functions

Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer defined

Predefined Functions  Predefined in the libraries  Example:  Calculate the square root of a number  double sqrt(double) is defined in library  Three parts:  Return type  Function name  Argument list

The Function Call  To use:  Must "#include" the library that defines the function  Provide required arguments  Example: double root; root = sqrt (9.0);  The argument in a function call (9.0) can be a literal, a variable, or an expression

A Larger Example: Display 3.1 A Predefined Function That Returns a Value (1 of 2)

A Larger Example: Display 3.1 A Predefined Function That Returns a Value (2 of 2)

Predefined Functions  Libraries full of functions for our use!  Two types:  Those that return a value  Those that do not (void)  Must "#include" appropriate library  e.g., , (Original "C" libraries)  (for cout, cin)

More Predefined Functions  #include  abs()// Returns absolute value of an int  pow(x, y)  Returns x to the power y  Notice this function receives two arguments  A function can have any number of arguments, of varying data types

Even More Math Functions: Display 3.2 Some Predefined Functions (1 of 2)

Even More Math Functions: Display 3.2 Some Predefined Functions (2 of 2)

Predefined Void Functions  No returned value  Performs an action, but sends no "answer"  All aspects same as functions that "return a value"  They just don’t return a value!  Example  exit(int)

Random Number Generator  Return "randomly chosen" number  Used for simulations, games  rand()  Takes no arguments  Returns value between 0 & RAND_MAX  Scaling  Squeezes random number into smaller range rand() % 6  Returns random value between 0 & 5  Shifting rand() %  Shifts range between 1 & 6 (e.g., die roll)

Random Examples  Random double between 0.0 & 1.0: rand()/(double)(RAND_MAX)  Type cast used to force double-precision division  Random int between 1 & 6: rand() %  "%" is modulus operator (remainder)  Random int between 10 & 20: rand() %

Pseudorandom Numbers  The function rand() takes no arguments and returns a integer in the rage of [0, RAND_MAX]  Numbers appears to be random, but really not.  It is called pseudorandom numbers  The sequence of the random is determined by seed

Pseudorandom Numbers  If you start rand with the same seed, you will produce the same sequence random number  To get true random number, use function srand to reset seed. void srand(int)

Character Functions  Include bool isdigit(char) bool isalpha(cha) bool isspace(char) bool islower(char) bool isupper(char) int tolower(char) int toupper(char)