OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions.

Slides:



Advertisements
Similar presentations
Python - Selection Starter
Advertisements

This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Python Basic Syntax. Basic Syntax - First Program 1 All python files will have extension.py put the following source code in a test.py file. print "Hello,
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 12: A few other things.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
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, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
CPS120: Introduction to Computer Science Functions.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
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.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
1 CS161 Introduction to Computer Science Topic #9.
Python - Selection Selection means selecting (or choosing) what to do next. Should I cycle to school, or ask for a lift? If it’s a sunny day I might cycle.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
OCR GCSE Computing © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 1: Introduction.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 4: Writing programs.
Files Tutor: You will need ….
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Let’s get started!.
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
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.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
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.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
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.
More on Functions (Part 2)
Introduction to Programming
Python Let’s get started!.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1110 Introduction to Programming Spring 2017
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Functions CIS 40 – Introduction to Programming in Python
CompSci 230 Software Construction
Teaching London Computing
File Handling Programming Guides.
An Introduction to Python
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
COMPUTER PROGRAMMING PYTHON
Introduction to Programming
More on Functions (Part 2)
CISC101 Reminders All assignments are now posted.
The structure of programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
COMPUTER PROGRAMMING SKILLS
The structure of programming
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.
Thinking procedurally
L L Line CSE 420 Computer Games Lecture #5 Control Structures.
Challenge Guide Grade Code Type Slides
Functions and Abstraction
Presentation transcript:

OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions

OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 11: Writing your own functions You should always write your programs as a series of functions. This makes it easier to construct a big project and also makes it easier to read. A function: is a package of code that returns one result; is called from another part of a program; returns control to the main program when it is finished; should do just one job. A function that you write yourself will be used in exactly the same way as a built-in function.

OCR Computing GCSE © Hodder Education 2013 Slide 3 Python 11: Writing your own functions Defining a function A function is defined by using the ‘def’ instruction. For example: def menu(): Notice the brackets for accepting arguments (all functions have these) and the finishing colon. If we want the function to process data that we pass to it, we put these arguments in the brackets, e.g. def cube(number): A function can start with a ‘docstring’. This is a triple quoted string that lets you specify or comment what the function is for. It must be the first line of the function. You don’t have to use one, but it is good practice. You can write a multiline docstring between the opening and closing triple quotes.

OCR Computing GCSE © Hodder Education 2013 Slide 4 Python 11: Writing your own functions Here is a function to cube a number:

OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 11: Writing your own functions Here is the program to write a few items to a file, written as a function: def write_file(): '''make a file handle and associate it with the actual text file''' #open file for writing ('w') register_file=open('registers.txt','w') #write a few items to file register_file.write('program counter\n') register_file.write('memory address register\n') register_file.write('memory data register\n') register_file.write('instruction register\n') #use the close method to close the file register_file.close() #reassure the user print('File is now written') Notice that, as usual, ‘def’ is followed by a colon and everything that is to be part of the function is indented.

OCR Computing GCSE © Hodder Education 2013 Slide 6 Python 11: Writing your own functions All the file handling programs from Section 10 are put together in one large program, which is available as a resource. Each section is made into a function. A menu is produced to allow the user to choose which part of the program to use.

OCR Computing GCSE © Hodder Education 2013 Slide 7 Python 11: Writing your own functions Here is the menu function. Notice that the function ends, as all function definitions do, by unindenting. #menu def menu(): print('The register files\n') print('What do you want to do?\n\n') print('Start a new file\n') print('Add new data\n') print('Read the file\n') print('Output one line of the file\n') print('Find record\n') print('Quit') while True: answer=(input('Press S, A, R, O or Q: ')) if answer in('S', 's'): write_file() if answer in('A', 'a'): append_file() elif answer in ('R', 'r'): read_file() elif answer in ('O', 'o'): read_one_line() elif answer in ('Q', 'q'): break else: print('Invalid response') #now call the menu function menu()