C++ Programming: From Problem Analysis to Program Design
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 6, Problem 1TF

Mark the following statements as true or false:

  1. To use a predefined function in a program, you need to know only the name of the function and how to use it. (1)

  2. A value-returning function returns only one value. (2, 3)

  3. Parameters allow you to use different values each time the function is called. (2, 7, 9)

  4. When a return statement executes in a user-defined function, the function immediately exits. (3, 4)

  5. A value-returning function returns only integer values. (4)

  6. A variable name cannot be passed to a value parameter. (3, 6)

  7. If a C++ function does not use parameters, parentheses around the empty parameter list are still required. (2, 3, 6)

  8. In C + +, the names of the corresponding formal and actual parameters must be the same. (3, 4, 6)

  9. A function that changes the value of a reference parameter also changes the value of the actual parameter. (7)

  10. Whenever the value of a reference parameter changes, the value of the actual parameter changes. (7)

  11. In C++, function definitions can be nested; that is, the definition of one function can be enclosed in the body of another function. (9)

  12. Using global variables in a program is a better programming style than using local variables, because extra variables can be avoided. (10)

  13. In a program, global constants are as dangerous as global variables. (10)

  14. The memory for a static variable remains allocated between function calls. (11)

(a)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

False.

Explanation of Solution

Given information:

The name of the function and the definition of the function are given.

Explanation:

To use a predefined function in a program, the complete signature of a function should be known. The signature of a function comprises the return type, the name of the function, and the number and data types of the parameters required by the function if any.

Hence, the given statement is FALSE.

(b)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

The function returns a value hence the return type is not void.

Explanation:

A function can either return a value or not return any value. A function that does not returns a value has the return type of void whereas a function that returns a value has a return type such as int, float, and so on. The return type can be any valid data type supported by the language.

A non-void function can have only one return statement. That means, a function can return only one value.

Hence, the given statement is TRUE.

(c)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

A function that takes parameters.

Explanation:

A function that takes parameters can take any value of the specified data type of the parameter. Hence, such a function can be called many times using a different set of values, and calling the same function multiple times with a different list of parameters is called function overloading.

An example program is as follows:

void display(int n)
{ cout<<n<<endl; }
int main()
{ display(1); 
   display(2);
  return 0;
}

Output:

The output of the above example program is as follows:

1

2

In the above program, the display() function is called multiple times with different values and generates different outputs.

Conclusion:

Hence, the given statement is TRUE.

(d)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

A function that returns a value.

Explanation:

A function that returns a value ends with a return statement. The return statement is the last in a value-returning function. The return statement returns a value and passes the control outside of the function. Therefore, it can be said that after executing the return statement, the function exits.

Hence, the given statement is TRUE.

(e)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

False.

Explanation of Solution

Given information:

A function that returns a value.

Explanation:

The nature of the value returned by a function depends upon the return type of the function. The return type of a function can be any valid data type supported by the language. It can be of primitive data type such as int, float, char, and so on and it can also be of a user-defined type such as an object.

Conclusion:

Therefore, a value-returning function will always return a value having type as of return type of function and will not always be an integer. Hence, the given statement is FALSE.

(f)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

A function that takes parameters.

Explanation:

A function that takes value parameters must be passed values and not variable names. A variable name can be passed as a reference variable using the & symbol. This is not acceptable to a function that takes value parameters.

Hence, the given statement is TRUE.

(g)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

A function that does not takes parameters.

Explanation:

A set of parenthesis identifies a function irrespective of whether a function takes parameters or not. A function is always defined and declared using the parenthesis after the function name. If a function is not having any parameters, then the parenthesis will be blank otherwise variables can be declared in the parenthesis. Hence, parentheses in a function are always required.

Thus, the given statement is TRUE.

(h)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

False.

Explanation of Solution

Given information:

A function that takes parameters.

Explanation:

When a function takes parameters, the formal variables refer to the names of the parameters included in the function definition. While the actual variables are the names of the variables that are passed as parameters while calling the parameterized function.

