AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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
def 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:
def my_function(person3, person2, person1):
  print("The older person is " + person3)

my_function(person1 = "John", person2 = "Chris", person3 = "Tom")
Output:
The older person is Tom


Default arguments - example

If we call python function without argument, it will uses default value:

Example:
def my_function(town = "Barcelon"):
  print("I am from " + town)

my_function("Milan")
my_function("Napoli")
my_function("Marseille")
my_function("Frankfurt")
Output:
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:
def my_function(y):
  return 12 * y

print(my_function(12))
print(my_function(4))
print(my_function(6))
print(my_function(7))
Output:
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
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news 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
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news 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
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.