0% found this document useful (0 votes)
273 views9 pages

Functions Notes by Nitin Paliwal

Uploaded by

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

Functions Notes by Nitin Paliwal

Uploaded by

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

Page 1

NITIN PALIWAL
Page 2

CLASS 11 AND 12 COMPUTER SCIENCE

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.

Functions Defined in Modules:


 Definition: Functions that are part of external modules or libraries, extending
Python's functionality.
 Examples:
 math.sqrt(): Returns the square root of a number (from the math
module).
 random.randint(): Generates a random integer (from the random
module).

User-Defined Functions:
 Definition: Functions created by the user to perform specific tasks or
operations.
 Example:

NITIN PALIWAL
Page 3

FUNCTION BASICS

Arguments and Parameters:


 Parameters: Variables listed in the function definition.
 Arguments: Values passed to the function during a function call.
 Parameters act as placeholders for arguments.

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

PROGRAM TO CALCULATE THE PAYABLE

AMOUNT FOR THE TENT WITHOUT USING

FUNCTIONS K2 (KYA, KAISE)


# Program to calculate the payable amount for the tent without 1. User Inputs:
functions - The program prompts the
user to enter values for the
cylindrical part of the tent:
# Prompting the user to enter values for the cylindrical part of the tent
height, radius, and slant height
print("Enter values for the cylindrical part of the tent in meters\n") of the conical part.
h = float(input("Enter height of the cylindrical part: "))
2. Area Calculation:
r = float(input("Enter radius: "))
- The program calculates the
l = float(input("Enter the slant height of the conical part in meters: ")) area of the conical part and
cylindrical part separately using
mathematical formulas.
# Calculating the area of the conical part
csa_conical = 3.14 * r * l 3. Total Canvas Area:
- The total canvas area used
# Calculating the area of the cylindrical part for making the tent is calculated
by adding the areas of the
csa_cylindrical = 2 * 3.14 * r * h conical and cylindrical parts.

# 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

PROGRAM TO CALCULATE THE PAYABLE

AMOUNT FOR THE TENT USING FUNCTIONS


K2 (KYA, KAISE)
# Program to calculate the cost of a tent
# Function definition for calculating the area of the cylindrical part
def cyl(h, r): 1. Function Definitions:
- Three functions are defined:
area_cyl = 2 * 3.14 * r * h # Area of cylindrical part
`cyl(h, r)`, `con(l, r)`, and
return area_cyl `post_tax_price(cost)`.
- They compute the area of
# Function definition for calculating the area of the conical part the cylindrical part, conical part,
and net payable amount
def con(l, r): including tax, respectively.
area_con = 3.14 * r * l # Area of conical part
return area_con 2. User Inputs:
- Height, radius, and slant
height of the tent are entered by
# Function definition for computing payable amount for the tent the user.
def post_tax_price(cost):
tax = 0.18 * cost
3. Function Calls:
net_price = cost + tax
return net_price - `cyl()` and `con()` are called
to calculate respective areas.

print("Enter values of cylindrical part of the tent in meters:")


h = float(input("Height: ")) 4. Canvas Area and Cost
r = float(input("Radius: ")) Calculation:

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:

print("Area of canvas = ", canvas_area, " m^2") - Tax is computed as 18% of


the total cost and added to get
the net payable amount.
# Calculate cost of canvas
unit_price = float(input("Enter cost of 1 m^2 canvas in rupees: "))
6. Output:
total_cost = unit_price * canvas_area
- The program displays the
print("Total cost of canvas before tax = ", total_cost) area of the canvas, total cost
before tax, and net amount
payable including tax.
# Calculate and print net amount payable (including tax)
print("Net amount payable (including tax) = ",
post_tax_price(total_cost))

NITIN PALIWAL
Page 6

WRITING A USER DEFINED FUNCTION TO ADD 2 NUMBERS AND

DISPLAY THEIR SUM


1. User Input:
 Accepts two numbers from the user.
# Function to add two numbers 2. Sum Calculation:
# The requirements are listed below:  Calculates the sum of the two numbers.
3. Output:
# 1. Accept 2 numbers from the user.
 Displays the sum of the numbers.
# 2. Calculate their sum.
# 3. Display the sum. Concise Explanation:

 The function addnum() prompts the user to input two


# Function definition numbers, calculates their sum, and then displays the
result. It accomplishes these tasks in a single function call.
def addnum():
fnum = int(input("Enter first number: ")) # Accept first number from the user
snum = int(input("Enter second number: ")) # Accept second number from the user
sum = fnum + snum # Calculate the sum
print("The sum of ", fnum, "and ", snum, "is ", sum) # Display the sum

# Function call
addnum() # Call the function to execute

WRITE A PROGRAM USING A USER DEFINED FUNCTION THAT

DISPLAYS SUM OF FIRST N NATURAL NUMBERS, WHERE N IS

PASSED AS AN ARGUMENT.

# Program to find the sum of first n natural numbers


# The requirements are: K2 (KYA, KAISE)
# 1. n be passed as an argument
Parameter Passing:
# 2. Calculate sum of first n natural numbers The function accepts n as a parameter.

# 3. Display the sum Sum Calculation:


It calculates the sum of the first n natural numbers using
a loop.
# Function definition Output:
The function displays the sum of the first n natural
def sumSquares(n): # n is the parameter numbers.
Concise Explanation:
sum = 0 The sumSquares() function takes an integer n as input
and calculates the sum of the first n natural numbers
for i in range(1, n+1): using a loop. The function then prints the sum. Finally,
the program prompts the user to input a value for n and
calls the sumSquares() function with the user-provided
value.
NITIN PALIWAL
Page 7

sum = sum + i
print("The sum of first", n, "natural numbers is:", sum)

# Getting user input for n


num = int(input("Enter the value for n: "))

# Function call
sumSquares(num)

NITIN PALIWAL
Page 8

NITIN PALIWAL
Page 9

NITIN PALIWAL

You might also like