STE Computer Programming Q4 MODULE 1 2
STE Computer Programming Q4 MODULE 1 2
Computer
Programming
Quarter IV – Module 1:
Create different Types of Function
Republic Act 8293, section 176 states that: No copyright shall subsist in any work
of the Government of the Philippines. However, prior approval of the government agency or
office wherein the work is created shall be necessary for exploitation of such work for profit.
Such agency or office may, among other things, impose as a condition the payment of
royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright holders.
Every effort has been exerted to locate and seek permission to use these materials from
their respective copyright owners. The publisher and authors do not represent nor claim
ownership over them.
Each SLM is composed of different parts. Each part shall guide you step-
by-step as you discover and understand the lesson prepared for you.
At the end of each module, you need to answer the test to self-check your
learning. Answer keys are provided for each activity and test. We trust that you
will be honest in using these.
In addition to the material in the main text, Notes to the Teacher are also
provided to our facilitators and parents for strategies and reminders on how they
can best help you on your home-based learning.
Please use this module with care. Do not put unnecessary marks on any
part of this SLM. Use a separate sheet of paper in answering the exercises and
tests. And read the instructions carefully before performing each task.
If you have any questions in using this SLM or any difficulty in answering
the tasks in this module, do not hesitate to consult your teacher or facilitator.
Thank you.
For the learner:
Welcome to the Computer Programming – Learning Module 1 on Creating
different types of functions.
This module was designed to provide you with fun and meaningful
opportunities for guided and independent learning at your own pace and time.
You will be enabled to process the contents of the learning resource while being
an active learner.
1. Use the module with care. Do not put unnecessary mark/s on any part of
the module. Use a separate sheet of paper in answering the
exercises.
2. Read the instructions carefully before doing each task.
3. Observe honesty and integrity in doing the tasks and checking your
answers.
4. Finish the task at hand before proceeding to the next.
5. Return this module to your teacher/facilitator once you are finished.
If you encounter any difficulty in answering the tasks in this module, do not
hesitate to consult your teacher or facilitator. Always bear in mind that you
are not alone.
We hope that through this material, you will experience meaningful learning
and gain deep understanding of the relevant competencies. You can do it!
Explore
Introduction:
A function is a block of code that performs a specific task.
Suppose, you need to create a program to create a circle and color it. You
can create two functions to solve this problem:
The sqrt() function calculates the square root of a number. The function is defined in
the math.h header file
User-defined function
#include <stdio.h>
void functionName()
{
... .. ...
... .. ...
}
int main()
{
... .. ...
... .. ...
functionName();
... .. ...
... .. ...
}
Learn
In your previous lesson, we learned how to deal with control
statements
A control statement is a statement that determines whether other
statements will be executed.
An if statement decides whether to execute another statement, or
decides which of two statements to execute.
A loop decides how many times to execute another statement. There
are three kinds of loops:
while loops test whether a condition is true before
executing the controlled statement.
do-while loops test whether a condition is true after
executing the controlled statement.
for loops are (typically) used to execute the controlled
statement a given number of times.
A switch statement decides which of several statements to execute.
Functions
Every programming language lets you create blocks of code that, when
called, perform tasks. Imagine a dog that does the same trick only when
asked. Except you do not need dog treats to make your code perform. In
programming, these code blocks are called functions.
Let's create a function that, when called, prints out “Hello World!” We'll
name this function hello_world(). Clearly I can call hello_world() anywhere
in my code and it will print out (display) the phrase, “Hello World!”
I also can have the hello_world() function return the same phrase. The
output will not display unless you call hello_world() then have your call
print or display the result. For example, I can create a variable, $hello, set
it equal to the output of my hello_world() function, then echo (or display,
or print on the computer screen) the output of my function: “Hello world!”
#include <stdio.h>
void checkPrimeNumber();
int main()
The checkPrimeNumber() function takes input from the user, checks
whether it is a prime number or not and displays it on the screen.
#include <stdio.h>
int getInteger();
int main()
{
int n, i, flag = 0;
// no argument is passed
The empty parentheses in the n = getInteger(); statement
indicates that no argument is passed to the function. And, the value
returned from the function is assigned to n .
Here, the getInteger() function takes input from the user and
returns it. The code to check whether a number is prime or not is inside
the main() function.
#include <stdio.h>
void checkPrimeAndDisplay(int n);
int main()
{
int n;
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
{
int n, flag;
Engage
Apply
INSTRUCTIONS:
Provide the appropriate missing codes of an No arguments passed but a
return value function. Write your answer on the blank.
#include <____________>
int getInteger();
int main()
{
int n, i, flag = 0;
// no argument is passed
n = getInteger();
if (flag == 1)
_______("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
return 0;
}
________ n;
}
Assess
Multiple Choice. Choose the letter of the best answer. Write the chosen
letter on a separate sheet of paper.
1. What is the reason why we must divide complex problems into smaller
chunks?
a. Program
b. Function
c. Value
d. Argument
4. After creating a function, what is the next process to test the function?
a. True
b. False
Reflect
___________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
____________________________.
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
References
https://www.kidscodecs.com/programming-functions/
https://www.cs.utah.edu/~germain/PPS/Topics/functions.html
https://www.codepoc.io/blog/c-programming/4727/functions-with-no-arguments-
and-no-return-values