You are on page 1of 8

I.

Introduction

A function is a group of statements that is executed when it is called


from some point of the program. By using functions, we can structure our
programs in a more modular way. In the general form of a function, there are
what we call as arguments or parameters. These are the information that are
taken from the function. These informations can be accessed through passed-
by-values and passed-by-reference. In this discussion, we are only limited to
passed-by-reference. To use this application in C++ programming will enable
a clean and more organized program and to use pass-by-reference rather than
passed-by-value will be more efficient since the function passed will not copy
the value but it will refer it to the other module.

II. Pass-by-Reference

There are instances where functions need to modify values having


complex structures like arrays. In cases like this, it is more efficient and wiser
to have the function modify the actual array passed to it rather than returning
it to the caller. One way to allow functions to modify the value of arguments
is by using pass by reference. To pass by reference is quite similar to a pointer
referring the address of a value. We are declaring the function parameters as
reference rather than normal variables.

Example:

void One(int &y)


{
y=y+1
}

When the function is called, y will become a reference to the argument.


Since a reference to a variable is treated exactly the same as the variable itself,
any change will be passed through it.

Looking closely at the example given, we see an ampersand sign before


the parameter. This specifies that the arguments are to be passed as reference
instead of just values.

There are other instances that we need a function to return multiple


values. However, functions can only have one return value. One way to return
multiple values is using reference parameters:

Example:

#include <iostream>
#include <math.h>
void GetSinCos(double dX, double &dSin, double &dCos)
{
dSin = sin(dX);
dCos = cos(dX);
}

int main()
{
double dSin = 0.0;
double dCos = 0.0;
GetSinCos(30.0, dSin, dCos);

std::cout << "The sin is " << dSin << std::endl;


std::cout << "The cos is " << dCos << std::endl;
system("PAUSE");
return 0;
}

This function takes one parameter (by value) as input, and “returns” two
parameters (by reference) as output.

One of the major disadvantages of pass by value is that all arguments


passed by value are copied to the parameters. When the arguments are large
structs or classes, this can take a lot of time. References provide a way to
avoid this penalty. Remember that an argument is passed by reference, a
reference is created to the actual argument and no copying of values takes
place. This allows us to pass large structs and classes with a minimum
performance penalty. However, this also opens us up to potential trouble.
References allow the function to change the value of the argument, which in
many cases is undesirable. If we know that a function should not change the
value of an argument, but don’t want to pass by value, the best solution is to
pass by const reference.

Here is an example:
void pass(const int &x)
{
x = 6;
}

III. Scope, Local Variable, and Global Variable

A block of a statement, also called compound statement, is a group of


statements that is treated by the compiler as if it were a single statement.
These are found all over the passed by reference. Blocks begin and end with
the bracket symbol “{}”, and the statements to be executed are placed
between them. Variables declared inside a block are called local variables and
local variables have block scope. The scope determines who can see the
variable, and how long it lives for and with the local scope, the local variable
is only applicable within the module. If the block or module has ended, then
so does the local variable and therefore it will be destroyed.
Here is an example:

#include <iostream>

void AddOne(int &y)


{
y++;
}

int main()
{
int x = 5;

cout << "x = " << x << endl;


Plus(x);
cout << "x = " << x << endl;

return 0;
}

Another type of variable is the global variable. This variable has the
program scope wherein the variable can be used by any block within the
program. Because global variables have program scope, they can be used
across multiple files.

Here is an example:

#include <iostream>
Using namespace std;
Int y = 3;

void AddOne(int &y)


{
y++;
}

int main()
{
int x = 5;

cout << "x = " << x << endl;


Plus(x);
cout << "x = " << x << endl;

return 0;
}
Pseudo Code:
Start program
Initialize iostream, cstdlib, math.h, conio, iomanip
Declaration of Variables
Integers: choice, choice1, repick, repick1, select, C=0, n, t=0, X,
sum=0, row=0, column=0
Doubles: sample[11], cons[11], stat[10], matrixA[3][3],
matrixB[3][3], Xpow=0, ans=0, Constant, mean=0, sd=0,
x=0
Choices:
Print: Select the number you want to use:
1. Polynomial Evaluation
2. Statistical Algorithm
3. Matrix Operations
4. Exit
Select the task you want the program to do:
Input: choice1
If: choice1 = 1 or choice1 = 2 or choice1 = 3 or choice1 = 4
choice = choice1 and continue to case

Else: Print “Wrong Choice Input!”


Print “Press any key to continue”
Go back to Choices again

Case 1: Polynomial Evaluation


Repeat1:
Print: You have selected number 1 – Polynomial Evaluation
CX^n
Please input the order (max of 10):
Please input the X value:
Show the X value exponent from zero to the inputted order.
Input the number of constants based on the inputted order
Input constant only:
Print the result of the polynomial
Check1: Error for wrong input for (y/n)
Print: Do you want to solve another problem using Polynomial
Evaluation? (Y/N)
1. Yes
2. No
Input:
Scan choice

If choice == Y
Clear screen
Go to Repeat1

Else if choice == N
Clear screen
Return to Choices

Else
Print “Wrong Choice Input! Choose the number correctly.”
“Press any key to continue!”
Go to Check1

Case 2: Statistical Algorithm


Repeat2:
Print: You have selected number 2 – Statistical Algorithm
Please input the number of data (max of 10):
If input data is greater than 10,
Print: You are already out of the limit
Do you want to solve another problem using Statistical
Algorithm?
1. Yes
2. No
If yes then proceed.
Print: Please input stat[n] value:
Clear Screen
Display input stat[n] values

Print: The Operations


1. Mean
2. Standard Deviation
3. Summation
4. Exit
If chosen 1. Mean,
mean = sum / n
Print: The Mean is:
Else if chosen 2. Standard Deviation,
Xpow = stat[t]-mean
Xpow = powf(Xpow,2)
x += Xpow
sd = sqrt(x/n)
Print: The Standard Deviation is:
Else if chosen 3. Summation,
sum += stat[t]
Print: The summation is:
Else if chosen 4. Exit,
Check2: Error for wrong input for (y/n)
Print: Do you want to solve another problem using Statistical
Algorithm? (Y/N)
3. Yes
4. No
Input:
Scan choice

If choice == Y
Clear screen
Go to Repeat2

Else if choice == N
Clear screen
Return to Choices

Else
Print “Wrong Choice Input! Choose the number correctly.”
“Press any key to continue!”
Go to Check2
Case 3: Matrix Operations
Repeat3:
Print: You have selected number 3 – Matrix Operations
Please input the row (max of 3):
Please input the column (max of 3):
Input matrix A’s row and column [m][n]:
Input matrix B’s row and column [m][n]:
Clear screen
Display Matrix A and Matrix B
Print: The Matrix Operations
1. Addition
2. Multiplication
3. Transpose
4. Exit
If chosen 1. Addition,
Print: The answer is:
Display resulting matrix
Else if chosen 2. Multiplication,
Print: Please input the multiplier:
Display resulting matrix A and B
Else if chosen 3. Transpose,
Print: The Transpose of Matrix A and Matrix B:
Display Matrix A
Display Matrix B
Else if chosen 4. Exit,
Check3: Error for wrong input for (y/n)
Print: Do you want to solve another problem using Statistical
Algorithm? (Y/N)
1. Yes
2. No
Input:
Scan choice
If choice == Y
Clear screen
Go to Repeat3

Else if choice == N
Clear screen
Return to Choices

Else
Print “Wrong Choice Input! Choose the number correctly.”
“Press any key to continue!”
Go to Check3

You might also like