Python Functions is a block of code that works on variable input and produces output. Functions offer higher modularity for your application and a high degree of code reusing.
Features of Functions
- Function is a block of code which is executed when it is called.
- You can pass information, referred to as parameters, into a function.
- A function can return information as a result.
Creating a Function
def keyword is used to define a function
def myFunction(): print("Hello from function")
Calling a Function
To call the particular defined function using the name of the function followed by ().
# Calling a Function myFunction()
Passing Parameter to Functions
You can pass data, known as parameters, into a function.
# Passing Parameters def myFunction1(fname="India",message="How are you?"): print("Hello",fname,message) myFunction1("Huzaif","How are you?") myFunction1() # Output -> ('Hello', 'India', 'How are you?')
Return values from Function
A function can return data as a result.
# Return Value def myFunction2(num): return 4 * num x = myFunction2(5) # returned value store in x print(x) # Output -> 20
Whats Next – Python Lambda Function