Skip to content

Commit 6716e4d

Browse files
Added a function file with example
1 parent 29afed0 commit 6716e4d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

other/functions.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
A pure Python implementation of Functions
3+
4+
A Function in python is a reusable piece of code that takes N parameters and can be called as many times as we want.
5+
Functions are important because:
6+
1) They make our code clean
7+
2) They make our code small
8+
3) It improves performance
9+
For example : A function to add two numbers a and b will have two parameters a and b
10+
11+
"""
12+
13+
def addition(a ,b):
14+
"""
15+
A function in python is created with a keyword 'def' in all small , followed with the name of the function for example in here addition
16+
and in the brackets we pass on the parameter's value the function will work upon
17+
18+
"""
19+
return a + b
20+
21+
"""
22+
The return keyword as the name suggests returns the result of the function in here it returns the value of addition of a and b
23+
24+
"""
25+
26+
print(addition(4,5))
27+
28+
"""
29+
To call a function you type in the function's name and pass on the values that you want your function to pass
30+
For example : Result for this case will be 9 as 4 + 5 here automatically the function assumes a = 4 and b = 5
31+
another way to call a function is to specify the values for example print(addition(a=4 ,b= 5))
32+
33+
"""
34+
35+
36+
"""
37+
38+
Another function for example can be to print every item in a list
39+
For example
40+
41+
"""
42+
43+
def each_item_in_list(list):
44+
for item in list:
45+
print(item)
46+
"""
47+
Here the function takes takes the parameter 'list' and this function when called upon will print every item of the list
48+
"""
49+
each_item_in_list([1,2,3,4,5,6])
50+
51+
"""
52+
Upon calling the function the result will be
53+
1
54+
2
55+
3
56+
4
57+
5
58+
6
59+
"""

0 commit comments

Comments
 (0)