-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
88 lines (57 loc) · 1.85 KB
/
functions.py
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
77
78
79
80
81
82
83
84
85
86
87
88
#Functions
# example of flow of execution of functions
def hello():
print("good morning")
print("kibabii university")
print('hello')
print("how are you doing")
hello()
print("done!")
#Parameters and arguments
#parameters are passsed durin function defination
#function defination
def add(a,b):
return a+b
#Arguments are passed during function call
#fuction call
results=add(12,48)
print (results)
#function syntax
#Syntax:
def functionname():
#statements
functionname()
'''Function definition consists of following components:
1. Keyword def indicates the start of function header.
2. A function name to uniquely identify it. Function naming follows the same rules of writing
identifiers in Python.
3. Parameters (arguments) through which we pass values to a function. They are optional.
4. A colon (:) to mark the end of function header.
5. Optional documentation string (docstring) to describe what the function does.
6. One or more valid python statements that make up the function body. Statements must have
same indentation level (usually 4 spaces).
7. An optional return statement to return a value from the function.'''
#Example:
def hf():
print("hello world") #function definition
hf() #function call
'''In the above example we are just trying to execute the program by calling the function. So it
will not display any error and no output on to the screen but gets executed.
To get the statements of function need to be use print().'''
#calling function in python:
def hf():
print("hello world")
hf()
'''Output:
hello world
--------------------------'''
def hf():
print("hw")
print("gh kfjg 66666")
hf()
hf()
hf()
def my_function(fname):
print(fname + "Kamau")
my_function("Eliab ")
my_function('Dennis ')