In_Progress
In_Progress

Reputation: 31

C++ Function Parameters: Passing Data to Subroutines and variables

#include <iostream>

using namespace std;

void show_menu ()
{
    cout << "Welcome" << endl;
    cout << "1) Match line-up" << endl;
    cout << "2) Match result" << endl;
    cout << "3) Quit program" << endl;

}

int selection ()
{
    cout << "Select option" << endl;

    int input;
    cin >> input;

    return input;
}

int process (int x)
{
    switch(x)
    {
    case 1:
        cout << "Line ups" << endl;
    case 2:
        cout << "Arsenal 3 - Crystal Palace 0" << endl;
    case 3:
        cout << "Quitting" << endl;
    default:
        cout << "Select Option1" << endl;
    }

}

int main ()
{
    show_menu();
    int input2 = selection ();
    process(input2);

    return 0;
}

So this is a code for some menu and input option, i wrote it as a exercise on subroutins, but in the function below i had a problem that i solved throgh some trile and arror, but still i dont get it.

int process (int x)
{
    switch(x)
    {
    case 1:
        cout << "Line ups" << endl;
    case 2:
        cout << "Arsenal 3 - Crystal Palace 0" << endl;
    case 3:
        cout << "Quitting" << endl;
    default:
        cout << "Select Option1" << endl;
    }

}

Why do i need the variable (int x) in order dor this function to work? i have a feel that i dont understand something very basic. pls help)

Upvotes: 0

Views: 422

Answers (2)

Jobin Jose
Jobin Jose

Reputation: 184

The switch part checks whether the value of int x is 1,2,3 or default(something other than 1,2 and 3) and take the appropriate action(the different cout for each case). There is no way for it to check the value of int x if you don't pass int x to the process function. I hope this makes things clear.

Upvotes: 0

RIO
RIO

Reputation: 13

OK , so the function name is process (meaning you need to process something) NOW , in order for your function to process something you need to give it that thing to be processed , right?

and the x variable which is an int data type ( int data type because it reflect the same value that was assigned inside your main method "input") is your argument that will be matched with the proper switch case as specified inside your process function.

one more thing , since you are not returning anything through your function , you don't need to declare it as an int , you only need to declare a data type for a function when you return the same data type , so in this case your function could be void.

hope this will help :)

Upvotes: 1

Related Questions