0% found this document useful (0 votes)
10 views12 pages

Lesson 6

The document introduces modular programming and functions in computer science, using the analogy of building a LEGO structure to explain how breaking down complex tasks into smaller, manageable parts makes programming easier. It includes activities for students to apply these concepts, such as building a bahay kubo and creating a simple calculator program in C. The document emphasizes the importance of functions for code organization, reusability, and simplicity.

Uploaded by

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

Lesson 6

The document introduces modular programming and functions in computer science, using the analogy of building a LEGO structure to explain how breaking down complex tasks into smaller, manageable parts makes programming easier. It includes activities for students to apply these concepts, such as building a bahay kubo and creating a simple calculator program in C. The document emphasizes the importance of functions for code organization, reusability, and simplicity.

Uploaded by

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

Lesso

n Creative Technologies:
Modular Programming
1 (Functions)

Imagine you're building a massive LEGO structure. Instead of trying to


construct the entire thing at once, you'd probably break it down into smaller,
manageable parts. You might create the base first, then add walls, then a roof, and
so on. Each of these parts could be built separately and then combined to form the
whole structure.

This is exactly what modular programming is all about in the world of computer
science!

As you dive into the world of programming, functions will become your best
friends.

They'll help you organize your code, make complex tasks manageable, and save
you time and effort in the long run. Happy coding!

What’s In

Using the word bank below, complete the statements by filling in the blank spaces.

Function Modular
Variable Parameters Argument
Call Return

1. A __________ is a block of code that performs a specific task and can be reused
throughout a program.
2. When defining a function, you can specify inputs known as __________, which allow
you to pass data into the function.
3. The value that a function sends back to the part of the program that called it is
known as the __________ value.
4. __________ programming involves breaking down a program into smaller,
manageable pieces or modules.
5. A __________ is used to store data that can be referenced and manipulated within
a program.

What’s New

Activity 1: “Bahay-Kubo Assembly Line”

Today, we're going to learn about a powerful concept in programming called


"functions" through a fun and culturally relevant activity - building a bahay kubo!

Story

Imagine you're part of a team tasked with building bahay kubos for a new
eco-friendly resort. The resort needs many identical bahay kubos built quickly and
efficiently. Your team leader decides to use an "assembly line" approach, where
each team member is responsible for a specific part of the building process. This
way, bahay kubos can be constructed faster and with consistent quality.

Materials needed:

● Paper and markers/pens

● Pictures of bahay kubo parts

Steps:

1. You will be divided into groups of 4-5 students.

2. Each group will create their own "Bahay Kubo Construction Company" with a
design for a standard bahay kubo.

3. Break down the bahay kubo building process into 4-5 main steps indicated
below:

o Build the foundation

o Construct the frame

o Add walls and windows

o Install the nipa roof

o Add finishing touches

4. Each student in the group becomes responsible for one of these steps, just
like a function in programming.

5. Create "function cards" for each step:


o Write the step name at the top (e.g., "Construct the frame")

