PYTHON DICTIONARY
CommentsData typesVariablesIf ElseIf Elif ElseFunctionsFor loopWhile loopData structuresString slicing/indexingUser defined functionLocal/Global variableValidation and TryMethodsFile I/OPseudocodeModules |
What is a subprogram?
Python challenge - User defined procedureClick here to see the lyrics of the worlds most annoying song.
Now imagine if you had to write some code to display all the lyrics. Programmers are a bit lazy and love to copy and paste as much as possible to work quicker. In this annoying song, the chorus is sung 6 times. If you were writing some code to display the lyrics, you would have to display the chorus 6 times! There is a quicker way and it is to use a "user defined function". The example below uses a user defined function to hold the chorus "def chorus():"and whenever we want the chorus to be displayed we "call" it using the code "chorus()".
Try to code this. Remember to "indent" all the content inside the "def chorus():" Do NOT type out the lyrics, copy and paste them from here. Just add the code as you see it above.
Python challenge - User defined procedure - with input()Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Here we can see a user defined function called "happyBirthday" and it is being passed an argument (keyword that means data) called "person". "Person" is the name of a variable that gets is value from a user via the "input()" function.
Python challenge - User defined procedure - with input()Below is a poor joke. Can you create some code that allows a user to enter their name instead of "Dave"? Use the example above to help you. Goodluck!
"Do you want to hear a really good Batman impression?" asked My mate Dave. "Go on then," I replied. "NOT THE KRYPTONITE!" he screamed. I said, "That's Superman." He said, "Thanks, I've been practising." User defined function and procedureEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
A function has a return statement/value. A procedure does not.
So, "def add_then_return()" is a function "def main()" is a procedure A function has a return statement/value. A procedure does not
User defined function - with return statement
Enter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Try the code with and without the return value (return result). What does "return result" do?
User defined function - return statement/value explainedEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Return statement/value usedFunction = a subprogram with a return statement/value
Procedure = a subprogram without a return statement/value Notice how "main()" gets the other subprograms to do something. For example, line 10 calls the subprogram called getFirstName() on line 1 which asks for a name and returns the name back to line 10 into variable called "FirstName". User defined procedure - The bubble sortEnter the following code in a Python code editor. Before running the code, what do you think it will do? Press "F5" then "Enter" to run
Parameters vs arguementsParameters explained
When you make a user defined function or a user defined procedure, you put brackets at the end and then a colon. If the brackets are empty then your code does not have any parameters, e.g.: def DisplayYes(): print("yes") In the code below, the brackets have two parameters, itemWidth and itemheight. A parameter is a variable name inside the brackets of a function "header" that receive a value. Arguments explained Argument is the name given to the data that is passed to the function in the call command. In the code below, the call command is on line 5 (yes, the call command exists in a variable called itemArea, but dont let this confuse you ). The call calArea() has two arguments in it, 2 and 5. These two arguments are passed to the parameters in calcArea function that is on line 1. Arguments are passed to parameters Subprogram for menuLots of software use a menu system to allow the user to choose what they want to do. The code below contains 4 subprograms/user defined functions. The main one displays the options available then branches the program to another subprogram to execute the chosen option.
Look out for the while loop which keeps re-displaying the options. Also look out for the how the loop is controlled with the sentry variable on line 11 which is changed on line 22 to exit the loop. |