1#Letters, Nums,
2#& underscores
3#only, can't =7
4#start w/ num | _ 5 is the default value if no
5# | | | value is passed
6# v v v
7def funcName(param1, param2=5): #The colon terminates func header
8 str(param1) #Typecasting is temparary
9 strVar = str(param1) # To make typecast perma, assign to var
10 param1 = param2 # 5 is passed to param1
11 return param1 # 5 is returned
12# ^ ^
13# | |_ return value is optional, if no return value, func
14 #Optional will end returning to func call
15 #return
16
17x = funcName(7)
18print(x) # prints 5
1#Functions can be re used in programmes and can be extremely useful.
2#You would use functions a lot when you make programmes using packages such
3#as Tkinter, of which you will in the future.
4#The Format:
5#def [Whatever name you want]()
6#[The "()" at the end is compulsory as we are making a function.]
7
8#Think of it like using an inbuilt function from python like .lower()
9
10
11def greet():
12 print("Hello!")
13
14greet() #We are recalling the function by typing out its name and brackets
15
16
17#This function will have something known as a parameter aka arguement
18#This example will show a non changeable arguement unless coded
19
20
21#Option 1, will directly print the sum:
22def math(num1, num2):
23 sum = num1+num2
24 print(sum)
25
26math(1, 2) #We change the num 1 and num 2 to the one and 2, though this can't change unless progammed to.
27
28#Option 2, will return the sum and then print upon command.
29def math(num1, num2):
30 sum = num1+num2
31 return sum
32
33print(math(1, 2))
34
35#Good luck to all my future Software engineers! Hope life treats you all well!
36#Inshallah! (Meaning if Allah allows it!)
1OK, basically def function() is a block where you will have one task that you can repeat all over again in the code to make it look short and clean
1def function():
2 print('This is a basic function')
3
4function()
5// Returns 'This is a basic function'
6
7def add(numA, numB):
8 print(numA+numB)
9
10add(1,2)
11// Returns 3
12
13def define(value):
14 return value
15
16example = define('Lorem ipsum')
17print(example)
18// Returns 'Lorem ipsum'
19
20
1#A function is a block of code which only runs when it is called.
2#You can pass data, known as parameters, into a function.
3#following is a simple function
4def exmple_of_function():
5 print("Hello from a function")
6
7example_of_function()