Lesson 6
Lesson 6
n Creative Technologies:
Modular Programming
1 (Functions)
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
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:
Steps:
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:
4. Each student in the group becomes responsible for one of these steps, just
like a function in programming.
o List the specific instructions for that step (e.g., "Set up bamboo posts,
attach horizontal beams, ensure structure is level")
Guide Questions:
1. How did breaking down of the bahay kubo building process into steps (functions)
make it easier and more efficient?
_____________________________________________________________________________________
___
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_________
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!
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!
Examples of Functions
1. Simple Function without Parameters or Return Value
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
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!)
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%)
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.
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