Raspberry Pi Week 8 – Day 2 (Python Function Parameters)
Function parameters in python are values that are passed to functions and these parameters are used within the function. For example if you are writing a simple calculator program that adds two numbers then you will need to pass two numbers so that the function can add the numbers. AddNumbers(10, 10) The result would be 20. Take a look at the example below. Firstly we will write a program that displays a message, adds two numbers and displays the result. Download simpleAddition.py #a simple program that adds two numbers and displays the result num1 = 10 num2 = 10 result = 0
result = num1 + num2 print(“The result is “, result) A quick run down of the program is in order. As always a comment is needed so that the reader of the program knows what it does. #a simple program that adds two numbers and displays the result
Next 2 variables are created, num1 and num2. These variables both hold the number 10 and a result variable is used and is assigned at value of 0.
num1 = 10 num2 = 10 result = 0 The num1 and num2 variables are added together and the result is stored in the result variable.
result = num1 + num2
Finally the print function is used to display the result.
(‘The result is ‘, 20) The next step will be to use our own functions to create the same result but with much cleaner program code. Before we do this it might be worth revisiting the print function which is the subject on the next days lesson. Download the PDF file for Week 8 – Day 2 – Python Function Parameters
Day-2-Python-Function-Parameters (1).pdf
(62.48 KB, 下载次数: 1)
|