Function Parameters
Function Parameters
A function is a set of statements that take inputs, do some specific computation, and produce output. The
idea is to put some commonly or repeatedly done tasks together and make a function so that instead of
writing the same code again and again for different inputs, we can call the function. Functions that readily
come with Python are called built-in functions. Python provides built-in functions like print(), etc. but we can
also create your own functions. These functions are known as user defines functions.
Table of Content
User defined functions
Parameterized functions
o Default arguments
o Keyword arguments
o Variable length arguments
o Pass by Reference or pass by value?
Syntax:
def function_name():
statements
.
.
Example: Here we have created the fun function and then called the fun() function to print the statement.
Python
# Declaring a function
def fun():
print("Inside function")
# Driver's code
# Calling function
fun()
Output:
Inside function
Example:
A simple Python function to check whether x is even or odd.
Python
def evenOdd( x ):
if (x % 2 == 0):
print("even")
else:
print("odd")
# Driver code
evenOdd(2)
evenOdd(3)
Output:
even
odd
# Driver code
myFun(10)
Output:
x: 10
y: 50
# Keyword arguments
student(firstname ='Geeks', lastname ='Practice')
student(lastname ='Practice', firstname ='Geeks')
Output:
Geeks Practice
Geeks Practice
# global variable
a = 15
b = 10
# calling a function
add()
Output:
25
Output:
UnboundLocalError: local variable 'a' referenced before assignment
This output is an error because we are trying to assign a value to a variable in an outer scope. This can be
done with the use of a global variable.
Output:
Value of x inside a function : 20
Value of x outside a function : 20
Output:
'arr' list before executing fun(): [10, 20, 30]
'arr' list after executing fun(): [20, 30, 40]
Here we are trying to assign a new list to the global variable. Thus, we need to use the global keyword as a
new object is created. Here, if we don’t use the global keyword, then a new local variable arr will be created
with the new list elements. But the global variable arr will be unchanged.
Python3
arr = [10, 20, 30]
def fun():
global arr
arr = [20, 30, 40]
print("'arr' list before executing fun():", arr)
fun()
print("'arr' list after executing fun():", arr)
Output:
'arr' list before executing fun(): [10, 20, 30]
'arr' list after executing fun(): [20, 30, 40]
Python Scope of Variables
Python Scope variable
The location where we can find a variable and also access it if required is called the scope of a variable.
def f():
# local variable
s = "I love Geeksforgeeks"
print(s)
# Driver code
f()
Output
I love Geeksforgeeks
If we will try to use this local variable outside the function then let’s see what will happen.
def f():
# local variable
s = "I love Geeksforgeeks"
print("Inside Function:", s)
# Driver code
f()
print(s)
Output:
NameError: name 's' is not defined
Global variables are the ones that are defined and declared outside any function and are not specified to any
function. They can be used by any part of the program.
Example:
Python3
# This function uses global variable s
def f():
print(s)
# Global scope
s = "I love Geeksforgeeks"
f()
Output:
I love Geeksforgeeks
# Global scope
s = "I love Geeksforgeeks"
f()
print(s)
Output:
Me too.
I love Geeksforgeeks
Consider the below example for a better understanding of the topic.
Python3
a =1
# Global scope
print('global : ', a)
f()
print('global : ', a)
g()
print('global : ', a)
h()
print('global : ', a)
Output:
global : 1
Inside f() : 1
global : 1
Inside g() : 2
global : 1
Inside h() : 3
global : 3
In Python, when we define functions with default values for certain parameters, it is said to have its
arguments set as an option for the user. Users can either pass their values or can pretend the function to use
theirs default values which are specified.
In this way, the user can call the function by either passing those optional parameters or just passing the
required parameters.
There are two main ways to pass optional parameters in python
Without using keyword arguments.
By using keyword arguments.
Passing without using keyword arguments
Some main point to be taken care while passing without using keyword arguments is :
The order of parameters should be maintained i.e. the order in which parameters are defined in function
should be maintained while calling the function.
The values for the non-optional parameters should be passed otherwise it will throw an error.
The value of the default arguments can be either passed or ignored.
Below are some codes which explain this concept.
Example 1:
Python
# Here b is predefined and hence is optional.
def func(a, b=1098):
return a+b
print(func(2, 2))
Output:
4
1099
Example 2: we can also pass strings.
Python
# Here string2 is the default string used
def fun2(string1, string2="Geeks"):
print(string1 + string2)
Output:
GeeksForGeeks
GeeksForGeeks
Passing with keyword arguments
When functions are defined then the parameters are written in the form “datatype keyword-name”. So python
provides a mechanism to call the function using the keyword name for passing the values. This helps the
programmer by relieving them not to learn the sequence or the order in which the parameters are to be
passed.
Some important points we need to remember are as follows:
In this case, we are not required to maintain the order of passing the values.
There should be no difference between the passed and declared keyword names.
Output:
As we can see that we don’t require any order to be maintained in the above example. Also, we can see that
when we try to pass only the optional parameters then it raises an error. This happens since optional
parameters can be omitted as they have a default with them, but we cannot omit required parameters (string1
in the above case.) Hence, it shows an error with the flag: “missing 1 required argument”.
print("first call")
comp(1)
print("second call")
comp(2, 1)
print("third call")
comp(b=1, a=-1)
print("fourth call")
comp(-1, b=0)
Output:
first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
So one thing we should remember that the keyword argument should used after all positional arguments are
passed. Hence this is an important thing we must keep in mind while passing parameters in both ways to
same function.
How to pass optional parameters to a function in Python? – FAQs