How to set default parameter values to a function in Python?

PythonProgrammingServer Side Programming

Python functions are used to implement logic that you wish to run repeatedly at various locations across your code. These functions accept function arguments as input parameters. You can specify default argument values in Python functions in addition to supplying parameters to them through function calls.

If you don't explicitly specify a parameter value to the given argument, these default values are applied to function arguments. The actual values supplied to function arguments are called parameters.

The syntax representation and default values for function parameters are different in Python. If no argument value is given during the function call, default values mean that the function parameter will assume that value. By employing the assignment(=) operator with the syntax keywordname=value, the default value is set.

Function in Python

A function is a section of code that only executes when called. You can supply parameters that take data to a function. As a result, a function may return data.

Example

Let us understand what a function in python is with the help of an example.

def demo_function(): print("Hello World") demo_function()

Output

The output generated is as follows.

Hello World

Arguments in a function in python

Functions accept arguments that can contain data. The function name is followed by parenthesis that list the arguments. Simply separate each argument with a comma to add as many as you like. The function in the following example only takes one argument (fname). A first name is passed to the function when it is called, and it is utilized there to print the whole name.

Function with Default Arguments

The arguments that take default values when no explicit values are supplied to them from the function call are known as default arguments in Python functions.

Example 1

Let us look at a simple python function as an example that uses a default argument.

def greet(name="world"): print("Hello,", name) greet()

Output

The output generated is as follows.

Hello, world

Example 2

Let us create a function that has a single default argument.

The script defines the function find sqaure() with a single integer as the default argument. The integer argument's default value is set to 2. The cube of a value passed as the integer argument to the find cube() method's call will be returned by the function.

Otherwise, the default value, 2, will be assigned to the integer argument of the find square() function, and the function will return the square of 2, which is 8. If you do not pass any value for the integer argument of the find cube() function, you will observe that.

Let's initially use the argument value of 10 to call the find cube() method −

def find_cube(integer1=2): result = integer1 * integer1 * integer1 return result result= find_cube(10) print(result)

Output

The output generated is as follows.

1000

Example 3

Let us see one more example for this −

def func(data=[]): data.append(1) return data func() func() def append2(element, foo=None): if foo is None: foo = [] foo.append(element) return foo print(append2(12)) print(append2(43))

Output

The output generated is as follows.

[12]
[43]

Multiple Default arguments

It is also possible are several default arguments for a Python method. For instance, the function sums the integer numbers supplied as parameters in the script below.

Example

The default arguments for the function would be 12 and 4, respectively, if either of the integer values were omitted, as illustrated below.

def add_integers(int1=12, int2=4): result = int1 + int2 return result result = add_integers() print(result)

Output

The output generated is as follows.

16
raja
Updated on 16-Sep-2022 07:38:48

Advertisements