Functions Notes by Nitin Paliwal
Functions Notes by Nitin Paliwal
NITIN PALIWAL
Page 2
FUNCTIONS
WHAT ARE FUNCTIONS?
A function is a reusable block of code that performs a specific task or set of tasks.
Functions provide a way to organize code into modular and manageable pieces.
They take input, process it, and produce output. Functions are crucial for promoting
code reusability, readability, and maintainability.
TYPES OF FUNCTIONS:
Built-in Functions:
Definition: Functions that are predefined in Python and come built into the
interpreter.
Examples:
len(): Returns the length of an object (e.g., a string, list, or tuple).
print(): Outputs text or variables to the console.
type(): Returns the type of an object.
User-Defined Functions:
Definition: Functions created by the user to perform specific tasks or
operations.
Example:
NITIN PALIWAL
Page 3
FUNCTION BASICS
PARAMETER
ARGUMENT
Default Parameters:
Definition: Assign default values to parameters in the function definition.
If an argument is not provided during the function call, the default value is
used.
DEFAULT PARAMETER
Positional Parameters:
Definition: Parameters defined by their position in the function call.
The order and number of arguments matter.
NITIN PALIWAL
Page 4
# Calculate total canvas area used for making the tent 4. Cost Calculation:
- The program prompts the
canvas_area = csa_conical + csa_cylindrical
user to enter the cost of 1 m² of
print("The area of the canvas is", canvas_area, "m^2") canvas.
- It calculates the total cost of
canvas by multiplying the total
# Prompting the user to enter the cost of 1 m^2 canvas
canvas area by the unit price.
unit_price = float(input("Enter the cost of 1 m^2 canvas: "))
5. Tax Calculation and Net
Amount Payable:
# Calculate total cost of canvas
- Tax is calculated as 18% of
total_cost = unit_price * canvas_area the total cost.
print("The total cost of canvas = ", total_cost) - The net amount payable by
the customer, including tax, is
calculated by adding the tax to
# Adding tax to the total cost to calculate net amount payable by the
the total cost.
customer
tax = 0.18 * total_cost 6. Output:
- The program displays the
net_price = total_cost + tax
area of the canvas, total cost of
print("Net amount payable = ", net_price) canvas, and the net amount
payable by the customer.
NITIN PALIWAL
Page 5
csa_cyl = cyl(h, r) # Function call to calculate cylindrical area - Total canvas area and cost
l = float(input("Enter slant height of the conical area in meters: ")) are calculated based on user
inputs.
csa_con = con(l, r) # Function call to calculate conical area
# Calculate area of the canvas used for making the tent 5. Tax Calculation and Net
canvas_area = csa_cyl + csa_con Amount Payable:
NITIN PALIWAL
Page 6
# Function call
addnum() # Call the function to execute
PASSED AS AN ARGUMENT.
sum = sum + i
print("The sum of first", n, "natural numbers is:", sum)
# Function call
sumSquares(num)
NITIN PALIWAL
Page 8
NITIN PALIWAL
Page 9
NITIN PALIWAL