0% found this document useful (0 votes)
14 views4 pages

Untitled Document

document

Uploaded by

Mariane Pagulon
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)
14 views4 pages

Untitled Document

document

Uploaded by

Mariane Pagulon
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/ 4

Mariane S.

Pagulon April 13, 2023


BSCE 1 Mr. Angelus Vincent Guilalas

Computer Fundamental Programming


EXERCISE NO. 4
FUNCTIONS

Objectives: (at the end of the of the activity, the students will be able to)
1. Define new functions in Python.
2. Write programs that use functions to reduce code duplication and increase program
modularity.

Equipment
Quantity
2 PC per students with python IDE

Basic Concepts:

Introduction to function

CONCEPT: A function is a group of statements that exist within a program for the purpose of performing a
specific task. In the previous activities, we described a simple algorithm for calculating an employee’s
pay. In the algorithm, the number of hours worked is multiplied by an hourly pay rate. A more realistic
payroll algorithm, however, would do much more than this. In a real-world application, the overall task of
calculating an employee’s pay would consist of several subtasks, such as the following:
A program that has been written with each task in its own function is called a modularized program.

Introduction to Functions
1. What is a function?
- A function is a group of statements that exist within a program for the purpose of performing a
specific task.

2. What is meant by the phrase “divide and conquer”?


- to make a group of people disagree and fight with one another so that they will not join together
against one.

3. How do functions help you reuse code in a program?


- Functions help you reuse code in a program because the function can be written once to perform an
operation, and then be executed any time it is needed.

4. How can functions make the development of multiple programs faster?


- Functions can be written for the commonly needed tasks, and those functions can be incorporated
into each program that needs them.

5. How can functions make it easier for programs to be developed by


teams of programmers?
- When a program is developed as a set of functions that each performs an individual task, then
different programmers can be assigned the job of writing different functions.
Defining and Calling a Void Function
1. A function definition has what two parts?
- A block and a header.

2. What does the phrase “calling a function” mean?


- Calling a function actually gives the command, so the computer will run the code for that function.

3. When a function is executing, what happens when the end of the function’s block is reached?
- The interpreter jumps back to the part of the program that called the function, and the program
resumes execution at that point.

4. Why must you indent the statements in a block?


- To delimit code blocks.

Passing Arguments to Functions


1. What are the pieces of data that are passed into a function called?
- Parameters and Arguments.

2. What are the variables that receive pieces of data in a function called?
- Parameter.

3. What is a parameter variable’s scope?


- A parameter variables is the entire body of the method.

4. When a parameter is changed, does this affect the argument that was passed into the
parameter?
- It changes made to the parameter will affect the argument used to call the subroutine.

5. The following statements call a function named show_data. Which of the statements passes
arguments by position, and which passes keyword arguments?
a. show_data(name='Kathryn', age=25)
b. show_data('Kathryn', 25)
- A passes keyword arguments, while B passes arguments by position.

Global Constants
1. What is the scope of a global variable?
- it is visible (hence accessible) throughout the program, unless shadowed.

2. Give one good reason that you should not use global variables in a program.
- The reason is to protect data and members of classes from being changed, inadvertently or on
purpose, by other parts of the program.

3. What is a global constant? Is it permissible to use global constants in a program?


- A global constant is a name that is accessible to all software modules. Global constants are legal
to utilize. You need not be concerned about a global constant's value changing because it cannot
be altered while the program is being executed.
Machine Problems:
1. Create a software algorithm that requests a distance in kilometers from the user and then
translates that value to miles. The following is the conversion formula:
Miles to Kilometers 3 0.6214
2.Write a program that prompts the user to enter the monthly cost for each of the following
auto-related expenses: loan payment, insurance, petrol, oil, tires, and maintenance. The
program should then show the entire yearly cost of these expenses as well as their total
monthly cost.
3.The assessment value of a property, which is 60% of its actual worth, is used by the
county to calculate property taxes. For instance, if a piece of land is assessed at $10,000
per acre, $6000 is the value. Then, for every $100 of the assessment value, the property
tax is 72cents. The tax on the $6,000-as-assessed acre will be $43.20. Create a program
that requests a piece of property's actual worth and then displays the assessment value
and property tax.
4.One gallon of paint and eight hours of effort will be needed for every 112 square feet of
wall area, according to a painting company's calculations. For labor, the business costs
$35.00 an hour. Create a program that prompts the user for the number of square feet of
wall space that needs painting and the gallon pricing of paint. The following information
should be shown by the program.
• The number of gallons of paint required
• The hours of labor required
• The cost of the paint
• The labor charges
• The total cost of the paint job
5. A retail business is required to submit a monthly sales tax report detailing the total
monthly sales as well as the amount of state and local sales tax collected. The county
sales tax is 2.5 percent, whereas the state sales tax is 5%. Create a program that
prompts the user to enter the month's total sales. The following figures should be
computed and shown by the program using this figure:
• The amount of county sales tax
• The amount of state sales tax
• The total sales tax (county plus state)

Generalization:
Today’s task is to program a topic function. Function programming is all about static truths such as in
Math, is easier to reason about, is tractable, and in a lot of places solves our problems better than
imperative solutions.

References:
[1]
chegg, “‘Starting Out With Python’: Ch. #3 Flashcards | Chegg.com,” www.chegg.com, Apr. 13,
2023. https://www.chegg.com/flashcards/starting-out-with-python-ch-3-9a40482e-6e02-45aa-
89e7-bf8689a2ded6/deck (accessed Apr. 13, 2023).
[2]
quizlet, “Starting Out with Python, 3e Ch 5,” quizlet, Apr. 13, 2023.
https://quizlet.com/442988869/starting-out-with-python-3e-ch-5-flash-cards/ (accessed Apr. 13,
2023).

You might also like