def-ining a function A function as an execution control structure

Slides:



Advertisements
Similar presentations
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Advertisements

Introduction to Computing Using Python Imperative Programming  Python Programs  Interactive Input/Output  One-Way and Two-Way if Statements  for Loops.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Lists Introduction to Computing Science and Programming I.
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
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.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
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.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Course A201: Introduction to Programming 11/04/2010.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python 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.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Functions Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Python Functions : chapter 3
INLS 560 – F UNCTIONS Instructor: Jason Carter.
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)
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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Introduction to Programming
Topic: Functions – Part 1
Introduction to Programming
Introduction to Python
COMPSCI 107 Computer Science Fundamentals
CS 1110 Introduction to Programming Spring 2017
CS 100: Roadmap to Computing
Chapter 3 Instructor: Zhuojun Duan
Topic: Functions – Part 2
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Functions.
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Imperative Programming
Teaching London Computing
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Introduction to Programming
5. Functions Rocky K. C. Chang 30 October 2018
Python Functions Part I Presented by : Sharmin Abdullah
Topics Introduction to Functions Defining and Calling a Void Function
G. Pullaiah College of Engineering and Technology
Nate Brunelle Today: Functions
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
General Computer Science for Engineers CISC 106 Lecture 03
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Introduction to Programming
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 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
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.
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
 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.
Imperative Programming
Functions John R. Woodward.
© Sangeeta M Chauhan, Gwalior
Using Modules.
Functions and Abstraction
Presentation transcript:

def-ining a function A function as an execution control structure Introduction to Computing Using Python def-ining a function A function as an execution control structure def-ining a new function Passing and using function parameters return-ing a value from a function

Function calls and execution control Introduction to Computing Using Python A function call is another kind of statement that alters the order in which statements in a program are executed A function call creates a temporary ‘excursion,’ causing the sequence of execution to jump to the named function. When the function exits, execution returns to the point where the function call occurred. We've already seen these built-in function calls: len(), type(), min(), max(), str(), bool(), ... Today we are going to learn how to write our own functions how to specify what parameters are passed to our functions how to return something from a function

Defining a new function Introduction to Computing Using Python We have seen these calls to built-in functions: abs(), max(), len(), sum(), print() >>> abs(-9) 9 >>> max(2, 4) 4 >>> lst = [2,3,4,5] >>> len(lst) >>> sum(lst) 14 >>> print() >>> def iSquaredPlus10(x): result = x**2 + 10 return result >>> f(1) 11 >>> f(3) 19 >>> f(0) 10 You can define a new function using def def: function definition keyword iSquaredPlus10: name of function x: variable name for input argument def iSquaredPlus10(x): result = x**2 + 10 return result return: specifies the value that the function returns (default: None)

print() versus return Introduction to Computing Using Python return returns execution to the point where the function is called. The returned value becomes the value of the function call. def iSquaredPlus10 (x): result = x**2 + 10 print(result) def iSquaredPlus10(x): result = x**2 + 10 return result squarePlusTen = iSquaredPlus10(5) print(squarePlusTen) The value calculated by the function is returned, assigned to a variable, and then printed to the screen. The function prints the value of result to the screen. Because the function does not explicitly return a value, the default value None is returned.

Defining a new function Introduction to Computing Using Python The format of a function definition is a def <function name> (0 or more parameter names>): <indented body of the function> b Let’s write a function areaOfRectangle() that: Takes two numbers as parameters (side lengths a and b of a rectangle ) Returns the are of the rectangle def areaOfRectangle(a, b): area = a*b return area >>> areaOfRectangle(3,4) 12.0 >>>

Exercise Write function hello that: Introduction to Computing Using Python Exercise Write function hello that: takes a name (i.e., a string) as input prints a personalized welcome message (Note: because hello does not explicitly return anything, the default value None is returned >>> hello('Julie') Welcome, Julie, to the world of Python. >>> print(hello('Julie')) >>> None def hello(name): line = 'Welcome, ' + name + ', to the world of Python.' print(line)

Exercise Write function oddCount() that: Introduction to Computing Using Python Exercise Write function oddCount() that: takes a list of numbers as input returns the number of odd numbers in the list here is an example of input and output for oddCount Hint: you are going to use both a for statement and an if statment >>> numList = [4, 0, 1, -2] >>> oddCount(numList) 1 >>>

Exercise Write function oddCount() that: Introduction to Computing Using Python Exercise Write function oddCount() that: takes a list of numbers as input returns the number of odd numbers in the list def oddCount(aNumList): result = 0 for i in aNumList: if i%2 == 1: result += 1 return result >>> numList = [4, 0, 1, -2] >>> oddCount(numList) 1 >>>

Commenting a function A program should be documented so that: Introduction to Computing Using Python A program should be documented so that: the program’s ‘mission’ is explained the developer who writes/maintains the code understands it Python has built-in support for documenting each function with a docstring. The docstring should appear immediately after the def line have an imperative style ('Do this!') make clear the function input and output Example docstring def iSquaredPlus10(i): '''return i squared plus 10''' result = i**2 + 10 return result