Custom Python Functions

Python Methods and Functions | Regular Expressions

Functions that are easily part of Python are called inline functions. Python provides built-in functions like print () and so on, but we can also create your own functions. These functions are known as custom functions .

Table of Content

Custom Functions

All functions written by us fall under the Custom Functions category. Following are the steps for writing UDFs in Python.

  • In Python, the def keyword is used to declare UDFs.
  • Indented statement blocks are followed by the function name and arguments that contain the function body.

Syntax :

 def function_name (): statements. ... 

Example :

# Python program for
# demonstrate functions

 
# Function declaration

def fun ():

print ( " Inside function " )

 
# Driver code
# Function call
fun ()

Exit:

 Inside function 

Pair metric function

A function can take arguments (arguments), also called parameters, as input in opening and closing parentheses, immediately after the function name, followed by a colon.

Syntax:

 def function_name (argument1, argument2, ...): statements. ... 

Example :

# Python program for
# demonstrate functions

 
# Simple Python function to check
# is x even or odd

def evenOdd (x): 

if (x % 2 = = 0 ): 

print ( "even" )

  else

  print ( " odd " )

 
# Driver code

evenOdd ( 2

evenOdd ( 3 )

Exit :

 even odd 

Default Arguments

Default Argument — it is a parameter that takes on a default value if no value is specified in the function call for that argument. The following example shows the default arguments.

Example :

# Demo Python program
# default arguments

def myFun (x, y = 50 ) : 

print ( "x:" , x) 

print ( "y:" , y) 

  
# Driver code (we call myFun () only with
# argument)

myFun ( 10

Output:

 x: 10 y: 50 

Note. To learn more about default arguments, click here .

Key arguments

The idea is to let the caller side to specify the name of the argument with values ​​so that the caller does not need to remember the order of the parameters.

Example :

# Python program to demonstrate keyword arguments

def student (firstname, lastname): 

print (firstname, lastname) 

 

 
# Keyword arguments

student (firstname = 'Geeks' , lastname = 'Practice'

student (lastname = 'Practice' , firstname = ' Geeks'

Exit:

 Geeks Practice Geeks Practice 

Variable length arguments

We can have both a regular and a keyword with a variable number of args parameters.

  • The special syntax * args in function definitions in Python is used to pass a variable the number of arguments to a function. It is used to pass a variable length argument list without a key.
  • The special syntax ** kwargs in function definitions in python is used to pass a keyed variable length argument list. We use the double star name kwargs. The reason is that the double star allows us to pass through keyword arguments (and any number of them).

Example :

# Python program for illustration
# * args and ** kwargs

def myFun1 ( * argv): 

for arg in argv: 

print (arg) 

 

def myFun2 ( * * kwargs): 

for key, value in kwargs.items (): 

print ( "% s ==% s " % (key, value)) 

  
# Driver code

print ( "Result of * args:" )

myFun1 ( 'Hello' , ' Welcome ' , ' to' , 'GeeksforGeeks'

 

print ( " Result of ** kwargs " )

myFun2 (first = 'Geeks' , mid = 'for' , last = ' Geeks'

Exit:

 Result of * args: Hello Welcome to Python.Engineering Result of ** kwargs mid == for first == Geeks last == Geeks 

Note. To learn more about variable length arguments,

# Python program for
# confirm upload by link

 

def myFun (x):

print ( "Value recieved : " , x, " id: " , id (x))

 
# Driver code

x = 12

print ( "Value passed:" , x, "id:" , id (x))

myFun (x)

Exit:

 Value passed: 12 id: 10853984 Value recieved: 12 id : 10853984 

If the value of the above variable is changed within the function, it will create another variable as a number that is unchanged. However, if a mutable list object changes inside a function, the changes are also reflected outside the function.

Example :

def myFun (x, arr):

print ( " Inside function " )

 

# change integer will

# Also change the link

# to variable

x + = 10

print ( "Value received" , x, "Id" , id (x))

  

  # Modifying mutable objects

# will also be reflected outside

# function

arr [ 0 ] = 0

print ( " List received " , arr, "Id" , id (arr))

  
# Driver code

x = 10

arr = [ 1 , 2 , 3 ]

  

print ( "Before calling function" )

print ( "Value passed" , x, "Id" , id (x))

print ( "Array passed" , arr, "Id" , id (arr))

print ()

 
myFun (x, arr)

 

print ( "After calling function" )

print ( "Value passed" , x, "Id" , id (x))

print ( "Array passed" , arr, "Id" , id (arr))

Exit :

 Before calling function Value passed 10 Id 10853920 Array passed [1, 2, 3] Id 139773681420488 Inside function Value received 20 Id 10854240 List received [0, 2, 3] Id 139773681420488 After calling function Value passed 10 Id 10853920 Array passed [0, 2, 3] Id 139773681420488 

Sometimes we may need the result of using a function in a later process. Therefore, the function must also return a value when it finishes executing. This can be achieved by return .
The return statement is used to complete a function call and "return" the result (the value of the expression after the return keyword) to the caller. Statements after return statements are not executed. If the return statement has no expression, the special value None is returned.

Syntax :

 def fun (): statements. ... return [expression] 

Example :

# Python program for
# demonstrate refund

 

def add (a, b):

 

# return the amount a and b

return a + b

 

def is_true (a):

 

# returning a boolean

return bool (a)

  
# calling function

res = add ( 2 , 3 )

print ( "Result of add function is {}" . format (res))

 

res = is_true ( 2 & lt; 5 )

print ( "Result of is_true function is {}" . format (res))

Exit:

 Result of add function is 5 Result of is_true function is True 




Get Solution for free from DataCamp guru