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.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Programming for GCSE Topic 9.1: Logic Circuits T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary.
Programming for GCSE Topic 10.2: Designing for File I/O T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
Programming for GCSE Topic 3.1: If Statements in Python T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
Chapter 6: User-Defined Functions I
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Functions and subroutines – Computer and Programming.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Programming for GCSE Topic 6.2: Testing T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
David Stotts Computer Science Department UNC Chapel Hill.
Programming for GCSE Topic 4.2: Faults and Debugging T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Introduction to Computer Programming
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Programming for GCSE Topic 9.2: Circuits for Adding T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
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.
CS0004: Introduction to Programming
Chapter 6: User-Defined Functions I
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Functions Jay Summet.
User-Defined Functions
Teaching London Computing
Functions, Procedures, and Abstraction
Reviewing key concepts
Functions Christopher Harrison | Content Developer
Functions Jay Summet.
Topics Introduction to Functions Defining and Calling a Function
Objectives In this chapter, you will:
COMPUTER PROGRAMMING SKILLS
Functions Jay Summet.
Introduction to Computer Science
A Level Computer Science Topic 6: Introducing OOP
CISC101 Reminders Assignment 3 due today.
Functions, Procedures, and Abstraction
 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.
Presentation transcript:

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 of London

Outline Functions What's the big idea? Syntax Variables in functions: scope Name clashes Functions that make changes Global variables

What's a Function? A part of a program with A name Parameters Result this function does a calculation Result Inputs

BIG IDEA This I S A BIG I DEA Building blocks of a program Big programs cannot be made in one piece Use 'blocks' from another programmer (library) Naming parts of a program Name the function  behaviour

S IMPLE F UNCTION E XAMPLE Defining and calling a function

Definition of a Function A function is a NOT a complete program def double(num): result = num * 2 return result Key wordName Key word Parameter

Calling a Function – I Call the function def double(num): result = num * 2 return result anum = int(input("A number:")) anum = double(anum) print("Now doubled twice:", anum) Function call

Calling a Function – II Call the function def double(num): result = num * 2 return result anum = int(input("A number:")) anum = double(double(anum)) print("Now doubled twice:", anum) Function call

Program Order Write the functions first One function can call another (providing it is defined first) Do not put one function inside another The 'main' program calls the functions Function def Main program Initialise variable Call functions

Words, Words … You define (or declare) a function A function has parameters You call a function You pass a value to a function … it returns a result The function creates a new scope Functions are also called Procedures Subroutines Methods … and more

Example Create a function that is passed a name and prints the string "Hello XXXX" Choose a suitable name Change the function to capitalise the name Choose a new name

Example Solution def greetMe(name): print("Hello", name) def greetMeLoudly(name): print("Hello", name.upper())

V ARIABLES IN F UNCTIONS The idea of 'scope'

Variable Scope Function create a 'box' Variable 'result' is a 'local' variable It only exists inside the box 'num' can be used like a variable It is given a value in the call def double(num): result = num * 2 return result

Scope: Simple Version The variables used inside a function are totally separate from other variables Appear when function is called Disappear afterwards Name clash: confusing variables inside and outside a function Use different names

F UNCTIONS THAT M AKE C HANGES Some more complex and less essential ideas

What is the Effect of a Function? No effect Return a value Nothing changes! Effect Print something File output too Change value of a variable outside the function How is this possible? this function does a calculation Result Inputs Global variables

Global Variables Local: inside a function Global: outside a function Variable inside (local) and outside (global) not totally separate def double() : global num num = num * 2 num = 10 double() print(num) Key word

Using a List as a Parameter When a list is used as a parameter, you can change it def addZero(mylist): mylist.append(0) herList = [1,2,3] addZero(herList) print(herList) >>> [1, 2, 3, 0] >>>

Parameters and Assignment There is a close parallel between Parameter passing is like assignment def myFunction(param) :... statements num = 10 myFunction(num) num = 10 param = num... statements

S YLLABUS AND T EACHING I SSUES

Syllabus – Functions Writing functions is AS not GCSE (OCR) … but lots of related ideas So why learn functions? Using functions e.g. 'len' Planning solutions: breaking down a problem in parts … some students will teach themselves

Summary Programming is problem solving Problems are solved in steps Functions are for step-by-step programming Defining functions is not essential for GCSE Using them is!