﻿

# Functions and Procedures

Functions and procedures and one of the key ideas of developing larger programs. Some programming languages have different structures and commands for functions and procedures, but in Python they're created in the same way and the only difference is that you don't return a value from a procedure. If you missed the first assignment, click here for instructions on how to install Python. If you are unable to install Python then you can use replit.com to run your programs on-line.

## Commands

Here is a reminder of the commands you might need for this assignment.

• def function_name() - defines a function or procedure. function_name must be unique within the program, and can't be the name of an existing Python function or procedure (e.g. len or print). You can include one or more arguments inside the brackets and use its value inside the function or procedure, e.g. defining a function as follows and calling double(3) will return 6.
```def double(x):
return x * 2
```
• return - sends back a value from a function (it is not needed for a procedure); this can be a fixed value or the result of a calculation.
• you can call a procedure just using its name like a command - e.g. if you define a procedure using def hello(): at the top of the program, you run it by adding the command hello() to the main body of your program.
• because a function returns a value, you need to use the name of the function in conjunction with another function or structure - i.e. you need to print the result, store it in a variable, or make a decision based on it. For example, with the double() function above you can do things like print(double(3)), x = double(3) and if double(x) > 10.

If you would prefer a longer explanation, I have written a blog called Creating a function - odd or even?

You will already be familiar with built-in functions and procedures such as len() and print(), but today you're going to create your own (you can also do the same thing in Excel if you learn a bit of BASIC). Your program should:

1. create a function that tests a number for a particular property, e.g. whether it is:
1. odd or even
2. square
3. prime
4. a palindrome (or has repeated digits)

Anything that you're comfortable with, really!

2. display the numbers from 0 to 100, and the result of the test in step 1 (i.e. whether the number is odd/even, square, prime, etc.)

If you can think of a more interesting use for a function, then feel free to program that and submit it instead - just remember to use the Comments box to tell me what it does.