Mind map
creating functions (AFEC course 4)
Map content
creating functions (AFEC course 4)
some functions (print(), name=input(), move forward(), text (), rect(), size(), fill(),textSize) are built into Python. Others are from the processing library. Functions can also be created.
- to create a function, it is defined using 'def' (eg. def draw).
- code below the def function() is indented, and it gives the function instructions. For example, with a function def say_hello(), below that, the instructions could be print("Hello!"). This code does not mean the function will be executed: to actually execute the function, seperately in the code, type say_hello (the part after the 'def')
- to create a function, it is defined using 'def' (eg. def draw).
with the fill function, that has three numbers, each number represents the red, green and blue components of a colour
parameters of a function are the values used within the function brackets (like with 'rect', the x and y coordinates, and the width and height, and for 'fill', the RGB code). Parameters must be inside the brackets area.
- variables can be used as parameters (like in an instance where the parameters are not fixed values), eg. def output_user_details(user_name, given_name, age)
- an error will be caused if parameters are outside the function
- variables can be used as parameters (like in an instance where the parameters are not fixed values), eg. def output_user_details(user_name, given_name, age)
concatenation is the operation of joining two strings (like hand and bag to become handbag)
a function that returns values can be created, often involving a calculation (like to return the number of lives a player has in a game
- an example: def triple_it(number): / value = 3 * number / return value / x = triple_it(5) / print(x)
- the slash symbol (/) in this case represents every time the code moves onto a new line
- on the first line, the parameters (number) of the function are specified
- on the second line, the parameter (number) is multiplied by 3, and the result of this calculation is stored in the variable 'value'. Then on the third line, the variable 'value' is returned (like a function machine, the variable 'value' is the result, what comes out)
- on the fourth line, the parameter 'number' is given an actual value (5), and the function triple_it is called to calculate the number (5) * 3. This value is stored inside x, and is printed on the fifth line.
- changing the number from 5 to 6 would mean that 6 is multiplied by 3 instead, so x would equal 18
- when using a returned value, if it needs to be stored, the variable is on the right, such as x=triple_it(5). If the function does not need to return a variable, the variables are on the left (like, output_user_details(user_name, given_name). In this case, user_name and given_name are variables that do not need to be returned.
- an example: def triple_it(number): / value = 3 * number / return value / x = triple_it(5) / print(x)
*tip; instead of using ^ to square numbers, multiply the number by itself (instead of x^2, do x*x)
Information
- Description
- Mind Map on creating functions (AFEC course 4), created by emily ! on 24/05/2020.
- Created by
- emily !
- Views
- 1
- Rating