Unit 4 Python Functions
Unit 4 Python Functions
Creating a Function
Example
def my_function():
print("Hello from a function")
EXAMPLE
ef add(num1: int, num2: int) -> int:
"""Add two numbers"""
num3 = num1 + num2
return num3
# Driver code
num1, num2 = 5, 15
ans = add(num1, num2)
print(f"The addition of {num1} and {num2} results
{ans}.")
Output:
The addition of 5 and 15 results 20.
Passing Arguments:
Functions can also accept input in the form of arguments.
Arguments are values or variables that are passed into the
function to be used during its execution. To pass arguments to
a function, you simply include them in the parentheses when
calling the function. For example, if you have a function that
takes two arguments called "add_numbers," you would call it
like this: "add_numbers(3, 4)". In this case, the function would
add 3 and 4 together and return the result.
numbers = [1, 2, 3, 4]
print(list(doubled_numbers))
# Output: [2, 4, 6, 8]
def myfunc(n):
return lambda a : a * n
Anonymous Functions
• Lambda functions
• Lambda expressions
• Lambda abstractions
• Lambda form
Creating a Function
Example
def my_function():
print("Hello from a function")
• Formal parameters
• Actual parameters
Syntax
• def function_name(formal_parameters): #function
definition
• #statements
•
• function_name(actual_parameters) #function call
• Formal parameters
• The parenthesis of function definition consists of variables
known as formal parameters. The information of the actual
parameters gets copied into these formal parameters
when a function gets called.
• EXAMPLE
• def add(a,b): #function definition
• return a+b
• Actual parameters
In python, the actual parameters are also known as Arguments.
The arguments are the data or objects specified during a
function call. It passes the data to the function where the formal
parameters capture the data and copies into them.
• Default arguments
• Required arguments
• Keyword arguments
• Arbitrary arguments
Let us understand them clearly in an easy way.
result = add_numbers(3, 4)
print(result)
# Output: 7
4.2.2 Calling non-value returning function
To call a non-value returning function in Python, you can follow
these steps:
```
def say_hello(name):
print("Hello, " + name + "!")
```
2. Call the function: To call the function, write its name followed
by parentheses with any required arguments inside them.
For example, you can call the "say_hello" function like this:
```
say_hello("Alice") # Output: Hello, Alice!
```
Example
def add(a,b=5,c=10):
return (a+b+c)
Required arguments
Local variables are those that are initialized within a function and are
unique to that function. It cannot be accessed outside of the function.
Let’s look at how to make a local variable.
def f():
# local variable
s = "I love Geeksforgeeks"
print(s)
# Driver code
f()
Python Global variables
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.
# Global scope
s = "I love Geeksforgeeks"
f()
Now suppose a variable with the same name is defined inside the
scope of the function as well then it will print the value given inside
the function only and not the global value.
EXAMPLE
# Global scope
s = "I love Geeksforgeeks"
f()
print(s)