Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

STE Computer Programming Q4 MODULE 1 2

This document is a self-learning module for Grade 10 students focusing on creating different types of functions in computer programming. It includes sections on exploring, learning, engaging, applying, assessing, and reflecting on the material, as well as guidelines for using the module effectively. The module covers standard library functions and user-defined functions in C programming, along with examples and exercises to reinforce understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

STE Computer Programming Q4 MODULE 1 2

This document is a self-learning module for Grade 10 students focusing on creating different types of functions in computer programming. It includes sections on exploring, learning, engaging, applying, assessing, and reflecting on the material, as well as guidelines for using the module effectively. The module covers standard library functions and user-defined functions in C programming, along with examples and exercises to reinforce understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

10

Computer
Programming
Quarter IV – Module 1:
Create different Types of Function

"Designed by macrovector / Freepik"


Computer Programming – Grade 10
Self-Learning Module
First Edition, 2020

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.

Published by the Department of Education – Regional Office VIII


Regional Director: DR. Ma. Gemma Mercado-Ledesma, CESO VI
Assistant Regional Director: DR. Sherlita A. Palma, CESO VI

Development Team of the Module


Writers: Jeffrey C. Labor
Language Editors: Rianelle A. Abella
Content Editors: Brian Jojit B. Kitane
Illustrators:
Layout Artist:
Management Team:
Rosemarie M. Guino EdD, OIC – Chief, CLMD
Ryan R. Tiu EdD, EPS, CLMD – Science
Joy B. Bihag, EPS, CLMD – LRMS
Renato S. Cagomoc, Chief, CID
Joy Saldaña, EPS, CID – Science
Noel Sagayap, EPS, CID - LRMS

Printed in the Philippines by ________________________

Department of Education – Regional Office VIII

Office Address: Government Center, Candahug, Palo, Leyte

Telefax: 053 - 3233156


E-mail Address: region8@deped.gov.
Introductory Message
This Self-Learning Module (SLM) is prepared so that you, our dear learners,
can continue your studies and learn while at home. Activities, questions,
directions, exercises, and discussions are carefully stated for you to understand
each lesson.

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 Code Snippets section of the magazine explains parts of programming


languages common across most or all languages. It's a great way to understand
how languages work. And it helps reduce the ability of a new programming
language to glaze your eyes. Learning how to code should be much easier and
less frightening if I explain a part of a programming language, then show you
how it works in a couple different languages.

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.

This module has the following parts and corresponding icons:

This will give you an idea of the skills or


Explore
competencies you are expected to learn in the
module. A brief drill or review to help you link
the current lesson with the previous one. The
new lesson will also be introduced to you in
various ways such as a story, a song, a poem,
a problem opener, an activity, or a situation.
This section provides a brief discussion of the
Learn
lesson. This aims to help you discover and
understand new concepts and skills.

Engage This comprises activities for independent


practice to solidify your understanding and
skills of the topic. You may check the answers
to the exercises using the Answer Key at the
end of the module.
This includes questions or blank
Apply sentence/paragraph to be filled into process
what you learned from the lesson.

Assess This is a task which aims to evaluate your


level of mastery in achieving the learning
competency.
This contains answers to all activities in the
Answer Key module.

This contains the learner’s reflection. Learners


Reflect
are encouraged to think about the lessons
particularly the parts that went well (they
have understood) and the parts that were
weak (they have difficulty) and write about it
briefly. Learners can share their thoughts and
feeling about the lessons.
At the end of this module you will also find:
References This is a list of all sources used in
developing this module.
The following are some reminders in using this module:

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:

 create a circle function


 create a color function

Dividing a complex problem into smaller chunks makes our program


easy to understand and reuse.
Types of function
There are two types of function in C programming:
 Standard library functions
 User-defined functions

Standard library functions


The printf() is a standard library function to send formatted output to the screen (display
output on the screen). This function is defined in the stdio.h header file.
Hence, to use the printf() function, we need to include the stdio.h header file
using #include <stdio.h> .

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!”

Therefore, the phrase “Hello world!” exists in two possible forms:

 I create the hello_world() function to simply print out the phrase.


 I create the hello_world() function to return the phrase, not print it
out.

Types of User-defined Functions in C Programming

1. No arguments passed and no return value

#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.

The empty parentheses in checkPrimeNumber(); statement inside the


main()function indicates that no argument is passed to the function.

The return type of the function is void. Hence, no value is returned


from the function.

2. No arguments passed but a return value

#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.

3. Argument passed but no return value

#include <stdio.h>
void checkPrimeAndDisplay(int n);

int main()
{
int n;

printf("Enter a positive integer: ");


scanf("%d",&n);
The integer value entered by the user is passed to the
checkPrimeAndDisplay() function.

Here, the checkPrimeAndDisplay() function checks whether the


argument passed is a prime number or not and displays the appropriate
message.

4. Argument passed and a return value

#include <stdio.h>
int checkPrimeNumber(int n);

int main()
{
int n, flag;

printf("Enter a positive integer: ");


scanf("%d",&n);
The input from the user is passed to the checkPrimeNumber() function.

The checkPrimeNumber() function checks whether the passed argument is


prime or not.

If the passed argument is a prime number, the function returns 0. If


the passed argument is a non-prime number, the function returns 1. The
return value is assigned to the flag variable.

Depending on whether flag is 0 or 1, an appropriate message is


printed from the main() function.

Engage

Instruction: Match Column A with the correct statements of operators in


Column B by drawing a line to connect them.
COLUMN A COLUMN B

1. A block of code that A. printf()


performs a specific
task

2. A standard library B. checkPrimeNumber()


function to send
formatted output to
the screen (display
output on the screen.

3. Function that C. Function


calculates the square
root of a number

4. checks whether it is D. checkPrimeAndDisplay()


a prime number or
not and displays it
on the screen.

5. checks whether the E. sqrt()


argument passed is
a prime number or
not and displays
the appropriate
message.

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();

_____(i=2; i<=n/2; ++i)


{
if(n%i==0){
flag = 1;
break;
}
}

if (flag == 1)
_______("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);

return 0;
}

// returns integer entered by the user


int getInteger()
{
_______ n;

printf("Enter a positive integer: ");


scanf("%d",&n);

________ 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. To summarize the problem


b. To correct the problem
c. To make it easy to understand and re-use
d. To create another problem

2. The printf() code is what type of function in C programming?

a. Standard library function


b. User-defined function
c. Arguments
d. Value

3. These are block of codes that performs specific task.

a. Program
b. Function
c. Value
d. Argument

4. After creating a function, what is the next process to test the function?

a. Save the function


b. Run or recall the function
c. Exit the program

5. The checkPrimeNumber() code checks whether it is a prime number or not


and displays it on the screen.

a. True
b. False

Reflect

Today, I discovered that

___________________________________
________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

____________________________.

I encountered difficulties in________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________
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

For inquiries or feedback, please write or call:

Department of Education – Regional Office VIII – Curriculum and Learning


Management Division (CLMD) - Learning Resources Management Section (LRMS)

Government Center, Candahug, Palo, Leyte, 6501

Telefax: (053) 323-3156; 323-3854; 824-4627

Email Address: *region8@deped.gov.ph


*clmd.region8@deped.gov.ph *lrmds.region8@deped.gov.ph

You might also like