Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Types and Arithmetic Operators
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Structured programming
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Topic 10 return values, Math methods Based on slides bu Marty Stepp and Stuart Reges from " Thinking like a computer.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Java Basics (continued)
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Python Libraries Importing and Calling functions.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
The Math Class Methods Utilizing the Important Math Operations of Java!
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Java Basics (continued) Ms. Pack AP Computer Science A.
Python Programming Module 1 Computers and Programs Python Programming, 2/e1.
Fundamentals of Programming I Overview of Programming
Lesson 06: Functions Class Participation: Class Chat:
Math class Random() method Floor method Top-level parseInt function
Exam #1 You will have exactly 30 Mins to complete the exam.
Software Development I/O and Numbers
CMPT 120 Topic: Python Modules.
Building Java Programs
Building Java Programs
CS 100: Roadmap to Computing
Methods Chapter 6.
Building Java Programs
Building Java Programs
For loop, definite vs indefinite iteration, range, random
Introduction to Programming
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Building Java Programs
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Java's Math class Method name Description Math.abs(value)
Lesson 06: Functions Class Chat: Attendance: Participation
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Building Java Programs
Introduction to Value-Returning Functions: Generating Random Numbers
Terminal-Based Programs
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Using Modules.
Presentation transcript:

Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1

Modules Python small language with few command – True of most modern languages (efficiency) Can import additional capabilities as modules Syntax: import modulename Can get list of all modules with help(“modules”) – Just those included with Python download – Many others available at

Example: Math Module Contains basic math functions – Square root – Trig functions – Logarithms… Can get complete description of any module using help(“modulename”)

Functions and Modules Modules contain functions Syntax: modulename.functionname(…) Example: square root sqrt function x = math.sqrt(2) a = 3 b = 5 * math.sqrt(a + 1)

Evaluating Expressions with Functions Functions take arguments as input – Values inside () after function name Functions (may) return a value – What function evaluates to in expression Steps in evaluating expression with functions on RHS: 1.Evaluate expressions that are function arguments 2.Evaluate function using those arguments 3.Use return value in place of function in expression 4.Evaluate rest of expression

Evaluating Nested Functions Functions can use other functions as arguments – Evaluate inner function first – Use return value in argument of outer function Example: x = math.cos(math.radians(45)) – math.radians(45) returns – Evaluates to math.cos( ) Example: input value and convert to number in single statement x = float(input(“Enter a number: “))

Functions with List of Arguments Example: print function Example: math.pow(value1, value2) – Same as value1 ** value2 Note that order of parameters matters – math.pow(2, 3)  8 – math.pow(3, 2)  9

Constants Numbers (and other things) with fixed value Often built into modules – Syntax: modulename.constant Examples in math module: – math.pi  evaluates to … – math.e area = math.pi * radius ** 2

Random Numbers Many functions in random module – random.random() Returns random float between 0 and 1 – random.randint(lower, upper) Returns random int between lower and upper (inclusive) Example: dice1 = random.randint(1, 6) dice2 = random.randint(1, 6) print(“You rolled a”, dice1, “and a”, dice2)