In: Computer Science
Function writing:
1). (1 point) Write the prototype of a value-returning function checkIfOdd. This function takes three integer parameters. It returns true if all three of the parameters are odd, and false otherwise.
2). (1 point) Write a sample call for this function. (Assume that you are calling from main)
3). (3 points) Write the definition (header and body) of the function.
Code for given function writing question is provided below.
C++ code is written and necessary comments are added in code.
Code:
Filename: funWriting.cpp
#include<iostream>
using namespace std;
// Function prototype
bool checkIfOdd(int, int, int);
int main()
{
bool flag;
// Sample call for function from main
// Case-1
flag = checkIfOdd(5, 9, 11);
if(flag==true)
cout<<"\nAll parameters are Odd\n";
else
cout<<"\nFunction returned False\n";
// Case-2
flag = checkIfOdd(10, 4, 11);
if(flag==true)
cout<<"\nAll parameters are Odd\n";
else
cout<<"\nFunction returned False\n";
return 0;
}
//Function Definition
bool checkIfOdd(int num1, int num2, int num3)
{
if((num1%2 == 1) && (num2%2 == 1) && (num3%2 ==
1))
{
return true;
}
// This part will execute if condition in 'If' is not
satisfies...s
return false;
}
Compiled and executed on ubuntu terminal...
Output:
g++ funWriting.c
./a.out
All parameters are Odd
Function returned False
Output Explaination:
In first sample function call...
Parameters were 5, 9, 11.
As all are Odd numbers, function returnes value 'true' and therefore program prints the line 'All parameters are Odd'.
In second sample function call...
Parameters were 10, 4, 11.
As 10 & 4 are even numbers and 11 is an odd number, function returnes value 'false' and therefore program prints the line 'Function returned False'.