-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReuse of program(Function examples ).py
More file actions
76 lines (46 loc) · 1.43 KB
/
Reuse of program(Function examples ).py
File metadata and controls
76 lines (46 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def my_function(fname):
print(fname + "danial")
my_function("john ")
my_function("Mike ")
# If we call the function without parameter, it uses the default value:
def countryname(country="Norway"):
print("i am from " + country)
countryname("Pakistan")
countryname("")
countryname("china")
# You can send any data types of parameter to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
def eat(food):
for x in food:
print(x)
fruits = ["apple", "banana", "orange"]
fruits.append("mango")
eat(fruits)
# return value
def cal(x):
return 5 * x
print(cal(3))
print(cal(5))
print(cal(7))
# If the number of arguments are unknown, add a * before the parameter name:
def child(*kids):
print("the youngest child is " + kids[2])
child("amy", "mandy", "sam")
def changeme(mylist):
mylist.append([1, 2, 3, 4]);
print("Values inside the function", mylist)
return
mylists = [10, 20, 30, 40];
changeme(mylists);
print("Values outside the functioon:", mylists)
def changemy(mylist):
mylist = [1, 2, 3, 4];
print("Values inside the function", mylist);
return
mylist = [10, 20, 30, 40];
changemy(mylist);
print("Values outside the function", mylist);
def printinfo(name, age):
print("Name:", name)
print("age:", age)
return
printinfo('max', 35);