Python Functions Beginner Tutorial
Functions make the code organized, easy to read, save time and because you no need to write the same lines of codes when repeating same operation you save the memory with number of lines.
What are the functions in Python?
Function is a block of code which runs whenever is called.
Functions are defined by "def" keyword and followed by the name of function and both closing and opening parentheses and colon, the block of code with instructions is in the following indented lines. To call function use function_name() without "def" this time
def function_name():
.....
.....
function_name()
Think about functions like employs in the Restaurant, everyone has its function to do: Welcoming desk does welcome customers, Waiter - does take orders, Chef does cook meals, Waiter2- brings the food and takes back dishes.
Everyone has defined functions and that makes the job organized. Same do functions in Python programming, you can repeat same task you have done just by calling the function without writing same code again.
Below is example of defined printer function that prints a string: Hello World.
def printer():
print('Hello World')
printer()
__________________________________________________________________
/viteac/python/beginner/tutorial/python3.8 https://viteac.blogspot.com
Hello World
Passing arguments to function
What makes function more useful and powerful is that you can pass arguments to the functions.
def welcome(name):
print(f'Hello {name}')
welcome('Peter')
welcome('Kate')
__________________________________________________________________
/viteac/python/beginner/tutorial/python3.8 https://viteac.blogspot.com
Hello Peter
Hello Kate
We have defined function named "welcome", this function can take parameter "name" and in next line we're telling Python to print a text Hello ... and name passed as argument when calling the function.
Register Your Domains Hassle-Free with Namecheap starting at $3.98/year
Function returns value
You can do something with the object and exit the function with returning the value as well.
def multi(g, r):
return g + r
print(multi(2,3))
__________________________________________________________________
/viteac/python/beginner/tutorial/python3.8 https://viteac.blogspot.com
5
In above code two arguments were passed to multi() function which job was to add them and return the sum. Function multi is called in print statement so it will print returned value by the function.
Store returned value
You can store returned value.
def multi(g, r):
return g + r
result=(multi(1,3))
print(result)
result2=result2+1
print(result2)
__________________________________________________________________
/viteac/python/beginner/tutorial/python3.8 https://viteac.blogspot.com
4
5
Global and Local values in Python function.
Variable inside function is Local and outside function is Global.
Local variable if changed inside function won't be changed outside as Global.
n= 1 # Our Global n variable
def change(x):
n = x +1
print(f'Local n: {n}') # n is Local inside function
change(n)
print(f'Global n: {n}')
__________________________________________________________________
/viteac/python/beginner/tutorial/python3.8 https://viteac.blogspot.com
Local n: 2
Global n: 1
Comments
Post a Comment