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!
1def test_function(argument1,argument2,argument3) :
2 # Do something with the code, and the arguments.
3 print(argument1)
4 print(argument2)
5 print(argument3)
6
7# Calling the function.
8
9test_function('Hello','World','!')
10
11# Output
12'''
13Hello
14World
15!
16'''
1# Parameter of the function
2# |
3def greetings(Name):
4 #Content inside the function
5 print("Hello",Name)
6 print("How are you",Name)
7print(greetings("Python devloper"))
8# ^
9# |
10# Argument of the function
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
1def name():#name of the def(function);
2 print("Hallo, World!")#Output is Hallo, World!;
3
4name()#Name of the def, the programm will jump to the def;
5
6#output:
7#Hallo, World!
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!)