Teaching London Computing

Slides:



Advertisements
Similar presentations
Procedures and Functions. What are they? They are both blocks of code that can be reused to perform specific task. However there is a difference: Function-
Advertisements

A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
Chapter 2 Writing Simple Programs
Main task -write me a program
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.
Python.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Programming for GCSE 1.0 Beginning with Python T eaching L ondon C omputing Margaret Derrington KCL Easter 2014.
If the same piece of code needs to be used several times we can use a loop – but only if those times are all together. If you need to run the same bit.
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.
Starter – Its the small things in life What’s wrong with this code Height = 10 Width = 10 A = Height * Width Print A Remember to check: Spelling Spacing.
Programming for GCSE Topic 8.1: Functions T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
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… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
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.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Introduction to Programming
GCSE COMPUTER SCIENCE Practical Programming using Python
Python Review 1.
Whatcha doin'? Aims: To start using Python. To understand loops.
COMPUTATIONAL CONSTRUCTS
Topic: Iterative Statements – Part 1 -> for loop
Introducing Instructions
Introduction to Python
Functions and Procedures
Functions CIS 40 – Introduction to Programming in Python
Functions.
Engineering Innovation Center
Computer Science and an introduction to Pascal
Learning to Program in Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Functions and Procedures
Coding Concepts (Sub- Programs)
Teaching London Computing
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Topic 1: Problem Solving
Java Programming Function Introduction
Coding Concepts (Basics)
Margaret Derrington KCL Easter 2014
Faculty of Computer Science & Information System
G. Pullaiah College of Engineering and Technology
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
CISC101 Reminders All assignments are now posted.
Introduction to Computer Science
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Introduction to Python
Java Programming Function Introduction
CISC101 Reminders Assignment 3 due today.
def-ining a function A function as an execution control structure
CPS125.
 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.
Presentation transcript:

Teaching London Computing FUNCTIONS in Python Margaret Derrington KCL Easter 2014

Let’s start with some revision… What will these instructions do? print (“Hello World”) print (2+3) input (“2+3 = “) int(input (“type a number “)) len(“Margaret”) hex(42) bin(15) If you type them into python shell, what will happen? Check with your neighbour that you agree

These are actually all FUNCTIONS print (“Hello World”) print (2+3) input (“2+3 = “) int(input (“type a number “)) len(“Margaret”) hex(42) bin(15) print, input, len, input, hex, bin …. …………..Are all FUNCTIONS What do they have in common…? besides being PINK!

Functions We CALL a function It has a name, and brackets PASS PARAMETER RETURN We CALL a function It has a name, and brackets and inside the brackets are PARAMETERS Parameters are PASSED to a function The function does something with those parameters, and returns an output or result

procedures, subroutines, BUILT – IN FUNCTIONS Python has lots of build in functions We have already learnt and used several of them But today we are going to learn to CREATE OUR OWN NEW FUNCTIONS We will DEFINE functions by giving them a NAME, saying what the parameters will be And what the result will be when we CALL the function and pass it the necessary parameters Also called procedures, subroutines, methods Also called DECLARING

Which is now one more than it was We can call a function and use the result afterwards or do one function inside another Defining a function Which is now one more than it was def AddOne (number): number = number+1 return number answer= AddOne(7) print (answer) print (AddOne(4)) def repeat (text): return text*2 print (repeat(“hello”)) print (repeat(“2”)) print (repeat (2)) What happens? What happens?

Defining a function def NameOfFunction (parameters) : Don’t forget the colon def NameOfFunction (parameters) : explain what happens and how you work out the return RESULT <~~~stop indenting at end of definition INDENT Parameters are like variables; names that can have lots of different values The syntax is important and must be correct

More than one parameter A function may have more than one parameter…. def AreaOfRectangle (length, width) return length*width This function with multiply the length by the width to give the area of a rectangle It has TWO parameters.

Exercise Create some simple functions to add three to a number To triple a number To get a user input name and print it in capital letters To get a username and print a string “Hello username” Remember if the function does not include printing the result, you will have to do that in a separate function…. print. Define a new function ShoutName which uses these functions to make a function which gets a user’s name and shouts (in capital letters) “HELLO USERNAME!!!” Functions aren’t actually programs, but a program which defines and uses functions is much easier to follow.

SCOPE (of a variable) GLOBAL and LOCAL are used to describe variables which exist throughout a program (global) or just within a small part of it (local)… like inside a function These are important concepts, because they can cause a lot of confusion if they get mixed up def doubler (number): answer = number*2 return answer Here answer is a local variable, it only exists and has a meaning inside the function It will cause confusion if answer is used elsewhere in the program with a different meaning.

GCSE Syllabus USE of functions is required Creating functions is on AS But creating them helps you undertsand how to use them And it also helps to break problems into separate parts that can get reused. And to use functions to fix those separate parts