Laboratory Activity 6
Laboratory Activity 6
: 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
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.
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
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;
}
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
Syntax:
#include<iostream>
using namespace std;
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. 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 _
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.
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:
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 _
The data stored in output are the first data that the user inputted.
Using the output parameters when you need to return more than one _
Since, I onloy used I fumnction, global variables are the same as the answers in the question _
_
Placing forward declarations of function in header files allows for splitting a program into translation
_
_ _
The return statement causes your function to exit and hand back value to its caller
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
// Supplemental Activities
# include <iostream>
#include <string>
conversion();
return 0;
}
Void conversion(){
Cout<<”enter a temperature in Celsius:”;
Cin>>Celsius;
Farenheit = (1.8)*Celsius+32;
Cout<< “farenheit=”
// Supplemental Activities
# include <iostream>
#include <string>
conversion();
}
Void conversion(){
Cout<<”enter a temperature in Celsius:”;
Cin>>Celsius;
Farenheit = (1.8)*Celsius+32;
Cout<< “farenheit=”<<farenheit<<”\n”;
#include <iostream>
#include <string>
Int mai(){
Conversion ();
}
Void conversion(){
Cin>>radius;
Diameter= 2*radius;
Cout<<”diameter:”<<diameter”<<”\n”;
Circumference =(2*3.14)*radius;
Cout<<”circumference:”<<circumference”<<”\n”;
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.