0% found this document useful (0 votes)
2 views4 pages

Week 3 - Python LAQ Assignment

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)
2 views4 pages

Week 3 - Python LAQ Assignment

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

Functions in Python

Introduction
A function in Python is a block of organized, reusable code that is used to
perform a specific task. Functions help in breaking a large program into smaller,
manageable parts, making the code more readable, reusable, and maintainable.

1. Function Definition
In Python, a function is defined using the def keyword, followed by the function
name and parentheses.
Syntax:
def function_name(parameters):
# function body
return value
Example:
def greet():
print("Hello, Welcome to Python!")

greet()
Output:
Hello, Welcome to Python!

2. Function Arguments
Arguments are the values passed to a function when it is called. Python supports
different types of arguments.

a) Positional Arguments
These arguments are passed in the correct positional order.
def add(a, b):
print(a + b)

add(5, 3)
Output:
8

b) Keyword Arguments
Arguments are passed using parameter names, so order does not matter.
def display(name, age):
print("Name:", name)
print("Age:", age)

display(age=21, name="John")
Output:
Name: John
Age: 21

c) Default Arguments
A default value is assigned to parameters. If no value is provided, the default is
used.
def greet(name="Guest"):
print("Hello", name)

greet()
greet("Alice")
Output:
Hello Guest
Hello Alice

d) Variable-Length Arguments
Used when the number of arguments is not fixed.
*i) args (Non-keyword variable arguments)
def add_numbers(*args):
print(sum(args))

add_numbers(1, 2, 3, 4)
Output:
10
**ii) kwargs (Keyword variable arguments)
def display_info(**kwargs):
for key, value in [Link]():
print(key, ":", value)

display_info(name="John", age=25)
Output:
name : John
age : 25
3. Return Statement
The return statement is used to send a value back from a function to the caller.
def square(x):
return x * x

result = square(4)
print(result)
Output:
16
A function can return multiple values:
def operations(a, b):
return a + b, a * b

sum_val, mul_val = operations(2, 3)


print(sum_val, mul_val)
Output:
56

4. Scope of Variables
Scope refers to the region where a variable is accessible.
a) Local Scope
Variables defined inside a function are accessible only within that function.
def my_function():
x = 10
print(x)

my_function()

b) Global Scope
Variables defined outside all functions are accessible throughout the program.
x = 20

def show():
print(x)

show()
c) Using Global Keyword
To modify a global variable inside a function, use the global keyword.
x=5

def change():
global x
x = 10

change()
print(x)
Output:
10

5. Importance of Functions
 Promotes code reusability
 Makes programs modular and organized
 Improves readability and debugging
 Reduces code duplication

Conclusion
Functions are a fundamental concept in Python programming. They help in
structuring code efficiently by dividing tasks into smaller units. Understanding
function definitions, arguments, return values, and scope is essential for writing
effective and scalable Python programs.

You might also like