- What is a function?
- A function is a block of code that performs a specific task.
- It can take inputs, perform operations, and return outputs.
- It helps in reusability of code and makes the code modular.
- Defining a function:
- A function is defined using the
def
keyword followed by the function name and parameters in parentheses. - The code block inside the function is indented.
- A function is defined using the
- Parameters and arguments:
- Parameters are the variables defined in the function header.
- Arguments are the values passed to the function when it is called.
- Return statement:
- The
return
statement is used to return a value from the function. - If the
return
statement is omitted, the function returnsNone
.
- The
- Calling a function:
- A function is called by using its name followed by parentheses and the arguments.
- The returned value can be stored in a variable.
- Default parameters:
- Default parameters have a default value, which is used if no argument is provided.
- They are defined using the equals sign after the parameter name.
- Keyword arguments:
- Keyword arguments are used to pass arguments to a function using the parameter name.
- They are defined using the parameter name followed by the equals sign and the argument value.
- Variable scope:
- Variables defined inside a function have local scope and are not accessible outside the function.
- Variables defined outside a function have global scope and are accessible inside the function.
- Lambda functions:
- Lambda functions are anonymous functions that are defined using the
lambda
keyword. - They are useful for creating small, one-line functions.
- Lambda functions are anonymous functions that are defined using the
- Recursion:
- Recursion is a technique where a function calls itself.
- It can be useful for solving problems that can be broken down into smaller sub-problems.
Examples
Simple function:
Define a function that takes two numbers as arguments and returns their sum. Call the function with different arguments to show how it works.
def add_numbers(a, b): return a + b # Run the function print(add_numbers(5, 3)) print(add_numbers(2, 7))
Default parameters:
Define a function that takes two numbers as arguments and returns their product. Set the default value of the second parameter to 1. Call the function with different arguments to show how it works.
def multiply_numbers(a, b=1): return a * b # Run the function print(multiply_numbers(5)) print(multiply_numbers(2, 7))
6.2 How to define a function¶
- Start a functon with def
- Write name of the function
- Add parentheses
( )
- Write the parameters (variables that the function will recieve)
- Add
:
- Make sure that the code starts as indented block on the next line
Example 1: Add numbers¶
Create a function for sum of two numbers
In [1]:
def add_numbers(a, b):
return a+b
In [2]:
# Call this function
add_numbers(123, 345)
Out[2]:
Example 2: Average¶
Create a function that finds average of two numbers
In [3]:
def average(a, b):
return (a+b)/2
In [4]:
# Call average function
average(345, 633)
Out[4]:
Example 3: Average of list¶
Define avg_list that will accept a list and return its average
In [5]:
def avg_list(z) :
return sum(z) / len(z)
In [7]:
# call avg_list function
# First create a list
marks = [65,67,98,90,45,89,87,99]
# Find average of the marks list
avg_list(marks)
age = [23,17,25,34,65,27]
avg_list(age)
# See we have reused the same function avg_list
Out[7]:
6.3 Default values¶
We can add default values with the symbol =
Example 4: Product¶
Define a function for product of two numbers The second parameter (variable) should have a default value of 1
In [10]:
def multiply (a, b = 1) :
return a * b
In [11]:
# Call with two values
multiply(30, 20)
Out[11]:
In [12]:
# Since there is a default value for the second parameter
# We can run this function with one parameter
multiply(30)
Out[12]:
Example 5: Product of list¶
Define a function product_list, it should accept list and return the product of its values
In [27]:
def product_list(z):
product = 1
# loop through each element of the list
for i in range(0, len(z)) :
product = z[i] * product
return product
In [28]:
# Call this function
mylist = [10,2,5,3,2]
product_list(mylist)
Out[28]:
Example 6: Add value of list¶
In [29]:
def sum_list(z):
sum = 0
for i in range(0, len(z)) :
sum = sum + z[i]
return sum
In [30]:
sum(mylist)
Out[30]: