PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
Functions Jay Summet. Aug Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)
Chapter 6: User-Defined Functions I
1 Functions Samuel Marateck © A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
Functions.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
Python Functions : chapter 3
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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!
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Topic: Functions – Part 1
Chapter 6: User-Defined Functions I
Introduction to Programming
Web Design II Flat Rock Community Schools
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Functions Jay Summet.
FIGURE 4-10 Function Return Statements
Functions CIS 40 – Introduction to Programming in Python
Topics Introduction to Repetition Structures
Writing Functions( ) (Part 5)
FIGURE 4-10 Function Return Statements
Procedures Programming Guides.
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
CSC 113: Computer programming II
Function Notation “f of x” Input = x Output = f(x) = y.
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Functions Jay Summet.
G. Pullaiah College of Engineering and Technology
Nate Brunelle Today: Functions
CHAPTER 6: Control Flow Tools (for and while loops)
Test Automation For Web-Based Applications
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Functions Jay Summet.
Introduction to Computer Science
CSE 231 Lab 4.
Python While Loops.
Input and Output Python3 Beginner #3.
Python Functions.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
def-ining a function A function as an execution control structure
Problem to solve Given four numbers, days, hours, minutes, and seconds, compute the total number of seconds that is equivalent to them. For example, 0.
 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.
Functions Jay Summet.
Functions John R. Woodward.
Presentation transcript:

PYTHON FUNCTIONS

What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)

Function Definition def function_name(input1, input2, …): #code in function body Define functions at the top of your file AFTER imports Function definitions can have 0 or more input parameters Example: def sayHello3(): print(“Hello!”) print(“Hey!”) print(“Hi!”)

More Examples def f(x): print(2 * x) def slope(x1, y1, x2, y2): m = (y2 – y1) / (x2 – x1) print(m) def turnRight(myRobot): myRobot.turnLeft()

Function Terminology def slope(x1, y1, x2, y2): m = (y2 – y1) / (x2 – x1) print(m) function name input parameters function definition function body function header

Function Calls After defining a function, you can use it by making a function call Function call  name of function (input parameters) Do not forget that function calls have parentheses! Input parameters can also be variables slope(2, 5, 4, 9)#function call

What happens during a function call? Parameters in the function call are copied/passed into the input parameter variables in the function header When the function body executes, the parameter variables have the data from the function call def slope(x1, y1, x2, y2): m = (y2 – y1) / (x2 – x1) print(m) slope(2, 5, 4, 9)#function call

What happens after a function call? When the function body is done running, the computer goes back to where the function call was made Code before function call Code after function call Function call Function body Function definition

What about returning data? Example: def f(x): print(2 * x) sum = f(1) + f(2) + f(3) #DOES NOT WORK! Our functions need to return values def f(x): return 2 * x #function returns the #data back to the call

Return Examples def slope(x1, y1, x2, y2): m = (y2 – y1) / (x2 – x1) return m def triangle_area(base, height) return 0.5 * base * height print(slope(6, 3, 1, 8)) print(triangle_area(3, 5)) #the function calls return data, which are #then printed by print()