0% found this document useful (0 votes)
40 views5 pages

Practical 7 - User Defined Functions

Uploaded by

ashrafpathan3312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views5 pages

Practical 7 - User Defined Functions

Uploaded by

ashrafpathan3312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PIMPRI CHINCHWAD UNIVERSITY

School of Engineering and Technology

Academic Year:2024-2025 Sem: III


Course: Python Programming Course code: UBTCE203/PCC
Name: Roll No:

Practical 7: User Defined Functions

Title: To write a python program to Create user-defined functions with different types of
function arguments.

Objective:
The objective of this program is to demonstrate the creation and usage of user-defined functions
in Python, highlighting different types of function arguments. These include positional arguments,
keyword arguments, default arguments, and variable-length arguments.

Outcomes:
By the end of this project, users will:
1. Understand how to define and call functions in Python.
2. Learn about different types of function arguments and their uses.
3. Gain practical experience in creating versatile functions that can handle various types of
inputs.

Aim:
The aim of this program is to showcase how to define and utilize user-defined functions with
various types of arguments, providing examples for each type to illustrate their usage and
benefits.

Theory:
Functions are reusable blocks of code that perform a specific task. In Python, functions can have
parameters, which allow them to accept inputs. These inputs can be specified in several ways:
1. Positional Arguments: Arguments passed to a function in the order in which they are
defined.
2. Keyword Arguments: Arguments passed to a function by explicitly stating the parameter
name.
3. Default Arguments: Parameters that assume a default value if a value is not provided.
4. Variable-Length Arguments:
 *args allows a function to accept an arbitrary number of positional arguments.
 **kwargs allows a function to accept an arbitrary number of keyword arguments.

Algorithm:
1. START
2. Define the Functions:
 Create functions with different types of arguments.
3. Demonstrate Positional Arguments:
 Define a function that takes a fixed number of positional arguments.
PIMPRI CHINCHWAD UNIVERSITY
School of Engineering and Technology

Academic Year:2024-2025 Sem: III


Course: Python Programming Course code: UBTCE203/PCC
Name: Roll No:
4. Demonstrate Keyword Arguments:
 Define a function that takes keyword arguments.
5. Demonstrate Default Arguments:
 Define a function with default arguments.
6. Demonstrate Variable-Length Arguments (*args and **kwargs):
 Define functions using *args and **kwargs to handle an arbitrary number of arguments.
7. Call the Functions:
 Call each function with appropriate arguments to demonstrate their functionality.
8. Display Output:
 Show the output of each function call to illustrate the effects of different types of
arguments.
9. END

Source Code:
# Function with Positional Arguments
def positional_example(a, b, c):
return a + b + c

# Function with Keyword Arguments


def keyword_example(name, age, country):
return f"Name: {name}, Age: {age}, Country: {country}"

# Function with Default Arguments


def default_example(name, age=30, country="USA"):
return f"Name: {name}, Age: {age}, Country: {country}"

# Function with Variable-Length Arguments (*args)


def args_example(*args):
return f"Sum of args: {sum(args)}"
PIMPRI CHINCHWAD UNIVERSITY
School of Engineering and Technology

Academic Year:2024-2025 Sem: III


Course: Python Programming Course code: UBTCE203/PCC
Name: Roll No:

# Function with Variable-Length Keyword Arguments (**kwargs)


def kwargs_example(**kwargs):
result = "Information:\n"
for key, value in kwargs.items():
result += f"{key}: {value}\n"
return result

# Main function to demonstrate the examples


def main():
# Positional Arguments
print("Positional Arguments:")
result1 = positional_example(1, 2, 3)
print(f"Result: {result1}\n")

# Keyword Arguments
print("Keyword Arguments:")
result2 = keyword_example(name="John", age=25, country="Canada")
print(f"Result: {result2}\n")

# Default Arguments
print("Default Arguments:")
result3 = default_example(name="Alice")
print(f"Result with default values: {result3}")
result4 = default_example(name="Bob", age=40, country="UK")
print(f"Result with provided values: {result4}\n")
PIMPRI CHINCHWAD UNIVERSITY
School of Engineering and Technology

Academic Year:2023-2024 Sem: II


Course: Advanced C Programming Laboratory Course code: CSE102
Name: Roll No:

# Variable-Length Arguments (*args)


print("Variable-Length Arguments (*args):")
result5 = args_example(1, 2, 3, 4, 5)
print(f"Result: {result5}\n")

# Variable-Length Keyword Arguments (**kwargs)


print("Variable-Length Keyword Arguments (**kwargs):")
result6 = kwargs_example(name="David", age=35, country="Australia",
profession="Engineer")
print(result6)

if __name__ == "__main__":
main()

Output :
Positional Arguments:
Result: 6

Keyword Arguments:
Result: Name: John, Age: 25, Country: Canada

Default Arguments:
Result with default values: Name: Alice, Age: 30, Country: USA
Result with provided values: Name: Bob, Age: 40, Country: UK

Variable-Length Arguments (*args):


Result: Sum of args: 15

Variable-Length Keyword Arguments (**kwargs):


Information:
name: David
age: 35
country: Australia
profession: Engineer
PIMPRI CHINCHWAD UNIVERSITY
School of Engineering and Technology

Academic Year:2023-2024 Sem: II


Course: Advanced C Programming Laboratory Course code: CSE102
Name: Roll No:

Conclusion:
The program successfully demonstrates the use of user-defined functions in Python with various
types of arguments. Users can see how to define and utilize functions with positional, keyword,
default, and variable-length arguments (*args and **kwargs).
This provides a clear understanding of how different types of function arguments can be
used to create flexible and powerful functions, allowing for a wide range of inputs and
functionality.
The program serves as an excellent educational tool for those learning Python function
definitions and argument handling.

You might also like