0% found this document useful (0 votes)
56 views

Laboratory Activity 6

The document describes a laboratory activity on functions. The objectives are to understand using functions in a program, designing modular programs with functions, and constructing programs with user-defined functions. It discusses function structure, prototypes, parameters, return values, and global and local variables. It provides examples of function syntax and uses. The activity involves writing algorithms, flowcharts, and programs to solve quadratic equations using functions. It asks questions about outputs for different numeric inputs to the quadratic equation program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Laboratory Activity 6

The document describes a laboratory activity on functions. The objectives are to understand using functions in a program, designing modular programs with functions, and constructing programs with user-defined functions. It discusses function structure, prototypes, parameters, return values, and global and local variables. It provides examples of function syntax and uses. The activity involves writing algorithms, flowcharts, and programs to solve quadratic equations using functions. It asks questions about outputs for different numeric inputs to the quadratic equation program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Course: CPE 102A Experiment No.

: ACTIVITY 6
Name: BAGUITAN, KRIZEA ABEGAIL Section: EE21S1
LEAL
Date Performed: November 17, 2021
Date Submitted: November 17, 2021
Instructor: ENGR. JULIE ANN B. SUSA

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


{
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

STEP 1: START START


STEP 2: enter values of a,b and c  Input the integers a, b and c
STEP 3: check wether the values entered  Check wether the values input are
are not equal to zero not equal to zero
STEP 4: if the values of a, b and c are zero,  if the values of a, b and c are zero,
then invalid entry, user must enter valid then invalid entry, user must enter
number valid number that are not equal to
STEP 5: if the values are not equal to zero zero. ( go back in entiring a
STEP 6: solve for the quadratic equation number)
STEP 7: Display the results  if values !=0. Then
STEP 8: END  solve for the quadratic equation
 display
END
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.

SINCE THE USER DIDN’T ENTER ZERI FROM ANY OF A, B AND C THEN IT WILL PRINT
RESULT _

Inputs: 20, 9 and answers: -0.225 + 0.222205 – 0.225-0.2222050 _

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.

Since the user didn’t enter zero from any of a,b and c then it will print result _
Input: 2.9 and answers: -0.113999, -4.38860 _

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.

Inputs: 10. 40 and 5 answers: -0.129171, - 3.870830 _

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.

Input 80, 20 and answers: -0.125 + 1.11102 – 1.111020 _

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.

Inputs: 20, 80 and answers: -2 + 1, -2 -10 _

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
STEP 1: START  START
STEP 2: enter name  enter name
STEP 3: enter course  enter course
STEP 4: Enter scores of prelims, midterms and  Enter scores of prelims,
Finals midterms and Finals
Quiz, Seatweork, Lab exercises, o Quiz, Seatweork, Lab
Assignment, Exam and Total exercises, Assignment,
Grade Exam and Total Grade
STEP 5: compute for Prelim Grade, Midterm  compute for Prelim Grade,
Grade and Final grade Midterm Grade and Final grade
STEP 6: Check whether final grade is greater  Check whether final grade is
than or equal to 75 greater than or equal to 75
STEP 7: print remarks “PASSED”  print remarks “PASSED”
STEP 8: End
 End
FLOWCHART
QUESTIONS

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

Char name and course int scores ( quiz, seatwork, lab exercises, assignment final exam ) and float
Total grade are needed to produce local variables these variables are needed in order for the
program to execute operations _

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

The data stored in output are the first data that the user inputted.

Using a return value when you only need to return one _

Using the output parameters when you need to return more than one _

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

Since, I onloy used I fumnction, global variables are the same as the answers in the question _
_

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

Placing forward declarations of function in header files allows for splitting a program into translation
_

_ _

5. What is the important of return in function?

The return statement causes your function to exit and hand back value to its caller

The point functions in general is to take in input and return something

The return statement is use when a function is ready to return a value to its _

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

// Supplemental Activities

// using parameter and return

# include <iostream>

#include <string>

Using namespace std;

Float celsuis, farenhiet, kelvin;


void Conversion();

int main(float celsuis, float farenheit, float kelvin) {

conversion();
return 0;
}

Void conversion(){
Cout<<”enter a temperature in Celsius:”;

Cin>>Celsius;

Farenheit = (1.8)*Celsius+32;

Cout<< “farenheit=”

<<farenheit<<”\n”; kelvin= Celsius+


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

Write your answer.

// Supplemental Activities

// using parameter and return

# include <iostream>

#include <string>

Using namespace std;

Float celsuis, farenhiet, kelvin;


void Conversion();

int main(float celsuis, float farenheit, float kelvin) {

conversion();
}

Void conversion(){
Cout<<”enter a temperature in Celsius:”;

Cin>>Celsius;

Farenheit = (1.8)*Celsius+32;

Cout<< “farenheit=”<<farenheit<<”\n”;

Kelvin= Celsius +273


Cout<<”degree kelvin=”<<kelvin<<”\n”;
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.

// global and local variable

#include <iostream>

#include <string>

Float radius; // global


Void conversion();

Int mai(){
Conversion ();
}
Void conversion(){

Float diameter, circumference, area: // local variable

Cout << “ enter radius:”;

Cin>>radius;

Diameter= 2*radius;
Cout<<”diameter:”<<diameter”<<”\n”;

Circumference =(2*3.14)*radius;
Cout<<”circumference:”<<circumference”<<”\n”;

Area = 3.14 * (radius* radius);


Cout<<”area:”<<area”;
6. Conclusion:

Functions modify values, carry out actions, and/ or return values. I’ve found
out that function are best used to execute a section of code that would
otherwise be repeated, and divide the program into well organize pieces.

You might also like