CL103 - Computer Programming - Lab # 01 (Introduction & Functions)
CL103 - Computer Programming - Lab # 01 (Introduction & Functions)
Instructions:
Getting Started
1. While working on one function, you can focus on just that part of the program and construct it,
debug it, and perfect it.
2. Different people can work on different functions simultaneously.
3. If a function is needed in more than one place in a program or in different programs, you can
write it once and use it many times.
4. Using functions greatly enhances the programs readability because it reduces the complexity of
the function main
Predefined Functions
#include <iostream>
#include <cmath>
#include <cctype>
#include <conio.h>
void main()
{
int x;
double u, v;
cout<<"Line 1: Uppercase a is "<<(char)(toupper('a'))<<endl;
u = 4.2;
v = 3.0;
u = u + pow(3.0, 3);
x = -15;
_getch();
}
Output
Line 1: Uppercase a is A
Line 4: 4.2 to the power of 3 = 74.088
Line 5: 5.0 to the power of 4 = 625
Line 7: u = 31.2
Line 9: Absolute value of -15 = 15
User-Defined Functions
User-defined functions are classified into two categories:
1. Value-returning functions - Functions that have a return type. These functions return a value of
a specific data type using the return statement, which we will explain shortly.
2. Void functions - Functions that do not have a return type. These functions do not use a return
statement to return a value.
Value-returning functions
Syntax of a Value-returning function
functionType functionName(formal parameter list)
{
statements
}
#include <iostream>
#include <conio.h>
void main()
{
double one, two;
cout << "Line 2: The larger of 5 and 10 is "<< larger(5, 10) << endl;
cout << "Line 3: Enter two numbers: ";
cin >> one >> two;
cout << endl;
cout << "Line 6: The larger of " << one<< " and " << two << " is "<<
larger(one, two) << endl; //Function call
cout << "Line 7: The largest of 43.48, 34.00, "<< "and 12.65 is "<<
compareThree(43.48, 34.00, 12.65)<< endl; //Function call
_getch();
}
if (x >= y)
max = x;
else
max = y;
return max;
}
Sample Output
Line 2: The larger of 5 and 10 is 10
Line 3: Enter two numbers: 25.6 73.85
Line 6: The larger of 25.6 and 73.85 is 73.85
Line 7: The largest of 43.48, 34.00, and 12.65 is 43.48
#include <iostream>
#include <conio.h>
int funcRet1();
int funcRet2(int z);
void main()
{
int num = 4;
_getch();
}
int funcRet1()
{
int x = 45;
int funcRet2(int z)
{
int a = 2;
int b = 3;
Sample Output
Line 1: The value returned by funcRet1: 45
Line 2: The value returned by funcRet2: 7
Note: Even though a return statement can contain more than one expression, a return statement in
your program should contain only one expression. Having more than one expression in a return
statement may result in redundancy, wasted code, and a confusing syntax.
#include <iostream>
#include <conio.h>
void main()
{
int score;
Sample output
Enter Student's Score (0-100): 60
Course Grade: D
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;
int rollDice(int num);
void main()
{
cout << "The number of times the dice are rolled to "<< "get the sum 10
= " << rollDice(10) << endl;
cout << "The number of times the dice are rolled to "<< "get the sum 6
= " << rollDice(6) << endl;
_getch();
}
int rollDice(int num)
{
int die1;
int die2;
int sum;
int rollCount = 0;
srand(time(0));
do
{
die1 = rand() % 6 + 1
;
die2 = rand() % 6 + 1;
sum = die1 + die2;
rollCount++;
}
while (sum != num);
return rollCount;
}
Output
The number of times the dice are rolled to get the sum 10 = 11
The number of times the dice are rolled to get the sum 6 = 7
#include <iostream>
#include <conio.h>
void main()
{
double num;
double max;
int count;
_getch();
}
Sample Output
Enter 10 numbers.
5 6 90 30 90 12 47 52 60 6
The largest number is 90
#include <iostream>
#include <iomanip>
#include <conio.h>
void main()
{
int accountNumber;
char customerType;
double amountDue;
switch (customerType)
{
case 'r':
case 'R':
amountDue = residential();
cout << "Account number = "<< accountNumber << endl;
cout << "Amount due = $"<< amountDue << endl;
break;
case 'b':
case 'B':
amountDue = business();
cout << "Account number = "<< accountNumber << endl;
cout << "Amount due = $"<< amountDue << endl;
break;
default:
cout << "Invalid customer type."<< endl;
}
_getch();
}
double residential()
{
int noOfPChannels; //number of premium channels
double bAmount; //billing amount
cout << "Enter the number of premium "<< "channels used: ";
cin >> noOfPChannels;
cout << endl;
return bAmount;
}
double business()
{
int noOfBasicServiceConnections;
cout << "Enter the number of basic "<< "service connections: ";
cin >> noOfBasicServiceConnections;
cout << endl;
cout << "Enter the number of premium "<< "channels used: ";
cin >> noOfPChannels;
cout << endl;
return bAmount;
}
Sample Output
This program computes a cable bill.
Enter account number: 521
Void Functions
Void functions and value-returning functions have similar structures. Both have a heading and a body.
Because void functions do not have a data type, they are not used (called) in an expression. A call to a
void function is a stand-alone statement.
Function Definition
The function definition of void functions with parameters has the following syntax:
void functionName(formal parameter list)
{
statements
}
Function Call
The function call has the following syntax:
functionName(actual parameter list);
Example
#include <iostream>
#include <conio.h>
void main()
{
int noOfLines;
int counter;
int noOfBlanks;
cout << "Enter the number of star lines (1 to 20) "<< "to be printed:
";
cin >> noOfLines;
_getch();
}
S a m p l e O u t p u t
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
Parameters
Parameters provide a communication link between the calling function (such as main) and the called
function. They enable functions to manipulate different data each time they are called. In general, there
are two types of formal parameters: value parameters and reference parameters.
Value parameter: A formal parameter that receives a copy of the content of the corresponding actual
parameter.
Reference parameter: A formal parameter that receives the location (memory address) of the
corresponding actual parameter.
When you attach & after the dataType in the formal parameter list of a function, the variable following
that dataType becomes a reference parameter.
Value Parameters
#include <iostream>
#include <conio.h>
void main()
{
int number = 6;
funcValueParam(number);
_getch();
}
Output
#include <iostream>
#include <conio.h>
void main()
{
int courseScore;
cout << "Based on the course score, \n"<< " this program computes the
"<< "course grade." << endl;
getScore(courseScore);
printGrade(courseScore);
_getch();
}
Sample Output
Based on the course score,
this program computes the course grade.
Enter course score: 85
Course score is 85
Your grade for the course is B.
void main()
{
int num1, num2;
char ch;
num1 = 10;
num2 = 15;
ch = 'A';
cout << "Inside main: num1 = " << num1<< ", num2 = " << num2 << ", and
ch = "<< ch << endl;
cout << "After funOne: num1 = " << num1<< ", num2 = " << num2 << ", and
ch = "<< ch << endl;
cout << "After funTwo: num1 = " << num1<< ", num2 = " << num2 << ", and
ch = "<< ch << endl;
_getch();
}
one = a;
a++;
b = b * 2;
v = 'B';
cout << "Inside funOne: a = " << a<< ", b = " << b << ", v = " << v<<
", and one = " << one << endl;
}
cout << "Inside funTwo: x = " << x<< ", y = " << y << ", and w = " <<
w<< endl;
}
Output
Inside main: num1 = 10, num2 = 15, and ch = A
Inside funOne: a = 11, b = 30, v = B, and one = 10
After funOne: num1 = 10, num2 = 30, and ch = A
Inside funTwo: x = 31, y = 50, and w = G
After funTwo: num1 = 10, num2 = 31, and ch = G
#include <iostream>
#include <conio.h>
void main()
{
int num = 5;
cout << "Inside main: num = " << num<< endl;
addFirst(num, num);
cout << "Inside main after addFirst:"<< " num = " << num << endl;
doubleFirst(num, num);
cout << "Inside main after "<< "doubleFirst: num = " << num << endl;
squareFirst(num, num);
cout << "Inside main after "<< "squareFirst: num = " << num << endl;
_getch();
}
first = first + 2;
cout << " Inside addFirst: first = "<< first << ", second = " << second
<< endl;
second = second * 2;
cout << " Inside addFirst: first = "<< first << ", second = " << second
<< endl;
}
one = one * 2;
cout << " Inside doubleFirst: one = "<< one << ", two = " << two <<
endl;
two = two + 2;
cout << " Inside doubleFirst: one = "<< one << ", two = " << two <<
endl;
}
cout << " Inside squareFirst: ref = "<< ref << ", val = " << val <<
endl;
cout << " Inside squareFirst: ref = "<< ref << ", val = " << val <<
endl;
val = val + 2;
cout << " Inside squareFirst: ref = "<< ref << ", val = " << val <<
endl;
}
Output
Inside main: num = 5
Inside addFirst: first = 5, second = 5
Inside addFirst: first = 7, second = 7
Inside addFirst: first = 14, second = 14
Inside main after addFirst: num = 14
Inside doubleFirst: one = 14, two = 14
Inside doubleFirst: one = 28, two = 14
Inside doubleFirst: one = 28, two = 16
Inside main after doubleFirst: num = 14
Inside squareFirst: ref = 14, val = 14
Inside squareFirst: ref = 196, val = 14
Inside squareFirst: ref = 196, val = 16
Inside main after squareFirst: num = 196
Global Variables
#include <iostream>
#include <conio.h>
int t;
void main()
{
t = 15;
funOne(t);
cout << "In main after funOne: "<< " t = " << t << endl;
_getch();
}
void funOne(int& a)
{
cout << "In funOne: a = " << a<< " and t = " << t << endl;
a = a + 12;
cout <<"In funOne: a = "<<a<< " and t = " << t << endl;
t = t + 13;
cout << "In funOne: a = " << a<< " and t = " << t << endl;
}
Output
In main: t = 15
In funOne: a = 15 and t = 15
In funOne: a = 27 and t = 27
In funOne: a = 40 and t = 40
In main after funOne: t = 40
The following program shows how static and automatic variables behave.
#include <iostream>
#include <conio.h>
void test();
void main()
{
int count;
_getch();
}
void test()
{
static int x = 0;
int y = 10;
x = x + 2;
y = y + 1;
cout << "Inside test x = " << x << " and y = "<< y << endl;
}
Output
Inside test x = 2 and y = 11
Inside test x = 4 and y = 11
Inside test x = 6 and y = 11
Inside test x = 8 and y = 11
Inside test x = 10 and y = 11