Functions
Functions
Objective Part
Q 1. Consider the following C++ function:
#include <iostream>
using namespace std;
int x;
void summer(int&, int);
void fall(int, int&);
int main() {
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;
}
Introduction to Programming
void fall(int u, int& v) {
int intNum2;
intNum2= x;
v = intNum2 * 4;
x = u - v;
}
#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;
}
else {
num--;
v = v + 5;
}
cout << v << ", " << num << endl;
}
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);
#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;
.
.
.
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) {
Introduction to Programming
char ch;
int a;
.
.
.
//Block four
{
int x;
char a;
.
.
}//end Block four
.
.
.
}
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.
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.
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.
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
reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532)
is -235.
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.
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.
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.