More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Introduction to Computing Science and Programming I
Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Chapter 2 Writing Simple Programs
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Functions.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Functions Chapter 4 Python for Informatics: Exploring Information
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Functions CMSC 201 – Lab 5. Overview Objectives for today's lab:  Practice breaking down a program into multiple functions  Practice writing function.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Python Programming Module 3 Functions Python Programming, 2/e1.
More on Functions (Part 2)
Python Programming Module 3 Functions Python Programming, 2/e.
Introduction to Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python - Functions.
Writing Functions( ) (Part 5)
Writing Functions( ) (Part 5)
Call Stacks, Arguments and Returns
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Functions, Procedures, and Abstraction
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More on Functions (Part 2)
More on Functions (Part 2)
Python for Informatics: Exploring Information
Types, Truth, and Expressions (Part 2)
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
CHAPTER 5: Control Flow Tools (if statement)
Introduction to Computer Science
Types, Truth, and Expressions
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
CISC101 Reminders Assignment 3 due today.
Functions, Procedures, and Abstraction
Types, Truth, and Expressions (Part 2)
Presentation transcript:

More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg

Functions From mathematics we know that functions perform some operation and return one value. They “encapsulate” the performance of some particular operation, so it can be used by others (for example, the sqrt() function).

Code Listing 6.1 # Temperature conversion def celsius2fahrenheit(celsius): """ Convert Celsius to Fahrenheit.""" return celsius*

Triple Quoted String in Function A triple quoted string just after the def is called a docstring docstring is documentation of the function’s purpose, to be used by other tools to tell the user what the function is used for.

How to Write a Function Does one thing. If it does too many things, it should be broken down into multiple functions (refactored). Readable. How often should we say this? If you write it, it should be readable. Reusable. If it does one thing well, then when a similar situation (in another program) occurs, use it there as well.

More on Functions Complete. A function should check for all the cases where it might be invoked. Check for potential errors. Not too long. Kind of synonymous with “does one thing”. Use it as a measure of doing too much.

Operation def celsius2Fahrenheit (Temp): return temp* F = celsius2Fahrenheit(C) 1. Call copies argument C to parameter Temp 2. Control transfers to function “celsius2Farenheit”

Operation (con’t) 3. Expression in celsius2Farenheit is evaluated 4. Value of expression is returned to the invoker F = celsius2Fahrenheit(C) def celsius2Fahrenheit (Temp): return Temp*

Two Parts to a Function Definition – creates the function Invocation – is the application of a function within a program A function must be defined before it is invoked 10

Function Definition

Function Definition Example def fahrenheit2Celsius(fahrTemp): if type(fahrTemp)==int: celsius = (fahrTemp - 32)*5/9 return celsius else: return None 12

Function Invocation Example In your program (after the function definition), we can invoke/call the function fahrenheit2Celsius by: originalTemp=90 convertedTemp = fahrenheit2Celsius(originalTemp) print(originalTemp,”in Celsius is”,convertedTemp) 13

Return Statement The return statement indicates the value that is returned by the function. The statement is optional (the function can return nothing). If no return, the function is often called a procedure.

What exactly is return doing? When python comes to a function inside your code… convertedTemp = fahrenheit2Celsius(originalTemp) …it runs the function, then substitutes the return value where the function stood convertedTemp =

Returning None None is a special value in Python that represents nothing  The first letter of None must be capitalized – it will turn orange Use it when you have nothing to return  Like if one of the parameters was invalid (Take a look at the fahrenheit2Celsius function again…) 16

Multiple Returns in a Function A function can have multiple return statements. Remember, the first return statement executed ends the function. #doing function stuff return result print(“Hello!”) #This line will never happen

Multiple Returns in a Function When you use if/elif/else statements, be sure to return a value for every branch! if result < 0: return None elif result == 1: return 1 else: return 42 #the answer to everything else

Looking at Lab09

How to Write a Function Does one thing. If it does too many things, it should be broken down into multiple functions (refactored). Readable. How often should we say this? If you write it, it should be readable. Reusable. If it does one thing well, then when a similar situation (in another program) occurs, use it there as well.

More on Functions Complete. A function should check for all the cases where it might be invoked. Check for potential errors. Not too long. Kind of synonymous with “does one thing”. Use it as a measure of doing too much.

Procedures Functions that have no return statements are often called procedures. Procedures are used to perform some duty (print output, store a file, etc.) Remember, return is not required.

Example def printCNBRules(): print("Please select from one of the following choices:") print(" Enter c for cowboy") print(" Enter n for ninja") print(" Enter b for bear") print(" Enter q to quit") print() #run program printCNBRules() 23

Functions can also call themselves One function can invoke another function Let’s take a look at functionFun.py 24