Python Function
Hello everyone! I will mention function in this article. I will code on Jupyter Notebook in Python. Let’s start!
Functions
What does the purpose of using the function in Python?
The mission of functions is to aggregate complex operations together.
is to enable us to do the operations in one step.
Creating a Function
In Python, a function is defined using the “def” keyword. To call a function, use the function name followed by parenthesis.
def my_favorite_movie():
print("Harry Potter")my_favorite_movie# It doesn't take the parameter.
Output:
<function __main__.my_favorite_movie()>
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
def sayHello(name):
print("Hello name {} welcome to club". format(name))sayHello("Hermione Granger")# It's taking the parameter
Output:
Hello name Hermione Granger welcome to club
Parameters or Arguments?
The terms parameter and argument can be used for the same thing: information that is passed into a function.
From a function’s perspective:
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that is sent to the function when it is called.
Let’s write a function that returns the sum of two numbers,
def add(num1, num2):
result = num1 + num2
return(result) # return key word provides return.result = add(3,9)
print(result)
Output:
12from random import randintprint(randint(10,20))
Output:
18def add(num1, num2):
return num1 + num2print(add(3,9))
Output:
12
Global and Local Variables Inside Function
I want to explain for an example,
global_variable = "Global"def func():
local_variable = "Local"
print("Global variable value inside the function {}, local variable {}" .format(global_variable, local_variable))
func()
Output:
Global variable value inside the function Global, local variable Local
Well, if we do
global_variable = "Global"def func():
local_variable = "Local"
print("Global variable value inside the function {}, local variable {}" .format(global_variable, local_variable))
func()
print("Global variable value outside the function {}, local variable {}" .format(global_variable, local_variable))
What happened?
It gives an error.
global_variable = "Global"def func():
local_variable = "Local"
print("Global variable value inside the function {}, local variable {}" .format(global_variable, local_variable))
func()
print("Global variable value outside the function {}, local variable {}" .format(global_variable, local_variable))
Output:
--------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [41], in <cell line: 7>()
5 print("Global variable value inside the function {}, local variable {}" .format(global_variable, local_variable))
6 func()
----> 7 print("Global variable value outside the function {}, local variable {}" .format(global_variable, local_variable))
NameError: name 'local_variable' is not defined
Why?
The reason is that the variable defined inside the function cannot be accessed outside the function.
Ok, let’s do another example,
a = 4def print_a():
print(a)
def print_a2():
a = 8
print(a)
def print_a3():
print(a)
print(a)
print_a2()
print_a3()
Output:
4
8
4
a = 4 → Global Variable
def print_a2():
a = 8 → Local Variable
If we want to change the global variable value,
a = 4def print_a():
print(a)
def print_a2():
global a
a = 8
print(a)
def print_a3():
print(a)
print(a)
print_a2()
print_a3()
Output:
4
8
8
We should write,
global a
a = 8
Modules In Python
What is a Module?
Consider a module to be the same as a code library.
A file containing a set of functions you want to include in your application.
import math as m
m.factorial(4)
x = m.factorial(5)
print(x)
print(m.factorial(6))
Output:
120
720from math import factorial
print(factorial(5))
Output:
120
We can write like this,
from math import factorial as f
print(f(5))
Output:
120
It’s the same thing.
I hope while you’re reading my article you can enjoy it!
See you on another topic :)
If you want, you can follow me at these links: