Site icon DataFlair

Python Program on Function Arguments

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Embarking on the practical aspects of Python, our focus shifts to understanding Python function arguments and their types. In simple terms, function arguments are the values passed to a function, influencing its behavior. Whether it’s specifying values in a specific order or explicitly identifying them by name, Python functions handle varied inputs. Our exploration aims to demystify the various argument types, offering clarity on how Python functions can flexibly receive and handle input.

Topic Explanation:

In Python, function arguments come in different types, with the most common being positional and keyword arguments. Positional arguments are values passed to a function in a specific order, while keyword arguments are explicitly identified by their parameter names. This flexibility allows functions to handle varied inputs. Additionally, default values can be assigned to parameters, ensuring smooth execution even when certain arguments are omitted.

As we delve deeper, we explore the use of variable-length argument lists, denoted by ‘*’ and ‘**’ symbols, providing a dynamic way to handle varying numbers of inputs. This versatility in argument handling contributes to the adaptability and robustness of Python functions.

Prerequisite:

Code 1 With Comments:

# Importing a module named mymodule using an alias 'my'
# n is assigned the user-input integer value
# The reversenum function from mymodule is called with n as an argument
import mymodule as my

n = int(input("Enter a number"))
my.reversenum(n)

# Nested functions example
# A function display(s) is defined, which contains a nested function msg()
# The msg function returns "Data Flair"
# display returns the concatenation of the result of msg() and the input string s
s1 = display("Free Courses")
print(s1)

# Function display(f) is defined, which returns the concatenation of "Hello" and the input parameter f
# Another function msg() is defined, which returns the string "Friends"
# The display function is called with the result of msg() as the argument, and the result is stored in x
# Finally, x is printed
def display(f):
    return "Hello" + f

def msg():
    return "Friends"

x = display(msg())
print(x)

Code 1 Output:
HelloFriends

Code 1 Explanation:

Code 2 With Comments:

# Definition of a function named myfactorial that takes an integer parameter n
def myfactorial(n):
    f = 1  # Initializing a variable f to 1
    while n != 0:
        f = f * n
        n = n - 1
    print("Factorial is %d " % f)  # Printing the factorial result

# Definition of a function named reversenum that takes an integer parameter n
def reversenum(n):
    s = 0  # Initializing a variable s to 0
    while n != 0:
        r = n % 10
        s = s * 10 + r
        n = n // 10
    print("Reverse of number is %d " % s)  # Printing the reversed number result

# Note: The code doesn't include any libraries, and the keywords used are standard Python syntax.

Output for Code 2:
Inputs Given:
myfactorial(5)
reversenum(123)

Output based on above input:
Factorial is 120
Reverse of number is 321

Code 2 Explanation:

Code 3 With Comments:

# Definition of a function named addition with default arguments
def addition(a, b=600, c, d=50):
    z = a + b + c + d
    print("Addition is %d" % z)

# Calling the function with specific arguments
addition(1, 2, 3, 4)
addition(1, 2, 3)
addition(1, 2)

Output For code 3:
Addition is 10
Addition is 656
Addition is 653

Code 3 Explanation:

Code 4 With Comments:

# Definition of a function named stud_info with keyword arguments
def stud_info(rollno, name, course):
    print("Roll no is ", rollno)
    print("Name is ", name)
    print("Course is ", course)

# Calling the function with keyword arguments
stud_info(name="Rahul", course="B.Tech", rollno=101)

Output For Code 4:
Roll no is 101
Name is Rahul
Course is B.Tech

Code 4 Explanation:

Code 5 With Comments:

# Definition of a function named addition with variable-length arguments (*args)
def addition(*args):
    for m in args:
        print(m)

# Calling the function with variable-length arguments
addition(10, 20, 30, "Hello", "Data-Flair")

Code 5 Output:
10
20
30
Hello
Data-Flair

Code 5 Explanation:

Conclusion:

Concluding our exploration into Python function arguments, we’ve uncovered the versatility and adaptability they bring to function design. Positional, keyword, default, and variable-length arguments collectively provide a powerful toolkit for handling diverse input scenarios. The ability to tailor function behavior based on different argument types enhances code readability and flexibility. As we reflect on this topic, the understanding of Python function arguments becomes integral to writing efficient and adaptable code, offering a valuable skill set for Python developers.

Exit mobile version