0% found this document useful (0 votes)
12 views

Week 16 2024 Topic 12 17 Programmers Functions Statement

Computer programming module for 2nd year College for free

Uploaded by

tolorioleodimar2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week 16 2024 Topic 12 17 Programmers Functions Statement

Computer programming module for 2nd year College for free

Uploaded by

tolorioleodimar2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

WMSU Prepared By:

DR. FHELDAURA T. ABUBAKAR

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

• A function is a subprogram that is executed


when it is called from some point of the program,
it will not be executed unless it is called. The
statement used to execute a function is called the
function call.

• A function call is an expression consisting of the


function name followed by the arguments
enclosed in parenthesis. 5
Read and Ponder!
FUNCTION

One of the advantages of using functions to divide


programming task into subtasks is that different
people can work on different subtasks. Also, it is
easier to debug a program with lesser lines of codes
compared to a huge program.

There are two types of functions two namely:


1. Pre-defined function
2. Programmer Defined Function
6
Read and Ponder!
Predefined Function

Python comes with libraries of Predefined functions that


you can use in your program. These Predefined functions
are ready made function and has already been defined.

For more Predefined functions please proceed to this link


https://docs.python.org/3/library/functions.html

7
Read and Ponder!
Predefined Function
Built-in Modules in Python

• The Python interpreter has a number of built-in


functions. They are loaded automatically as the
interpreter starts and are always available.

• For example, print() and input() for I/O, number


conversion functions int(), float(), complex(), data
type conversions list(), tuple(), set(), etc.
8
Read and Ponder!
Predefined Function
Built-in Modules in Python
• In addition to built-in functions, a large number of
pre-defined functions are also available as a part
of libraries bundled with Python distributions.

• These functions are defined in modules.

9
Read and Ponder!
Python - Math Module

• Some of the most popular mathematical functions are


defined in the math module. These include
trigonometric functions, representation functions,
logarithmic functions, angle conversion functions, etc.

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

Another well-known mathematical constant defined in the


math module is e. It is called Euler's number and it is a
base of the natural logarithm. Its value is
2.718281828459045.

12
Read and Ponder!
Programmer Defined Function

• Python allows programmers to create their own


functions. This is used to make the program more
organized and structured. These functions are called
programmer defined functions.

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

• Arguments in Python are passed by reference. Any


changes made with the value of the parameters within
the function, the changes reflects back in the calling
function.

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)

print("The total value of 2 inputted numbers is=", total)21


Read and Ponder!
SAMPLE PROGRAM
print(" MARIO DELA CRUZ")
print("BSEE2a")
import math
def SUM(num1,num2):
total=num1+num2
return(total)

#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

print(" MARIO DELA CRUZ")


print("BSEE2a")
import math

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")

R=float(input("Enter the value of RADIUS="))


AREA2=AREACIRC(R)
print("The AREA of the CIRCLE is=",AREA2,"sq. units")

elif answer=="no":
break
else:
print("Enter yes or no")
23
24
25

You might also like