Week 16 2024 Topic 12 17 Programmers Functions Statement
Week 16 2024 Topic 12 17 Programmers Functions Statement
1
FUNCTIONS
2
Read and Ponder!
Introduction to Functions
• After completing the previous sections, you are now equipped with
the basics of Python programming. You know that programs are
created to solve problems. So far, you only created small programs
that solve a specific problem.
• However, most of the programs such as websites or games
composed of smaller programs that are joined and integrated to
create one huge application. It sounds complicated, right? And
maybe you can already imagine thousands lines of codes.
• In most cases, these huge applications are divided into chunks for
easy programming and management not just by a single
programmer but also for a group of programmers. How? Well,
easy… functions! 3
Read and Ponder!
Introduction to Functions
• Let’s take a look at your own television sets. Do you know how
televisions are manufactured? Production of television consists of
several lines (of production) and each line is assigned for a special
task. At the end of each production line, outputs are being checked
before proceeding to the next production line. This is done to
assure that the unit is functional.
• In programming, you can also create programs chunk by chunk.
Each chunk is a part of a huge program. Each chunk is being tested
of its functionality before embedding to the huge program. These
chunks are called functions. This programming technique is called
the modular approach. 4
Read and Ponder!
FUNCTION
7
Read and Ponder!
Predefined Function
Built-in Modules in Python
9
Read and Ponder!
Python - Math Module
10
Read and Ponder!
Python - Math Module
• Some of the functions includes log(), exp(), pow(),
sqrt(), ceil() and floor().
• In addition, two mathematical constants are also
defined in this module.
• Pie (π). Here, you will be able to enhance your previous
programs by implementing variable) is a well-known
mathematical constant, which is defined as the ratio of
the circumference to the diameter of a circle and its
value is 3.141592653589793. 11
Read and Ponder!
Python - Math Module
12
Read and Ponder!
Programmer Defined Function
13
Read and Ponder!
Programmer Defined Function
• Functions are declared at the beginning of the program
code.
• Function definition begins with the header, composed of
the word def followed by the function name then with
the arguments(optional) that are within a parentheses.
• Then there should always be a block with the code that
the function is to execute. Any of the arguments passed
to the function can be used as if they were declared in
the block 14
Read and Ponder!
Programmer Defined Function
15
Read and Ponder!
Parameters and arguments
16
Read and Ponder!
SAMPLE PROGRAM
SAMPLE PROGRAM 1
Write a program that inputs two integer values and
outputs the sum, difference, quotient, product. The
program defines a function with 4 parameters,
performs the addition of two integers and returns
the sum, difference, quotient, product.
17
Read and Ponder! SAMPLE PROGRAM
PART 1 PART 2
#PROGRAMMER DEFINED #MAIN PROGRAM
FUNCTIONS print("YOUR NAME")
print("course yr and section")
def SUM(num1, num2):
add=num1+num2 print("OUTPUT THE SUM, PRODUCT, QUOTIENT,
return (add) DIFFERENCE")
A=int(input("ENTER the VALUE of A="))
def PRODUCT(numb1, numb2): B=int(input("ENTER the VALUE of B="))
prod=numb1*numb2 total=SUM(A,B)
return (prod) product=PRODUCT(A,B)
difference= DIFFERENCE(A,B)
def DIFFERENCE(a, b): quotient=QUOTIENT(A,B)
diff=a-b print(" the Total of the two inputted integer is =",
return (diff) total)
print(" the product of the two inputted integer is =",
def QUOTIENT(x, y): product)
quo=x/y print(" the difference of the two inputted integer is
return (quo) =", difference)
print(" the quotient of the two inputted integer
18 is =",
quotient)
Read and Ponder! SAMPLE PROGRAM
MAIN PROGRAM
while True:
answer=input("Do you want to compute for the HYPOTENUSE OF THE
def SUM(num1, num2): TRIANGLE [yes/no]:")
add=num1+num2 if answer=="yes":
return (add) print("YOUR NAME")
print("course yr and section")
def PRODUCT(numb1, numb2):
prod=numb1*numb2 print("OUTPUT THE SUM, PRODUCT, QUOTIENT, DIFFERENCE")
return (prod) A=int(input("ENTER the VALUE of A="))
B=int(input("ENTER the VALUE of B="))
def DIFFERENCE(a, b): total=SUM(A,B)
diff=a-b product=PRODUCT(A,B)
return (diff) difference= DIFFERENCE(A,B)
quotient=QUOTIENT(A,B)
def QUOTIENT(x, y): print(" the Total of the two inputted integer is =", total)
quo=x/y print(" the product of the two inputted integer is =", product)
return (quo) print(" the difference of the two inputted integer is =", difference)
print(" the quotient of the two inputted integer is =", quotient)
elif answer=="no":
break
else:
print("Enter yes or no") 19
Read and Ponder!
SAMPLE PROGRAM
print(" MARIO DELA CRUZ")
print("BSEE2a")
def SUM(num1,num2):
total=num1+num2
return(total)
#MAIN PROGRAM
A=float(input("Enter the first number="))
B=float(input("Enter the first number="))
totalSUM=SUM(A,B)
print("The SUM of the 2 inputted integer", A,"and", B, "is =",totalSUM)
20
Read and Ponder!
SAMPLE PROGRAM
def SUM(a,b):
x=a+b
return(x)
A=float(input("\nEnter A="))
B=float(input("\nEnter B="))
total=SUM(A,B)
#MAIN PROGRAM
while True:
answer=input("Do you want to compute for the HYPOTENUSE OF THE TRIANGLE [Y/N]:")
if answer=="y" or answer=="Y":
num=int(input("\nHow many Computations to make?"))
for cntr in range (0, num):
A=float(input("Enter the first number="))
B=float(input("Enter the first number="))
totalSUM=SUM(A,B)
print("The SUM of the 2 inputted integer", A,"and", B, "is =",totalSUM)
continue
elif answer=="no" or answer=="N":
print("Enter yes or no")
break
else:
print("TERMINATED PROGRAM")
22
Read and Ponder!
SAMPLE PROGRAM
def AREATRI(BASE,HEIGHT):
AT=0.5*BASE*HEIGHT
return(AT)
def AREACIRC(RADIUS):
pi=3.1416
AC=pi*(RADIUS**2)
return(AC)
#MAIN PROGRAM
while True:
answer=input("Do you want to compute for the HYPOTENUSE OF THE TRIANGLE [yes/no]:")
if answer=="yes":
B=float(input("Enter the BASEr="))
H=float(input("Enter the HEIGHT="))
AREA1=AREATRI(B,H)
print("The AREA of the TRIANGLE is=",AREA1,"sq. units")
elif answer=="no":
break
else:
print("Enter yes or no")
23
24
25