Professional Documents
Culture Documents
SY LBYEC72report6
SY LBYEC72report6
Laboratory Activity 6
By
Modular programming is a software design technique that emphasizes separating the functionality
of a program into independent, interchangeable modules, such that each contains everything necessary to
execute only one aspect of the desired functionality.
Most programming language provide many built in functions that would require steps and
solution to operate. Function allows us to conceive of our program as a bunch of steps. The main
feature is to reuse code instead of rewriting (Function, 2017). The function allows variable to
keep intact and neat when writing a code. The function is for proper programming in order to
squeeze all the program in to one liner declaration. It is easy to monitor and debug the problem
when debugging or fixing the program.
Function is important in engineering programming since engineering is all about data and
formula in which function can store such data in to a declaration separated from the main
program. It is very useful for the programmer to debug or evaluate the program when formula
and data are in the program. The function will contain instructions used to create the output from
its input.
OBJECTIVES
1
equivalent modular and reusable functions.
● Create reusable functions that can be used in other programs.
MATERIALS
PROCEDURE
Program is a modified version of the collection of formulas from Nested Conditional and Iterative
Statements.
Convert the formulas previously done in the collection of formulas to equivalent functions.
Program can be subdivided into smaller units by creating functions that compute for different
variables.
Requirements from Nested Conditional and Iterative Statements apply.
A formula from the formula screen must be chosen by clicking 1 or 2. When 3 is chosen, the program
exits.
2
After the first step, a variable to be solved must now be chosen.
3
compute for the 2nd variable.
4
Compute for the 4th variable.
SUMMARY
From this activity, I realized that function and modularizing programs are very useful since some
programs have thousands of lines. And without this type of program, it is quite impossible to manage
such due to possible logical errors and syntax errors. Thus, modular programming emphasizes on
breaking of large programs into small problems to increase the maintainability, readability of the code
and to make the program handy to make any changes in future or to correct the errors.
REFERENCES
5
https://stackoverflow.com/questions/37136552/meaning-of-pass-by-reference-in-c-and-c
https://www.codingunit.com/c-tutorial-functions-and-global-local-variables
APPENDIX
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//slope
float slo1(float y1, float y2, float y3); //y intercept
float slo2(float y4, float y5, float y6); //m intecept
float slo3(float y7, float y8, float y9); //x intercept
float slo4(float y10, float y11, float y12); //b intercept
do
{
printf("Formulas\n");
printf("1 - Volume of a Triangle Prism\n2 - Slope Intercept Form\n3 - Exit\nPlease enter the
number of choice: \n");
scanf("%d", &change);
system("PAUSE");
switch(change)
{
6
{
printf("Computing for the volume\n Enter the value of L: \n");
scanf("%d",&length);
printf("Enter the value of W: \n");
scanf("%d",&width);
printf("Enter the value of H: \n");
scanf("%d",&height);
printf("The volume is %d\n",vol1(length,width,height));
system("PAUSE");
system("cls");
}
else if((g1=='W') || (g1=='w'))
{
printf("Computing for the value of Width\nInput V: \n");
scanf("%d",&volume);
printf("Enter the value of L: \n");
scanf("%d",&length);
printf("Enter the value of H: \n");
scanf("%d",&height);
printf("The width is %d\n",vol3(volume,length,height));
system("PAUSE");
system("cls");
}
else if((g1=='L') || (g1=='l'))
{
printf("Computing for the Length\n Enter the value of V: \n");
scanf("%d",&volume);
printf("Enter the value of W: \n");
scanf("%d",&width);
printf("Enter the value of H: \n");
scanf("%d",&height);
printf("The Length is %d\n",vol2(volume,width,height));
system("PAUSE");
system("cls");
}
7
system("cls");
}
else if((g1=='X') || (g1=='x'))
{
system ("PAUSE");
return 0;
}
else
{
break;
case 2:
system("cls");
printf("Computing for slope\ny - Point on the y axis\nx - Point on the x axis\nm - slope\nb - y
intercept\nX - to exit\nFormula y=mx+b\nPlease enter variable to compute\n");
scanf("%s",&g2);
if((g2=='Y') || (g2=='y'))
{
printf("Let us compute for y intecept\nEnter the value of x: \n");
scanf("%f",&x);
printf("Enter the value of m: \n");
scanf("%f",&m);
printf("Enter the value of b: \n");
scanf("%f",&b);
printf("The value of Y is %.2f\n",slo1(x, m, b));
system("PAUSE");
system("cls");
}
else if((g2=='M') || (g2=='m'))
{
printf("Let us compute for the slope m\nEnter the value of Y: \n");
scanf("%f",&y);
printf("Enter the value of x: \n");
scanf("%f",&x);
printf("Enter the value of b: \n");
scanf("%f",&b);
printf("The value of m is %.2f\n",slo2(y, x, b));
system("PAUSE");
system("cls");
}
8
else if((g2=='X') || (g2=='x'))
{
printf("Let us compute for x\n Enter the value of y: \n");
scanf("%f",&y);
printf("Enter the value of m: \n");
scanf("%f",&m);
printf("Enter the value of B: \n");
scanf("%f",&b);
printf("The value of X is: %.2f\n",slo3(y, m, b));
system("PAUSE");
system("cls");
}
else if((g2=='B') || (g2=='b'))
{
printf("Let us compute for the y intercept form\n Enter the value of Y: \n");
scanf("%v",&y);
printf("Enter the value of m: \n");
scanf("%v",&m);
printf("Enter the value of x: \n");
scanf("%v",&x);
printf("The slope is: %.2f\n",slo4(y, m, x));
system("PAUSE");
system("cls");
}
else if((g2=='X') || (g2=='x'))
{
system ("PAUSE");
return 0;
}
else
{
break;
case 3:
printf("System exit\n");
system("PAUSE");
system("cls");
return 0;
break;
9
}while(<4);
return 0;
}
int vol1(int z1, int z2, int z3)
{
int a1;
a1 = (z1*z2*z3)/2;
return a1;
}
int vol2(int z4, int z5, int z6)
{
int a2;
a2 = z4/(z5*z6);
return a2;
}
int vol3(int z7, int z8, int z9)
{
int a2;
a2 = z7/(z8*z9);
return a2;
}
int vol4(int z10, int z11, int z12)
{
int a2;
a2 = z10/(z11*z12);
return a2;
}
float slo1(float y1, float y2, float y3)
{
float t1;
t1 = (y2*y1)+y3;
return t1;
}
float slo2(float y4, float y5, float y6)
{
float t2;
t2 = (y4-y6)/y5;
return t2;
}
float slo3(float y7, float y8, float y9)
{
float t3;
t3 = (y7-y9)/y8;
return t3;
}
float slo4(float y10, float y11, float y12)
10
{
float t4;
t4 = y10 - (y11*y12);
return t4;
Activity Number 7
At the end of the lab session, students should be able to:
Understand pass-by-reference.
Understand the use of local variables, scope, and global variables.
Modularize programs with the use of functions and pass-by-reference.
Convert algorithms previously done in single-dimensional and multi-dimensional arrays to
modular and reusable functions.
Create reusable functions that can be used in other programs.
Pass by reference is means the functions parameters are the same as callers. The passing of the
arguments copies the reference of the formal parameter inside the function. The reference is used to
access the argument that is being called, this function is also called call by reference
Parameters can accept functions and can return results, the functions is declared. The function
declaration starts 3 integers below
Example:
#include<stdio.h>
int main()
{
int answer, input1, input2;
11
scanf("%d", &input1);
scanf("%d", &input2);
answer = Add(input1,input2);
12