Predict the Output
7. What is the output of the following programs?
A) #include <iostream>
using namespace std;
int function(int);
int main()
{
int x = 10;
cout << function(x) << endl;
return 0;
}
int function(int num)
{
if (num <= 0)
return 0;
else
return function(num - 1) + num;
}
B) #include <iostream>
using namespace std;
void function(int);
int main()
{
int x = 10;
function(x);
return 0;
}
void function(int num)
{
if (num > 0)
{
for (int x = 0; x < num; x++)
cout << '*';
cout<< endl;
function(num - 1);
}
}
C) #include <cstdlib>
#include <string>
#include <iostream>
using namespace std:
void function(string str, int pos);
int main(int argc, char** argv)
{
string names = "Adam and Eve";
function(names, 0);
return 0;
}
void function (string str. int pos)
{
if (pos < str. length())
{
function(str, pos+1);
cout << str[pos]:
}
}
Want to see the full answer?
Check out a sample textbook solutionChapter 14 Solutions
Starting Out with C++: Early Objects (9th Edition)
Additional Engineering Textbook Solutions
Artificial Intelligence: A Modern Approach
C Programming Language
Concepts of Programming Languages (11th Edition)
Database Concepts (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Why do you need to include function prototypes in a program that contains user-defined functions? (5)arrow_forwardExplanation: Default arguments should always be declared at the rightmost side of the parameter list but the above function has a normal variable at the rightmost side which is a syntax error, therefore the function gives an error. 1. #include int fun(int=0, int = 0); int main() { cout << fun(5); return 0; } int fun(int x, int y) { return (x+y); } а) -5 b) 0 c) 10 d) 5 2. int main() { int a=10; int b,c; b =a++; c = a; cout<arrow_forwardC++arrow_forwardC++ pleasearrow_forwardC programing Given the function below, what would the function call question3(10, 101) return? int question3(int a, int b) {if (a == 0) return b;if (b == 0) return a;return question3(10*a+b%10, b/10);}arrow_forwardC++ Functions provide a means to modularize applications Write a function called "Calculate" takes two double arguments returns a double result For example, the following is a function that takes a single "double" argument and returns a "double" result double squareArea(double side){ double lArea; lArea = side * side; return lArea;}arrow_forward3. (a) A PHP function is defined as follows: function planets ($arr) { if ($arr [1]) { print "Mercury"; } else { for ($i=0; $i <= $arr [2]; $i++) {print $arr [3] [$i]; } } print count ($arr); } Recall that array $arr can be defined using a statement of the form: $arr = array (...); (i) Give an array $arr such that planets ($arr) prints Mercury For this case, what is the output of the instruction print count ($arr); Give an array $arr such that planets ($arr) prints Venus (ii)arrow_forwardTrue or False : The concept of function abstraction hinders our code development by confusing us with the details of the function.arrow_forwardThe statement in line1 is an example of int squareT (int x, int y) //line1 { if (xarrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_iosRecommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning