-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_function.py
52 lines (34 loc) · 1.16 KB
/
python_function.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
# Hello World program in Python
print("Python Function\n")
# Creating a Function
# In Python a function is defined using the def keyword:
def my_function():
print("Hello from a function")
# Calling a Function
# To call a function, use the function name followed by parenthesis:
my_function()
print("Parameters")
# Information can be passed to functions as parameter.
# The following example has a function with one parameter (fname). When the function is called, we pass along a first name,
# which is used inside the function to print the full name:
def my_function(fname):
print(fname + " of Employee")
my_function("name")
my_function("Emil")
my_function("salary")
# Default Parameter Value
# The following example shows how to use a default parameter value.
# If we call the function without parameter, it uses the default value:
def my_function(country="India"):
print("I am from " + country)
my_function("Sweden")
my_function("Japan")
my_function()
my_function("Brazil")
# Return Values
# To let a function return a value, use the return statement:
def my_function(x):
return 6 * x
print(my_function(4))
print(my_function(5))
print(my_function(10))