Python programming working with data Session 1 Subroutines

Python programming: working with data Session 1 – Subroutines Welcome to the session. Please use the text chat to communicate in this session (speech bubble icon on mobile app). If you cannot hear the facilitator, please let us know in the text chat. Please keep your microphone muted at all times, unless invited to speak by the facilitator. Please do not share any personal data or confidential information in this session. 02/06/20 | Gareth Griffiths National Centre for Computing Education

Intended learning outcomes By the end of this session you will: • • • understand the benefits of using subprograms understand the concept of passing data into and out of subprograms (procedures, functions) understand the need for, and how to use, global and local variables when implementing subprograms By the end of this session and the participant led session you will: • be able to write code that uses user-written and pre-existing (built- in, library) subprograms 02/06/20 | Gareth Griffiths National Centre for Computing Education

How to access code on this course? • to make the content accessible for all participants, all code files are available to access via the online platform Repl. it • this online platform provides you with an editor and console – just like other pieces of software i. e. IDLE • you can access the code files using the tiny. cc links provided on the slides and handouts • all the links are live 24/7 so can be accessed at home too. 02/06/20 | Gareth Griffiths National Centre for Computing Education

Teaching method: PRIMM Predict Run Investigate Modify Make • the PRIMM method is an approach to planning programming lessons and activities • introducing the skills in stages before writing a program from scratch. 02/06/20 | Gareth Griffiths National Centre for Computing Education

Predict - Arithmetic Functions Predict what you think the code will do when run. Run the code to check your predictions. Activity 1 tiny. cc/j 10 jnz Why does this line of code result in an error? 02/06/20 | Gareth Griffiths National Centre for Computing Education

Predict - Arithmetic Functions Predict what you think the code will do when run. Run the code to check your predictions. Why does this line of code result in an error? 02/06/20 | Gareth Griffiths National Centre for Computing Education Activity 1 tiny. cc/j 10 jnz

Predict - Arithmetic Functions Predict what you think the code will do when run. Run the code to check your predictions. Activity 1 tiny. cc/j 10 jnz This is the corrected code 02/06/20 | Gareth Griffiths National Centre for Computing Education

Investigating - String Functions Investigate what each of the following code snippets do. 02/06/20 | Gareth Griffiths National Centre for Computing Education Activity 2 tiny. cc/e 80 jnz

Investigating - String Functions Investigate what each of the following code snippets do. 02/06/20 | Gareth Griffiths National Centre for Computing Education Activity 2 tiny. cc/e 80 jnz

What are subroutines? • a set of instructions pre-written to perform a frequent task • a subroutine can be a function or a procedure Function Procedure Subroutine that returns a value. Set of commands that are executed in order. Doesn’t give feedback. 02/06/20 | Gareth Griffiths National Centre for Computing Education

Example subroutines tiny. cc/bd 1 jnz What is the difference? 02/06/20 | Gareth Griffiths National Centre for Computing Education

Example subroutines tiny. cc/bd 1 jnz Procedure Function 02/06/20 | Gareth Griffiths National Centre for Computing Education

Global & Local Variables A Global Variable is generally declared at the beginning of a program and can be accessed throughout the whole program global_variable = 1 A Local Variable is declared within a subroutine and is only available within that block of code def my. Subroutine: global_variable local_variable = 1 02/06/20 | Gareth Griffiths National Centre for Computing Education

Global & Local Variables Activity 3 tiny. cc/or 1 jnz Complete the following modifications to the code below: • add a command that adds another number to run. Count • add a print statement that displays the variable 'name' 02/06/20 | Gareth Griffiths National Centre for Computing Education

Example solution 02/06/20 | Gareth Griffiths National Centre for Computing Education Activity 3 tiny. cc/o 71 jnz

Run / Investigate Use text chat to discuss any errors that are presented when running the code. 02/06/20 | Gareth Griffiths National Centre for Computing Education

User Defined Functions A function is a piece of code that performs a specific task. It bundles up the code, gives it a name, and can be used any time by ‘calling’ it. 02/06/20 | Gareth Griffiths National Centre for Computing Education

User Defined Functions Naming the function def Keyword that marks the start of a function. A function name is used to uniquely identify it. 02/06/20 | Gareth Griffiths National Centre for Computing Education

User Defined Functions Naming the function Parameter List Parameters (arguments) are optional. Parameters are used to pass values into the function. Return value Argument List 02/06/20 | Gareth Griffiths National Centre for Computing Education

User Defined Functions Naming the function Parameter List Colon (: ) marks the end of the function header Operational Code Return value Calling a function 02/06/20 | Gareth Griffiths National Centre for Computing Education Argument List

Intended learning outcomes By the end of this session you will: • • • understand the benefits of using subprograms understand the concept of passing data into and out of subprograms (procedures, functions) understand the need for, and how to use, global and local variables when implementing subprograms By the end of this session and the asynchronous session you will: • be able to write code that uses user-written and pre-existing (builtin, library) subprograms 02/06/20 | Gareth Griffiths National Centre for Computing Education
- Slides: 21