Introduction

A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function. Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are called user-defined functions.

In Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. Parameter passing in Python is same as reference passing in Java.

# Here x is a new reference to same list myList 
def myFun(x): 
x[0] = 20

# myList is modified after function call. 
myList = [10, 11, 12, 13, 14, 15] 
myFun(myList); 
print(myList) 

# Output
[20, 11, 12, 13, 14, 15]

Default Arguments

A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument.

# Default argument
def myFun(x, y=50): 
  print("x: ", x) 
  print("y: ", y) 

# Called with one argument
myFun(10)

Keyword Arguments

It allow caller to specify argument name with values so that caller does not need to remember order of parameters.

# Demonstrate Keyword Arguments 
def myFun(str1, str2): 
  print(str1, str2) 
  
# Keyword Arguments			 
myFun(str1 ='Hello', str2 ='World')	 
myFun(str2 ='World', str1 ='Hello')

# Positional Argument
myFun('Hello', 'World')

Positional parameters are the default kind of parameter. For positional arguments, the order of the arguments matters. To define a function where all parameters are required to be referenced by keyword, begin parameter list with an asterisk as the first parameter. So these are only specifiable using the name of the argument, and cannot be specified as a positional argument.

def myFunction(str1, str2, *, str3): 
  print(str1, str2, str3)

myFunction('Hello', 'World', str3='!')
# myFunction('Hello', 'World', '!') will throw error

# Output
# Hello World !

In the above example str3 must be passed as keyword argument as shown. Passing it as position argument will give error.

Variable Length Arguments

We can have both normal and keyword variable number of arguments. Variable length argument should always be last argument.

# Illustrate variable number of arguments 
def myFun(*argv): 
  for arg in argv: 
    print (arg) 
  
myFun('Hello', 'Welcome')

Return Statement

The return statement is used to exit a function and go back to the place from where it was called. This statement can contain expression which gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.

# Example 1
def greet(name):
  print("Hello, " + name)

print(greet("May"))

# Output, 'None' is function return value
Hello, May
None

# Example 2
def absolute_value(num):
  if num >= 0:
    return num
  else:
    return -num

print(absolute_value(2))

# Output
2

Documentation String

Use a docstring right under the first line of a function declaration to explains what the function does.

def hello():
  """
  This Python function simply prints hello to the screen
  """
  print("Hello")
  
  
# Accessing the docstring
print(hello.__doc__)

# Output
This Python function simply prints hello to the screen

Deleting Function

You can delete a function with the ‘del’ keyword.

def myFun():
  print("Fun")

del myFun

# It will throw error
myFun()