Python Functions
Python Tutorial » Python Functions
A function is a block of code which only runs when it is called.
A function can return data as a result.
Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.
A function in Python is defined using "def" without quotes:
Example: how to make a function - calling function
Output:
Hello there, this is my first function Python
def my_pyt_function():
print("Hello there, this is my first function Python")
my_pyt_function()
print("Hello there, this is my first function Python")
my_pyt_function()
Output:
Hello there, this is my first function Python
You can call a function using this types of formal arguments:
Keyword arguments,
Required arguments,
Default arguments,
Variable-length arguments
Keyword arguments - example
When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
Example:
The older person is Tom
def my_function(person3, person2, person1):
print("The older person is " + person3)
my_function(person1 = "John", person2 = "Chris", person3 = "Tom")
Output: print("The older person is " + person3)
my_function(person1 = "John", person2 = "Chris", person3 = "Tom")
The older person is Tom
Default arguments - example
If we call python function without argument, it will uses default value:
Example:
I am from Milan
I am from Napoli
I am from Marseille
I am from Frankfurt
def my_function(town = "Barcelon"):
print("I am from " + town)
my_function("Milan")
my_function("Napoli")
my_function("Marseille")
my_function("Frankfurt")
Output:print("I am from " + town)
my_function("Milan")
my_function("Napoli")
my_function("Marseille")
my_function("Frankfurt")
I am from Milan
I am from Napoli
I am from Marseille
I am from Frankfurt
Return Values
To make a function to return value, have to use "return" statement without quotes:
Example:
144
48
72
84
def my_function(y):
return 12 * y
print(my_function(12))
print(my_function(4))
print(my_function(6))
print(my_function(7))
Output:return 12 * y
print(my_function(12))
print(my_function(4))
print(my_function(6))
print(my_function(7))
144
48
72
84
python function return, arguments, parameters, as parameters, documentation, declaration, with variable number of parameters, returns 2 values, arguments type, global variable
Python Functions - python
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.