Functions Solution
Functions Solution
Objective Part
Q 1. Consider the following C++ function:
Output: The function will return 1 immediately, since num is 0. So, the output will be: 1.
Output: The function will return -1 immediately, since num is negative. So, the output will be: -
1.
Output: The function will calculate the factorial of 10, which is 10! = 10 × 9 × 8 × 7 × 6 × 5 × 4
× 3 × 2 × 1 = 3628800. So, the output will be: 3628800.
#include <iostream>
using namespace std;
int x;
void summer(int&, int);
void fall(int, int&);
int main() {
Introduction to Programming
int intNum1 = 2;
int intNum2 = 5;
x = 6;
summer(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
fall(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
return 0;
}
void summer(int& a, int b) {
int intNum1;
intNum1 = b + 12;
a = 2 * b + 5;
b = intNum1 + 4;
}
void fall(int u, int& v) {
int intNum2;
intNum2= x;
v = intNum2 * 4;
x = u - v;
}
Output:
15 5 6
15 24 -9
#include <iostream>
using namespace std;
void tryMe(int& v);
int main()
{
int x = 8;
for (int count = 1; count < 5; count++)
tryMe(x);
return 0;
}
void tryMe(int& v)
{
static int num = 2;
if (v % 2 == 0) {
num++;
v = v + 3;
}
Introduction to Programming
else {
num--;
v = v + 5;
}
cout << v << ", " << num << endl;
}
Output:
11, 3
16, 2
19, 3
24, 2
Solution:
funcDefaultParam(); is incorrect because the function expects at least one argument,
even though it has default values.
funcDefaultParam(2.8); is correct because it provides a value for x, and the default
values for y and z will be used.
funcDefaultParam(3.2, 0, 'h'); is correct because it provides values for all three
parameters.
funcDefaultParam(9.2, '*'); is incorrect because the second argument is a char, but the
function expects an int for y.
funcDefaultParam(7, 3); is correct because it provides values for x and y, and the
default value for z will be used.
Introduction to Programming
Q 5. Consider following function definition
What is the output of the above mentioned function calls by providing input as:
a. defaultParam(7);
b. defaultParam(8, 2);
c. defaultParam(0, 1, 7.5);
d. defaultParam(1, 2, 3.0);
Solution:
defaultParam(7); * num1 is 7, num2 is default (7), and z is default (2.5).
* num1 becomes 7 + 2 = 9. * z becomes 7 + 9 * 2.5 = 31.5. * num3 is 7 - 9 = -2. *
Output: num3 = -2
defaultParam(8, 2); * num1 is 8, num2 is 2, and z is default (2.5). * num1 becomes
8 + 2 = 10. * z becomes 2 + 10 * 2.5 = 27. * num3 is 2 - 10 = -8. * Output: num3 = -8
defaultParam(0, 1, 7.5); * num1 is 0, num2 is 1, and z is 7.5. * num1 becomes 0 + 7
= 7. * z becomes 1 + 7 * 7.5 = 53.5. * num3 is 1 - 7 = -6. * Output: num3 = -6
defaultParam(1, 2, 3.0); * num1 is 1, num2 is 2, and z is 3.0. * num1 becomes 1 + 3
= 4. * z becomes 2 + 4 * 3.0 = 14. * num3 is 2 - 4 = -2. * Output: num3 = -2
#include <iostream>
using namespace std;
const double RATE = 10.50;
int z;
double t;
void one(int x, char y);
void two(int a, int b, char x);
void three(int one, double y, int z);
int main(){
int num, first;
double x, y, z;
char name, last;
.
Introduction to Programming
.
.
return 0;
}
void one(int x, char y){
:
:
}
int w;
void two(int a, int b, char x) {
int count;
:
}
void three(int one, double y, int z) {
char ch;
int a;
.
.
.
//Block four
{
int x;
char a;
.
.
}//end Block four
.
.
.
}
Solution:
Void = data type
{} = block
Length, width, area, perimeter = variables (parameters of a function)
areaAndPerimeter = function name.
Subjective Part
Q 1. Write the definition of a void function that takes as input two decimal numbers. If the first
number is nonzero, it outputs the second number divided by the first number; otherwise, it outputs a
message indicating that the second number cannot be divided by the first number because the first
number is 0.
Solution:
#include <iostream>
using namespace std;
void main() {
float num1, num2 = 0;
cout << "Enter The value of Two Numbers: \n";
cin >> num1;
cin >> num2;
divideNumber(num1, num2);
}
Q 2. Write a program that uses the function isPalindrome. The function return true if value is
Palindrome, false otherwise.
a. Mention proper function declaration/prototype, and function call from main body.
b. Implement isPalindrome(char[] value, int size) function and specify the return type.
Output:
#include < iostream>
using namespace std;
Q 3. Write a program that implements isVowel that returns the value true if a given character is a
vowel and otherwise returns false. Mention proper function declaration/prototype, and function call
from main body.
#include <iostream>
using namespace std;
bool isVowel(char c) {
c = tolower(c);
return(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
void main() {
char alphabat;
cout << "Enter Alphabat Here: \n";
cin >> alphabat;
if (isVowel(alphabat)) {
cout << "The Alphabat Is Vowel";
}
else {
cout << "The Alphabat Is not Vowel";
}
}
Q 4. Write a function, reverseDigit that takes an integer as a parameter and returns the number with
its digits reversed. For example, the value of reverseDigit(12345) is 54321; the value of
Introduction to Programming
reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-
532) is -235.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter The Number: \n";
cin >> num;
cout << "Reversed digit of Number is: " << reverseDigit(num) << endl;
return 0;
}
Q 5. During winter when it is very cold, typically, everyone would like to know the windchill factor,
especially, before going out. Meteorologists use the following formula to compute the windchill
factor, W:
W = 35.74 + 0.6215 * T - 35.75 * V 0.16 + 0.4275 * T * V0.16
Where V is the wind speed in miles per hour and T is the temperature in degrees Fahrenheit. Write a
program that prompts the user to input the wind speed, in miles per hour, and the temperature in degrees
Fahrenheit. The program then outputs the windchill factor. Your program must contain at least two
functions: one to get the user input and the other to determine the windchill factor.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float speed = 0;
float temp = 0;
float windspeed = getWindSpeed(speed);
float temperature = getTemperature(temp);
float WindChill = calculateWindChill(windspeed, temperature);
cout << "The WindChill Is: " << WindChill;
return 0;
}
Q 6. Write a function that takes as a parameter an integer (as a long value) and returns the number of
odd, even, and zero digits. Also write a program to test your function.
#include <iostream>
using namespace std;
int main() {
long num;
cout << "Enter a long number: ";
cin >> num;
cout << endl;
return 0;
}
Q 7. Write a program that defines the named constant p, i.e. const double p = 3.1419 which stores the
value of p.
I. surfaceArea which takes radius:r as input and return the value of 4pr2 , which is the surface
area of the sphere.
II. volume which takes radius:r as input and return the value of (4/3)pr3, which is the volume of the
sphere.
#include <iostream>
using namespace std;
int surfaceArea(float r) {
return 4.0 * r * r * p;
}
int volume(float r) {
return (4.0 / 3.0) * p * r * r * r;
}
int main() {
float radius = 0;
cout << "Enter The Value OF Radius: \n";
cin >> radius;
float SurfaceareaValue = surfaceArea(radius);
float VolumeValue = volume(radius);
cout << "Value Of Surface Area Is: " << surfaceArea(radius) << endl;
cout << "Value Of Volume Is: " << volume(radius) << endl;
return 0;
}