Ask Question
15 March, 13:00

Write the definition of a function named newbie that receives no parameters and returns true the first time it is invoked (when it is a "newbie"), and that returns false every time that it is invoked after that.

+1
Answers (1)
  1. 15 March, 15:05
    0
    The function definition to this question can be described as follows:

    bool newbie () / /defining a bool method newbie

    {

    static bool y = true; / / defining a static bool variable y and assign value true

    bool t1=y; / /defining bool variable t1 and assign value of variable y

    y=false; / / assigning value in variable y

    return t1; / / return value of variable t1

    }

    Explanation:

    The program to this question can be described as follows:

    Program:

    #include / /defining header file

    using namespace std;

    bool newbie () / /defining a bool method newbie

    {

    static bool y = true; / / defining a static bool variable y and assign value true

    bool t1=y; / /defining bool variable t1 and assign value of variable y

    y=false; / / assigning value in variable y

    return t1; / / return value of variable t1

    }

    int main () / /defining main method

    {

    int s, s1; / /defining integer variable s, and s1

    s=newbie (); / / variable s that call method

    cout<
    s1=newbie (); //variable s that call method

    cout<
    return 0;

    }

    Output:

    1

    0

    Description:

    The description of the above function can be described as follows:

    In the above method definition a boolean method "newbie" is declared, which can't accept any parameters, inside the method, two boolean variable "y and t1" is declared. In this variable, variable y is a static boolean variable, that assigns a true value, and in t1 variable, we hold static variable value. In the next step, the "y" variable assigns a value, that is false and returns variable t1 value. In the next step, the main method is declared, inside these two integer variable s, s1 is declared, that calls a method, and prints it value in the first time, it will print 1, that means the false and second time it will print 0, that means false.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write the definition of a function named newbie that receives no parameters and returns true the first time it is invoked (when it is a ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers