0% found this document useful (0 votes)
3 views5 pages

Es106 CFP Activity 3 Finals Alu

Uploaded by

cagasmarshemay
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)
3 views5 pages

Es106 CFP Activity 3 Finals Alu

Uploaded by

cagasmarshemay
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/ 5

ES106

Computer Fundamentals and Programming


Module No. 3 (FINALS)
Topic Programming functions, parameter passing, functions calling
parameters using values, recursion, and comparing global variables
to local variables.
Period Week no.: 4 Date: April 14, 2025

Python Programming Functions, Parameter Passing,


Functions calling by Values, Recursion and Global to
Local Variables
Activity 3 (FINALS): Applying knowledge close to course

YOUR QUEST:

For this activity, you will follow, implement and answers instructions and questions
that are listed before and after some activity provide a separate document file and
save it as a pdf file and your scripts upon submission, attached your lab manual with
your activity sheet or you can edit the manual and have your scripts paste it on every
number and submit it in our google classroom. By the end of this Laboratory work
you will be able to:

• Define and use functions in Python Language.


• Differentiate between call value and reference behavior in Python.
• Understand and Implement recursion.
• Differentiate between local and global variables.

YOUR MISSION:

Mission 1: Recalling our Latest Topic on Functions

Write a function/method called greet_yourself() that prints:

Hello, Civil Engineer “Your Name” !


Note: Change “Your Name” to your name

Write the function below:


def greet_yourself() :

#Your code here

Call the function you created:


#Your code here

Write the final output that prints your desired outcome base on the mission.

#Your output here

Created by: Engr. Alnur Mapandi 1


ES106
Computer Fundamentals and Programming

Module No. 3 (FINALS)


Topic Programming functions, parameter passing, functions calling
parameters using values, recursion, and comparing global variables
to local variables.
Period Week no.: 4 Date: April 14, 2025

Python Programming Functions, Parameter Passing,


Functions calling by Values, Recursion and Global to
Local Variables

Mission 2: Parameter Passing (Calling by Value)

Modify the Parameters given below and observe what happen upon your changing the
Values and Parameters. You can do this on Google colab/ Jupyter Notebook/Python3:

def updated_area(length) : #this method will allow you to run your function
length = length + 10 #your original value will be added by 10 and stored at your new Parameter length
#script below will give you an output of what value will result upon your function initiated
print (“This is inside my function, updated length: “, length)

l = 15 #this is your original declared value by default


#script below will give you an output of what value your Parameter l holds.
print(“This is outside my function, original length is: “, l)

#script below will call your defined function and will capture the value of l and used it.
update_area(l)

Observe your output and answer the question below:

1. What is the value of length inside the function?


Ans:

2. What is the value of length outside the function?


Ans:

3. Does the original variable changed outside the function?


Why or Why not ?
Ans:

#Your output here

Created by: Engr. Alnur Mapandi 2


ES106
Computer Fundamentals and Programming

Module No. 3 (FINALS)


Topic Programming functions, parameter passing, functions calling
parameters using values, recursion, and comparing global variables
to local variables.
Period Week no.: 4 Date: April 14, 2025

Python Programming Functions, Parameter Passing,


Functions calling by Values, Recursion and Global to
Local Variables
Mission 3: Recursion

Write this recursive function factorial(n) in the same platform what you use in mission
2 and run it to compute the factorial of a number n.

def factorial(n) : #this method will allow you to run your function
if n == 0: #this function is example of a if statement that is previously discussed
return 1 #this will give you an output n = 1 when your first statement will be true
#this is an example of a recursive case
else: #this statement will be initiated if your previous function is false
return n * factorial(n-1) #this will give you rerunning the previous function if n < 1.

# script below will give you an output of what value your Parameter n holds.
print (“Factorial of 5 is: “, factorial(5))

What is the base case for this function?


Ans:

What happens if there is no base case?


Ans:

Mission 4: Local and Global Variables

Write the function below to the same platform you use previously and run the scripts
and observe how the output look like.

kwarta = 36 #this will be your declared value for your declared parameter

def sulod_pitaka() : #this will be your function


kwarta = 136
# script below will give you an output of what value your Parameter kwarta holds
print (“Sulod sa pitaka:”, kwarta)

sulod_pitaka() #this method calling will allow you to run your function
# script below will give you an output of what value your Parameter kwarta holds
print (“Akong kwarta gawas pitaka:”, kwarta)

Created by: Engr. Alnur Mapandi 3


ES106
Computer Fundamentals and Programming

Module No. 3 (FINALS)


Topic Programming functions, parameter passing, functions calling
parameters using values, recursion, and comparing global variables
to local variables.
Period Week no.: 4 Date: April 14, 2025

Python Programming Functions, Parameter Passing,


Functions calling by Values, Recursion and Global to
Local Variables
What is the value of kwarta inside the function ?
Ans:

What is the value of kwarta outside the function ?


Ans:

Now try modifying your function to make your parameter kwarta global:

kwarta = 36 #this will be your declared value for your declared parameter

def sulod_pitaka() : #this will be your function


global kwarta
kwarta = 136
# script below will give you an output of what value your Parameter kwarta holds
print (“Sulod sa pitaka:”, kwarta)

sulod_pitaka() #this method calling will allow you to run your function
# script below will give you an output of what value your Parameter kwarta holds
print (“Akong kwarta gawas pitaka:”, kwarta)

#Your output here

Now by observing the output what changed?


Ans:

Mission 5: Engineering Application

By applying what we already learn, we will now compute the area of a rectangle. You
can define a function compute_area(length, width) that returns the area. Then write a
recursive function to simulate subdividing a rectangle into halves until the area is less
than a threshold.

def compute_area(length, width) : #this will be your function in computing the triangle area
return length * width

def recursive_subdivision(length, width) : #this will be your recursive function


area = compute_area(length, width) #this will create and store value to area parameter

Created by: Engr. Alnur Mapandi 4


ES106
Computer Fundamentals and Programming

Module No. 3 (FINALS)


Topic Programming functions, parameter passing, functions calling
parameters using values, recursion, and comparing global variables
to local variables.
Period Week no.: 4 Date: April 14, 2025

Python Programming Functions, Parameter Passing,


Functions calling by Values, Recursion and Global to
Local Variables
print (f’Current Area: {area}’) #this output the current area that is on function
if area <= 5: #this statement will allow you to continue the function until area is reduced to less than 5
return
#after displaying the Area it will now have the length and width division by 2 then repeat the area function
recursive_subdivision(length / 2, width / 2)

#this will call and initiate your function to start calculating


recursive_subdivision(36, 67)

By observing the output and modifying it in your own to fully understand, How does
the recursive subdivision help in engineering analysis like material stress testing or
flood zoning or state any real world engineering practice in your course that recursive
help.

Ans:

Conclusion with the Activity:

With the activity that you have done, list at least three statement what did you learn
and realize:

Submitted by:
Course and Section:
Instructor:
Date Submitted:

Created by: Engr. Alnur Mapandi 5

You might also like