o List the specific instructions for that step (e.g., "Set up bamboo posts,
attach horizontal beams, ensure structure is level")

o Pictures will be attached or associate them with the card.

6. Practice "building" bahay kubos by passing an imaginary plot through the


line, with each student explaining their step.

7. Introduce "parameters" by having a client (another student or the teacher)


request specific features (e.g., "Larger windows" or "Extra room"), and see
how the line adapts.

Guide Questions:

1. How did breaking down of the bahay kubo building process into steps (functions)
make it easier and more efficient?
_____________________________________________________________________________________
___

_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_________

2. How is this similar to writing a computer program?


____________________________________

_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_________

3. What happens when you need to make a small change (like adding larger
windows)? Isn't it easier to change just one "function" rather than the whole
process?

_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
____________
What is It

Hey there, Grade 8 coders! Today we're going to talk about something
super cool in programming: modular programming and functions. Don't worry if
these words sound big – we'll break them down together!

What is Modular Programming?


Imagine you're building a huge LEGO castle. Would you try to build the
whole thing at once? Probably not! You'd likely build it in parts – maybe the walls
first, then the towers, then the drawbridge. This is exactly what modular
programming is all about.
Modular programming means breaking a big program into smaller,
manageable pieces called modules. Each module has a specific job to do, just
like each part of your LEGO castle.

What are Functions?


Now, let's talk about functions. In programming, a function is like a mini-
program within your main program. It's a set of instructions that perform a
specific task.
Think of functions as your favorite kitchen appliances:
-a blender has one job: to blend things.
-a toaster has one job: to toast bread.
-a microwave has one job: to heat food quickly.

Each of these appliances does its job when you press a button. In
programming, when you want a function to do its job, you "call" it – kind of like
pressing that button!

In C programming, a function is a block of code that performs a specific


task. Functions help organize code, promote reusability, and make programs
easier to understand and maintain.
return_type function_name(parameter1_type parameter1_name, ...) {
// Function body
// Code to perform the task return value;
// Optional, depends on return_type
}
Function Structure
A typical C function has the following structure:
⮚ return_type: The type of value the function returns (e.g., int, float,
char, void)
⮚ function_name: A descriptive name for the function
⮚ parameters: Input values the function needs to perform its task
(optional)

Examples of Functions
1. Simple Function without Parameters or Return Value

This example shows a simple function greet() that prints a message.


It has no parameters and doesn't return a value (void).

2. Function With Parameters

This function takes a parameter (name) and uses it in the greeting.


3. Function With Return Value
This function takes two integers as parameters, adds them, and returns the
result.

How Do Functions Work?


Let's use a simple example. Imagine you're writing a program to help you
get ready for school. You might have functions like:

Now, when you want to use these functions in your main program, you can
just call them:
Why Use Functions?
1. Reusability: You can use the same function many times without writing the
instructions again and again.
2. Organization: Functions help keep your code neat and tidy.
3. Simplicity: Big problems become smaller, manageable tasks.
4. Flexibility: You can easily change one function without messing up the whole
program.

Remember, just like how different LEGO pieces snap together to make an
awesome creation, different functions work together to create an amazing
program!

Now you're ready to start thinking about how to break down big coding
problems into smaller, function-sized pieces. Happy coding!

Remember These:

Modular Programming:
Breaking a big program into smaller, manageable pieces
called modules
Each module has a specific job, like parts of a LEGO
castle
Function:
A mini-program within your main program
A set of instructions that perform a specific task
Like kitchen appliances (blender, toaster, microwave)
Function Call:
The act of using a function in your program
Similar to pressing a button on a kitchen appliance
Function Definition:
The code that describes what a function does
Includes the function name, parameters (if any), and the
task it performs
What’s More

Activity 2: "Function Factory"


In this activity, you will create a program that acts as a simple calculator
using functions. You'll implement various mathematical operations as separate
functions and then use them in a main program.

Objective: To reinforce understanding of functions in modular programming


through practical application.
Materials Needed:
● A C compiler
(https://www.programiz.com/c-programming/online-compiler/)
(https://onecompiler.com/c)
(https://nextleap.app/online-compiler/c-programming)

● Paper and pen for planning

Function Implementation
Implement the following functions:
1. add(int a, int b): Returns the sum of two integers.
2. subtract(int a, int b): Returns the difference between two integers.
3. multiply(int a, int b): Returns the product of two integers.
4. divide(float a, float b): Returns the quotient of two floats. (Remember to
handle division by zero!)
5. power(int base, int exponent): Returns the result of raising 'base' to the
power of 'exponent'.
6. factorial(int n): Returns the factorial of a non-negative integer. (Use recursion
for this function!)

Bonus Challenge (Optional):


If you finish early or want an extra challenge, try implementing one or more of these
additional functions:
● square_root(float n): Returns the square root of a number.

● logarithm(float n, float base): Returns the logarithm of a number with the


specified base.
● sin(float angle): Returns the sine of an angle (in radians).

Evaluation Criteria:
1. Correct implementation of all required functions (50%)
2. Proper use of modular programming principles (20%)
3. Error handling and input validation (10%)
4. Code organization and comments (10%)
5. Successful execution of the main program (10%)

Good luck and enjoy your Function Factory!


What I Have Learned

Complete the following statements and questions to reflect on what you've


learned about functions in modular programming.

Fill in the Blanks

1. Modular programming is a technique that emphasizes separating the


functionality of a program into ____________, ____________ modules.

2. The two main types of functions in C programming are ____________ functions


and ____________ functions.

3. A function ____________ specifies the function's name, parameters, and return


type, but doesn't contain the function body.

4. The three main parts of a user-defined function are: a)


________________________ b) ________________________ c) ________________________

5. In the function call result = add(5, 3);, the numbers 5 and 3 are called
____________ parameters.

True or False: Mark each statement as True (T) or False (F). If false, briefly explain
why.

6. ____ Functions always need to have parameters.


Explanation (if false): _________________________________________________

7. ____ The main() function is a user-defined function.

Explanation (if false): _________________________________________________

8. ____ A function can return multiple values at once in C.

Explanation (if false): _________________________________________________

9. ____ Modular programming makes it easier for multiple programmers to work


on the same project.

Explanation (if false): _________________________________________________

10.____ In C, you must always declare a function before using it.

Explanation (if false): _________________________________________________


What I Can Do

Activity 3: "Daily Life Function Challenge"


Objective: Identify and create functions based on everyday tasks, demonstrating
your understanding of modular programming concepts.
Activity Description:
1.Real-life Task Identification: List 5-10 daily activities they perform regularly (e.g.,
making breakfast, getting ready for school, doing laundry).
2. Task Breakdown: For each activity, you should break it down into smaller,
repeatable steps.
3. Function Creation: Create C functions for at least three of these activities. Each
function should:
● Have a descriptive name
● Include parameters where appropriate
● Use print statements to simulate the steps of the task
● Return a value if applicable
4.Main Program: Write a main program that calls these functions in a logical order
to simulate a typical day.

Rubric:
Needs Improvement
Criteria Excellent (3) Satisfactory (2)
(1)
Task Identified at least 5 daily Identified 3-4 daily Identified fewer than 3
Identification activities and broke them activities with some activities or steps are
& Breakdown down into clear steps breakdown into steps unclear
Created 3 or more well-
Created 2-3 functions Created fewer than 2
structured C functions with
Function with minor issues in functions or functions
appropriate names,
Creation structure, naming, or have major structural
parameters, and print
parameters issues
statements
Main Program Main program effectively Main program calls Main program is
calls all functions in a most functions with incomplete or doesn't
Needs Improvement
Criteria Excellent (3) Satisfactory (2)
(1)
logical order minor logical issues call functions logically
Code is disorganized
Code is well-organized and Code is mostly
Code Quality or lacks creativity in
creatively represents daily organized with some
& Creativity representing daily
activities creative elements
activities

You might also like