You can’t spell Functions in Computer Science without fun…
09 May 2017
There is lots of talk in the media currently about problem solving and how important it is as a skill to instil in students, especially in Computer Science. That got me thinking about KS4 students and what makes problem solving easier for them in Python.
Once students have become reasonably comfortable with the basics of programming (the techniques in the GCSE specification for example, 2.2 page 10) then they can start to solve problems that involve combining the different techniques to do something tangible.
Thinking functionally
I have seen the way in which students combine collections of techniques cause some degree of confusion, especially when the problem involves performing a number of different activities at once. To make this easier it is good practice to introduce functional programming as early as possible. I have tried this approach with year 8 students and it made the process of decomposing and solving the problem much simpler as they could put a flowchart or some pseudo-code into a “mini program” that functioned independently and logically did one thing. This technique made the troubleshooting process simpler and made piecing together the different parts of the puzzle easier. The one catch with this technique was that the syntax and process of functions had to be taught up front and the concepts of function scope and parameter passing were vital in the understanding.
Luckily in Python most students will have used functions from very early on using the print() function and so inadvertently already will be familiar with the need for the brackets and an argument. Students may well have also come across other Built In Functions (BIFs) such as input(), len() and help(). Click the link to access the full list:
https://docs.python.org/3/library/functions.html.
Understanding functions
Initially it is important to get across the concept of what a function is:
- A self-contained piece of code with a specific function or purpose
- Callable by other programs and modules to avoid repetition of code
- Can be passed data through arguments
- Can return results to its caller.
Next it needs to be made clear why we should use functions:
- Functions “divide and conquer” a complex problem into smaller chunks
- Functions should do one thing well so they can be used by you and other programmers easily
- Functions can be reused in different parts of a system to avoid duplication which mean changes only happen in one place
- Small functions are easier to understand and maintain.
It is a good idea to go through the process of creating a simple function and demonstrating how they work, especially using arguments and the scope of variables in a function. It’s also important to make clear the difference between a function and a procedure and the need for a return statement.
def func1(x):
y=x+1
return y print (func1(2))
| In this example you can draw attention to the declaration of the function and the prescribed argument and don’t forget the colon! The logic is very straightforward but attention should also be drawn to the return of y and how its scope is limited to within the function. The next line also needs dissecting as it has the function call with its own argument as the argument for the print function! This can be simplified onto two lines if needed.
|
Let the fun(ctions) begin
Once your students have the hang of these concepts you can get them to look at the code using this web resource: https://inventwithpython.com/chapter6.html and ask them to explain what is happening by drawing a flowchart to explain the process (the answer is on the web-page by the way).
The process of explaining the links between the functions and the logic used is illuminating and uncovers any misunderstandings. This can be extended by getting them to extend the game to involve more choices and of course more functions!
This could be preceded and differentiated by students writing their own functions for things like:
- A lottery number generator.
- A function that checks if a number is odd or even.
- A function that encrypts a message.
- There are loads of examples to be found here: https://projecteuler.net/archives
- Any previous programme they have made can be converted into a function!
Getting students to think “functionally” from the very beginning makes problem solving simpler and easier to carry out. I have seen some excellent practice in schools using scaffolding to support weaker students and weaning them off the support slowly whilst letting more able students get their hands dirty.
Using our coding challenges booklet candidates can practise solving problems in code until they are experts and these skills can be extended and applied in any real world context to overcome any real world problem.
Please comment below and share your tried and tested techniques using functions to solve computer science problems, you can also follow us on Twitter @OCR_ICT or email us at computerscience@ocr.org.uk if you have any questions.
About the author
Rob Leeman - Subject Specialist - Computer Science
Following on from his work on the Computing A Level, Rob is now assigned to lead the development for GCSE Computer Science qualification. His experience began in IT support before embarking on a career in teaching and assessment having taught and managed A Level IT, and prior to joining OCR in 2013 Rob held the post of Head of ICT and Computer Science at a secondary school in Cambridgeshire. In his spare time Rob loves to exercise his dogs and indulge in CrossFit, airsoft, diving, clay pigeon shooting and is a keen proponent of Open Source.