1def myFunction(say): #you can add variables to the function
2 print(say)
3
4myFunction("Hello")
5
6age = input("How old are you?")
7
8myFunction("You are {} years old!".format(age))
9
10#this is what you get:
11
12Hello
13How old are you?
14>>11 #lol my real age actually
15You are 11 years old!
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#folling is a simple function
4def exmple_of_function():
5 print("Hello from a function")
6
7example_of_function()
1# Python Functions
2def a_function(): #def FUNCTIONNAMEHERE(): Function content
3 print("Hello World!")
4 return 0
5def answerValid(prompt, correct_answers, invalid): # Functions can also have parameters, or variables that can be used only in tha function
6 while True:
7 user = input(prompt)
8 if user in correct_answers:
9 break
10 return user # Example Function
11 else:
12 print(invalid)
13
14a_function() # To call a function; Prints 'Hello World!'
15# But if this is done:
16function_called = a_function() # function_called would contain a 0, because the function returns 0
17answer = answerValid("What is 8 * 8?", [64], "Invalid Option")
18print(answer) # Prints 64, because that is the only possible answer
1def add(number):
2 equation = 5 + number
3 print(equation)
4
5
6add(10)
7
8output:
9
10 15
11