Presentation is loading. Please wait.

Presentation is loading. Please wait.

OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions.

Similar presentations


Presentation on theme: "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions."— Presentation transcript:

1 OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions

2 OCR Computing GCSE © Hodder Education 2013 Slide 2 Python 11: Writing your own functions You should always write your programs as a series of functions. This makes it easier to construct a big project and also makes it easier to read. A function: is a package of code that returns one result; is called from another part of a program; returns control to the main program when it is finished; should do just one job. A function that you write yourself will be used in exactly the same way as a built-in function.

3 OCR Computing GCSE © Hodder Education 2013 Slide 3 Python 11: Writing your own functions Defining a function A function is defined by using the ‘def’ instruction. For example: def menu(): Notice the brackets for accepting arguments (all functions have these) and the finishing colon. If we want the function to process data that we pass to it, we put these arguments in the brackets, e.g. def cube(number): A function can start with a ‘docstring’. This is a triple quoted string that lets you specify or comment what the function is for. It must be the first line of the function. You don’t have to use one, but it is good practice. You can write a multiline docstring between the opening and closing triple quotes.

4 OCR Computing GCSE © Hodder Education 2013 Slide 4 Python 11: Writing your own functions Here is a function to cube a number:

5 OCR Computing GCSE © Hodder Education 2013 Slide 5 Python 11: Writing your own functions Here is the program to write a few items to a file, written as a function: def write_file(): '''make a file handle and associate it with the actual text file''' #open file for writing ('w') register_file=open('registers.txt','w') #write a few items to file register_file.write('program counter\n') register_file.write('memory address register\n') register_file.write('memory data register\n') register_file.write('instruction register\n') #use the close method to close the file register_file.close() #reassure the user print('File is now written') Notice that, as usual, ‘def’ is followed by a colon and everything that is to be part of the function is indented.

6 OCR Computing GCSE © Hodder Education 2013 Slide 6 Python 11: Writing your own functions All the file handling programs from Section 10 are put together in one large program, which is available as a resource. Each section is made into a function. A menu is produced to allow the user to choose which part of the program to use.

7 OCR Computing GCSE © Hodder Education 2013 Slide 7 Python 11: Writing your own functions Here is the menu function. Notice that the function ends, as all function definitions do, by unindenting. #menu def menu(): print('The register files\n') print('What do you want to do?\n\n') print('Start a new file\n') print('Add new data\n') print('Read the file\n') print('Output one line of the file\n') print('Find record\n') print('Quit') while True: answer=(input('Press S, A, R, O or Q: ')) if answer in('S', 's'): write_file() if answer in('A', 'a'): append_file() elif answer in ('R', 'r'): read_file() elif answer in ('O', 'o'): read_one_line() elif answer in ('Q', 'q'): break else: print('Invalid response') #now call the menu function menu()


Download ppt "OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 11: Writing your own functions."

Similar presentations


Ads by Google