LABORATORY ACTIVITY 6 Ict

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Course: Experiment No.

:
Name: Section:
Date Performed:
Date Submitted:
Instructor:

LABORATORY ACTIVITY NO.6

FUNCTIONS

1. Objective(s):

The activity aims to perform a certain task a small blocks using analytical tools such as algorithm, flowchart
and program to evaluate the output based on the requirement.

2. Intended Learning Outcomes (ILOs):

The students shall be able to:

1. Understand the use of functions in a program and how to call function


2. Design a modularized program using function
3. Construct a program using user-defined function

3. Discussion:
A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed
from as many different points in program. Function groups a number of program statements into a unit and
gives it a name. The name of the function is unique and global.

Structure of a Function

There are two main parts of the function.

1. The function header - It has three main parts


a. The name of the function i.e. sum
b. The parameters of the function enclosed in parenthesis
c. Return value type i.e. int

2. The function body.


What ever is written with in { } in the above example is the body of the function.
Syntax: Function header
int sum(int x, int y)
{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum Function Body
return ans //return the answer
}

Function Prototypes
The prototype of a function provides the basic information about a function which tells the compiler that the
function is used correctly or not. It contains the same information as the function header contains. The
prototype of the function must have a semicolon at the end of the prototype.
prototype of the function
int sum (int x, int y);

NOTE: The only difference between the header and the prototype is the SEMICOLON (;)

Syntax:

#include<iostream>
using namespace std;

void MyPrint()
{
cout << "Printing from a function.\n";
}

int main()
{
MyPrint();
return 0;
}

Parameters and return


Functions can accept parameters and can return a result. Where the functions are declared in your
program does not matter, as long as a functions name is known to the compiler before it is called. In other
words: when there are two functions, i.e. functions A and B, and B must call A, than A has to be declared in
front of B.
Syntax:
#include<iostream>
using namespace std;
The function Add() will return the result of output1 + output2
int Add(int output1, int output2)
{
The return value is stored in the integer answer

return output1 + output2;


}

int main() main() function starts with the declaration of three


integers
{
int answer, input1, input2;

cout << "Give a integer number:"; user can input two whole (integer)
numbers. These numbers are used as
cin >> input1; input of function Add().
cout << "Give another integer number:";
cin >> input2;
Input1 is stored in output1 and input2 is stored in output2
answer = Add(input1,input2);
cout << input1 << " + " << input2 << " = " << answer;
return 0;
} The number stored in answer is then printed onto the screen

Global and Local variables


 A local variable is a variable that is declared inside a function.
 A local variable can only be used in the function where it is declared.
 A global variable is a variable that is declared outside ALL functions.
 A global variable can be used in all functions.

Syntax:

#include<iostream>
using namespace std;

int A, B; Global Variables

int Add()
{
return A + B;
}
int main()
{
int answer; Local Variables

A = 2;
B = 2;
answer = Add();
cout << A << " + " << B << " = " << answer;
return 0;
}

4. Resources:
DevC++
5. Procedure and Output

EXERCISE No.1

A young boy wants to create a program in block of code that will compute for the quadratic
equation then print the result. The main function composes of the input variables.
The formula is given below:

-B ± √ B ^2 – 4AC
---------------------------------
2A

If the user inputs zero (0), the program will display “Invalid Entry” and return to main menu to input another
valid numbers.

1. Write the corresponding algorithm


a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem.
3. Construct the program using Global and Local Variables and record your screen display result.
4. Create the equivalent program using the following structure.
a. Function Header
b. Function Prototype
c. Parameters and return
ALGORITHM
NARRATIVE PSEUDOCODE
FLOWCHART

QUESTIONS

1. If the first input is higher than the second input but lower than the third input, what will be the output
results? Write on the space provided the input values and the results.

_____________________________________________________________________

_____________________________________________________________________

2. If the third input is first than the second input but lower than the first input, what will be the output
results? Write on the space provided the input values and the results.

_____________________________________________________________________
_____________________________________________________________________

3. If the second input is higher than the first input but lower than the third input, what will be the output
results? Write on the space provided the input values and the results.

_____________________________________________________________________

_____________________________________________________________________

4. If the first input is higher than the second input and equal to the third input, what will be the output
results? Write on the space provided the input values and the results.

_____________________________________________________________________

_____________________________________________________________________

5. If the first input is lower than the second input and equal to the third input, what will be the output
results? Write on the space provided the input values and the results.

_____________________________________________________________________

_____________________________________________________________________

EXERCISE No. 2

Juan wanted to create a program that would use block of code (function) to group the student
grades. In each block, it consists of each prelim, midterm and final grade that will required inputting
a student name, course, student grade in quiz, seatwork, laboratory exercises, assignment and
exam. The formula below show the following computation:

Prelim Grade = Quiz * 20% + Seatwork * 10% + Lab. Exercise * 20% + Prelim Exam * 50%
Midterm Grade = Quiz * 20% + Seatwork * 10% + Lab. Exercise * 20% + Midterm Exam * 50%
Total Midterm Grade = 1/3 Prelim Grade + 2/3 Midterm Grade
Final Grade = Quiz * 20% + Seatwork * 10% + Lab. Exercise * 20% + Final Exam *
50%
Total Final Grade = 1/3 Midterm Grade + 2/3 Final Grade
Sample screen layout:
Student Name : Juan dela Cruz
Course : BS CpE
Grades
Quiz :
Seatwork :
Lab. Exercises :
Assignment :
Prelim Exam :
Prelim Grade : __

Quiz :
Seatwork :
Lab. Exercises :
Assignment :
Midterm Exam:
Total Midterm Grade:

Quiz :
Seatwork :
Lab. Exercises :
Assignment :
Final Exam: :
Total Final Grade:

Remarks: _Passed or Failed__

1. Write the corresponding algorithm


a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem.
3. Construct the program and record your screen display result.
ALGORITHM
NARRATIVE PSEUDOCODE
FLOWCHART
QUESTIONS

1. What is/are the variable/s needed to produce local variable/s?

_____________________________________________________________________

_____________________________________________________________________

2. Using Parameter and return structure, what inputs stored in output?

_____________________________________________________________________

_____________________________________________________________________

_____________________________________________________________________

_____________________________________________________________________

3. What is/are the variable/s needed to produce global variable/s?

_____________________________________________________________________

_____________________________________________________________________

4. What did you observe in function header and prototypes?

_____________________________________________________________________

_____________________________________________________________________

5. What is the important of return in function?

_____________________________________________________________________

_____________________________________________________________________
SUPPLEMENTAL ACTIVITIES

1. Write a parameter and return program that will require the user to enter a temperature in Celsius
and convert it to degree Fahrenheit and degree Kelvin.

Formula:
Fahrenheit = (9/5) * Celsius + 32
Kelvin = Celsius + 273

Write your answer.

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________
2. Refer to No. 1, transform the program using function header and function prototype.

Write your answer.

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________
3. Write a global and local variable program that would require the user to input a value of the Radius
of a circle and then show the output in the diameter, circumference of the circle and area of the
circle.

Write your answer.

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________
6. Conclusion:

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

You might also like