-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
68 lines (48 loc) · 1.19 KB
/
functions.py
File metadata and controls
68 lines (48 loc) · 1.19 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
a = 4
b = 23
sum = a + b
print(sum)
# function definition
def calc_sum(a, b): # parameters
return a + b
sum = calc_sum(1, 2) #function call; arguments
print(sum)
# # more lines of code
# a = 4
# b = 10
# sum = a + b
# print(sum) # again we rewrite the logic for the same task
# we are redundant the code, which is not good in coding
def calcSum(x, y):
sum = x + y
print(sum)
return sum
# how we call the function
calcSum(3, 7)
calcSum(4, 3)
calcSum(36, 6)
# now we decrease the redundant by using the function
# we use functions to reduce the redundancy form the code
def my_function():
print("hello from asfar")
my_function()
def fahrenheit_to_celsius(fahrenheit):
return(fahrenheit - 32) * 5 / 9
print(fahrenheit_to_celsius(77))
def get_greeting():
return "Hello Everyone"
message = get_greeting()
print(message)
def my_function(fname):
print(fname + "Refsnes")
my_function("Ismail")
my_function("ahmad")
# default parameter values
def greet(name = "friend"):
print("hello", name)
greet("asfar")
greet()
def name_of_animal(animal, name):
print("I have a", animal)
print(f"M {animal}'s name is {name}")
name_of_animal(animal="dog", name= "jack")