When a function that takes parameters is called, the parameters are passed either using direct values or variable names that have been initialized. In case, variables are passed when calling the parameterized function, the name of the actual variables doesn't have to be the same as the names of the formal variables.

Hence, the given statement is FALSE.

(i)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

A function that takes parameters.

Explanation:

When a variable is passed by reference to a parameterized function, the address of the actual parameter is passed via the reference variable. Any change made to the value at the given address will reflected in the actual parameter also.

Hence, the given statement is TRUE.

(j)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

A function that takes parameters by reference.

Explanation:

Whenever a function to which parameters are passed by reference, the address of the actual parameters is passed. Any change to the value at the given address will reflect in the actual parameter.

Hence, the given statement is TRUE.

(k)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

Nested functions.

Explanation:

C++ allows functions to be nested. That is, the body of one function can be placed inside the body of another function.

An example program is as follows:

int main()
{
   void outside()
  {
      cout << “outside” << endl;
      void inside()
      {
 	cout << “inside” << endl;
       }
  }
  inside();
  return 0;
}

Output:

inside

outside

In the above example, it is seen that the inside() function is defined within the outside() function. While calling the function, the function that is nested, that is inside() function is called.

Conclusion:

Hence, the given statement is TRUE.

(l)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

False.

Explanation of Solution

Given information:

Global variables.

Explanation:

C++ allows global variables to be used in a program. However, there are some issues associated with the usage of global variables. First, the use of global variables makes the maintenance of the code difficult. Secondly, it is difficult to track global variables throughout the program. To avoid such issues, local variables are used.

Hence, the given statement is FALSE.

(m)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

False.

Explanation of Solution

Given information:

Global variables.

Explanation:

C++ allows global constants to be used in a program. Such constants are declared at the beginning of the program outside all the functions. Such constants help in the faster execution of the program and make the code compact since the constant is defined only once throughout the program.

Hence, the given statement is FALSE.

(n)

Expert Solution
Check Mark
Program Plan Intro

To find whether the given statement is true or false.

Program Description Answer

True.

Explanation of Solution

Given information:

Static variables.

Explanation:

C++ allows static variables in a program. The value of a static variable remains unchanged in between the function calls. This is possible since the memory remains allocated to the static variables even between the function calls.

Hence, the given statement is TRUE.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Create a Database in JAVA Netbeans that saves name of the player and how many labyrinths did the player solve. Record the number of how many labyrinths did the player solve, and if he loses his life, then save this number together with his name into the database. Create a menu item, which displays a highscore table of the players for the 10 best scores public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private void checkGameState() {        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");            System.exit(0);        }         if (Math.abs(player.getX() - dragon.getX()) <= 1 &&            Math.abs(player.getY() - dragon.getY()) <= 1) {            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");            System.exit(0);        }    } } public…
Create a Database in JAVA OOP that saves name of the player and how many labyrinths did the player solve. Record the number of how many labyrinths did the player solve, and if he loses his life, then save this number together with his name into the database. Create a menu item, which displays a highscore table of the players for the 10 best scores. Also, create a menu item which restarts the game. public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private void checkGameState() {        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");            System.exit(0);        }         if (Math.abs(player.getX() - dragon.getX()) <= 1 &&            Math.abs(player.getY() - dragon.getY()) <= 1) {            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");…
Change the following code so that the player can see only the neighboring fields at a distance of 3 units.   public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");    private final ImageIcon dragonIcon = new ImageIcon("data/images/dragon.png");    private final ImageIcon wallIcon = new ImageIcon("data/images/wall.png");    private final ImageIcon emptyIcon = new ImageIcon("data/images/empty.png");     public GameGUI(Labyrinth labyrinth, Player player, Dragon dragon) {        this.labyrinth = labyrinth;        this.player = player;        this.dragon = dragon;         setFocusable(true);        addKeyListener(new KeyAdapter() {            @Override            public void keyPressed(KeyEvent e) {                char move = switch (e.getKeyCode()) {                    case KeyEvent.VK_W -> 'W';                    case…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
functions in c programming | categories of function |; Author: Education 4U;https://www.youtube.com/watch?v=puIK6kHcuqA;License: Standard YouTube License, CC-BY