Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).  A program is needed to prompt.

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING Task 1 LEVEL 6 PROGRAMMING: Be able to use a text based language like Python and JavaScript & correctly use procedures and functions.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Programming Logic and Design Fourth Edition, Introductory
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
CS 201 Functions Debzani Deb.
Introduction to Python
Guide To UNIX Using Linux Third Edition
Main task -write me a program
Introduction to Methods
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Python quick start guide
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
Control Structures FOR Statement Looping.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Python Libraries Importing and Calling functions.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Python – Procedures 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.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
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.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
David Stotts Computer Science Department UNC Chapel Hill.
Program #2 Cell Phone Usage ….Let’s start with understanding the problem!
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
I Power Higher Computing Software Development Development Languages and Environments.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
The Software Development Process
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.
Top-down approach / Stepwise Refinement & Procedures & Functions.
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.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Keywords: Range Trace table Testing List Index Activities: To create a trace table for a small piece of code using a set of data. Create a trace table.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Getting Started With Python Brendan Routledge
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
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.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Functions and Procedures
Functions CIS 40 – Introduction to Programming in Python
CSC113: Computer Programming (Theory = 03, Lab = 01)
Explain what touch develop is to your students:
User-Defined Functions
Functions, Procedures, and Abstraction
Functions and Procedures
Coding Concepts (Sub- Programs)
PROGRAMMING Sub Procedures I.
Coding Concepts (Basics)
Task 1 Computer Programming LEVEL 6 PROGRAMMING:
Lesson 06: Functions Class Chat: Attendance: Participation
Functions Christopher Harrison | Content Developer
Chapter 6: User-Defined Functions I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
CPS125.
Functions, Procedures, and Abstraction
Using Modules.
Presentation transcript:

Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).  A program is needed to prompt a user for their name and monthly wage. From this it should then calculate their yearly salary and yearly pension deductions (8% of their yearly salary). This summary should be written to file and displayed on screen.

Python – Functions and Procedures Procedures 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 of code again later, you can use a procedure.

Python - Procedures A Simple Procedure In this example I want to print the squares of the numbers from 1 to 20: #This is a procedure called output #It will print a given number and its square def output(number): print(number,"squared =",number*number) #This is the start of the main program number = 1 while number < 21: output(number) number = number + 1

Python - Procedures Here the top two lines are a procedure called output. The procedure expects to be given one variable (which it calls number) and will happily do its thing when told to. The main program is underneath, and the line that says output(x) will run the output procedure and give it the variable x to work with.

Python - Procedures Multiple Parameters A parameter is the variable (or variables) that get passed to the procedure. You can require more than one parameter: def output(number1,number2): print(number1,"+",number2,"=",number1+number2) for counter in range(20): number = counter**2 output(counter,number) You can pass as many parameters as you like to a procedure as long as you set it up correctly.

Python - Procedures Optional Parameters Once you have defined a procedure, you can call it in a number of ways: def output(grade,score=50,feedback="Well done!"): print("You scored",score,"which is a grade",grade,feedback) output("C") output("A",87) output("E",12,"Rubbish!") You can pass as many (or few) of the parameters as you like - as long as they’re in the right order. This means you have to plan the procedure quite carefully.

Python - Functions Functions So far our procedures have just printed an answer to the console. Sometimes a procedure will have to send some data back. This is called a return. Procedures that return something are called functions. In Python that isn’t a huge distinction, but in some programming languages it is very important. def calculate(number): newnumber = number / 100 return (newnumber) for counter in range(5,101,5): answer = calculate(counter) print(counter,"% = ",answer)

Python - Functions Functions The line return(newnumber) passes the value of the newnumber variable back to the main program. The line y = caluclate(x) shows that the main program expects a value to be returned and has somewhere to store it.

Python - Functions Modules There are some procedures already built in to Python (print, for example). Modules (sometimes known as libraries in other languages) are collections of extra procedures and functions that are pre-written. Try this program first. It won’t work, but I’m trying to prove a point (sqrt is short for square root): number = 49 answer = sqrt(49) print(answer) Now try this one: import math number = 49 answer = math.sqrt(49) print(answer) You can find a list of modules at modindex.html

Python - Functions Global Variables. When you want to use a variable as a global in a function – just declare it as follows def mult(x,y): global total total = x*y total = 0 mult(7,5) print(total)