Programming - ICT Teacher

Download Report

Transcript Programming - ICT Teacher

In

PROGRAMMING

Lesson 3

RECAP

 Complete the starter activity

OBJECTIVES

 Understand the benefits of a function  Be able to write and call a function  Understand how and why we should use comments in our code O UTCOMES  All-Level 4  Most-Level 5  Some-Level 6 To be able to write a function - with help be able to add comments to your code Be able to write a these with little help . Independently write these and adapt them and complete one of the extension tasks.

RECAP - VARIABLES

 Write the statements that ask the user for their name and a number and display them using this line of code print(user + " your number is " + number) Hint: Use the input() function.

RECAP-INPUT

 Did you get something like… print("Enter your name") user=input() print ("Enter your number") number=input() print(user + " your number is " + number)

FUNCTIONS

 In the context of programming, a function is a named sequence of statements that performs a desired operation.  You have already used several functions, who can name one?

input print int randint  This operation is specified in a function definition. In Python, the syntax for a function definition is:

CALLING A FUNCTION

 Functions allow us to split our coding problem into smaller chunks – which make it easier to understand  They can be reused – the print function for example  Allows many people to work on the same project at once  Allows a project to be tested as each part is completed.

 Lets make our function The process for this is File , New. In this new window you will need to create a function.

OUT FIRST FUNCTION def main ():

The syntax of a function. Main is the name of the function that you call in your program.

print ("Hello") main()

In your program – this is how you call the function called main.

Lets go through this.

TASK 1 – NAME FUNCTION

 Write a Function that will ask your name, and print it out.  You have 5 minutes to complete this task.

 Extension: Expand it by getting the user to enter a number that you will multiply by your age, add the day of the month and display it.

ANSWER

Update your self assessment in Lesson 3 section – Task 1 – Name function.

COMMENTS

 Commenting is

very very very

important in coding.  It describes what is going on  The idea is for someone who has never seen the code to be able to read it, understand it and change it  It acts as a reminder, when you go back and look at your code.

#

COMMENTS - STANDARD

 Every 2 lines of code NEEDS a comment

TASK 2 – NUMBERS FUNCTION

Write a program that will ask the user for four integer numbers and then add these numbers together before displaying the answer Remember I am looking for comments in your work.

ANSWER

Update your self assessment in Lesson 3, Task 2 – Numbers function.

PLENARY

FUN TIME

 Try the fractal creating python programs.

 In your lesson 3 folder – under fractals.