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# declare a function
2def function_name(param) :
3 # content inside a function
4
5function_name(param) # calling function
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
1# You can make a function by using the [def] keyword
2def foo():
3 print("Hello")
4
5# You can run it by calling it as such
6foo()
7
8def add(numA, numB):
9 print(numA+numB)
10 # or
11 return numA+numB
12
13num = add(1, 5)
14print(num)
1#Use this to shortern a task that you may want to repeat
2#Used for making custom commands
3def paste(a, b)
4 for i in b:
5 print(a)
6
7
8while True:
9 paste("Python is Cool!!!", 3)
10
11#Output:
12#Python is Cool!!!
13#Python is Cool!!!
14#Python is Cool!!!
15
16
17
18
19
20## - ANOTHER EXAMPLE - ##
21
22
23
24
25
26happy = "NEUTRAL"
27
28def yes():
29 happy="TRUE"
30 print("Thank you!")
31
32def no():
33 happy="FALSE"
34 print("That's not nice!")
35
36answer=str(input("Do you like my new shoes? (y/n) ")
37if answer=="yes" or "y" or "Yes" or "Y" or "YES":
38 yes()
39elif answer=="no" or "n" or "No" or "N" or "NO":
40 no()