0

I'm trying to learn C++ at my own pace via an online course. There is a function declaration and also there is an integer declaration just after that, which is q.

I don't understand what purpose the "q" serves in the code.

I tried to print out each step and it didn't make sense. I literally don't see the point of having a "q" in the foo function or what it does.

#include<stdio.h>


int foo (int q) {
    int x = 1;
    return (q + x);
}
int main (){
   int x = 0;
   while (x < 3) {
    printf ("%i\n", x);
      x = x + foo(x);
    }
}

This code gives me 0 1 Seems like the "q" is incrementing x but I don't see the reason why because we didn't assign it to anything but just saying (int q)

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
AliceX
  • 51
  • 4
  • Why is this tagged C#? Do you know C# already? `q` is the name of the parameter. Whatever you pass as an argument will be called `q` inside of `foo()`, no matter what it was called outside of `foo()` – Thomas Weller Aug 09 '19 at 19:22
  • 3
    *"I literally don't see the point of having a "q" in the foo function"* - Then remove it and see what happens. You are very much encouraged to tinker with your code as you learn. – David Aug 09 '19 at 19:23
  • https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jesper Juhl Aug 09 '19 at 19:24
  • `q` is not incrementing `x`. It is `x = x + ...` which increments x. – Thomas Weller Aug 09 '19 at 19:24
  • 6
    `int x = 0;` at `main()` is completely unrelated to `int x = 1;` in `foo()`. – πάντα ῥεῖ Aug 09 '19 at 19:25

5 Answers5

2

In the code you posted, q is an argument to the function foo and foo will return the value of its argument (q) +1.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Excuse me? Then why it's not stated as: int foo (int q) {return (q + 1);} ? – AliceX Aug 09 '19 at 19:49
  • 3
    @AliceX: It indeed can be stated exactly as that. Try it and find out. – David Aug 09 '19 at 19:54
  • 1
    @AliceX `int foo (int q) { int x = 1; return (q + x);` does exactly that. It just uses an intermediate (pointless) variable `x`. The generated machine code (with optimizations enabled) will be exactly the same though. – Jesper Juhl Aug 09 '19 at 20:13
1
int foo (int q)

declares a function that takes an input parameter, q. The value q takes, in foo, depends on the value that is passed to it. In this case, when you call it with:

foo(x)

You state: run the code in foo, but give to q, upon entering the function, the value of x in main(). When you start programming, at first, this is misleading as q and x appear to be different variable. You'll get used to it.

You must also realise that the x in foo has nothing to do with the x in main. Those are different variables.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
1

It is a call by value function call where foo() get call by passing the value to q variable. If you not declare any argument in the function it will give an error because it's int q only to get the value from the main function. So here when first time foo get call - it's return 1 and after that it's return 2 . So in second time x=1+2=3 3>3 condition false. It print only 0,1.

1

q is a parameter. That is, it is something in a function that accepts a value from the caller. As w3schools puts it:

Information can be passed to functions as a parameter. Parameters act as variables inside the function.

Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma:

Probably couldn't have explained it better myself.

This code gives me 0 1 Seems like the "q" is incrementing x but I don't see the reason why because we didn't assign it to anything but just saying (int q)

Ah, so, we actually did assign it to something. It might just not be noticeable. We did it when we called foo:

x = x + foo(x);

Here, by putting x between the parenthesis, we set q to the value of x (which appears to be 0, in this case). That means, q will have the value of x (0) inside the function call.

We also could have set it to something else, like 3:

x = x + foo(3);

Here, q is set to 3, because 3 is inside the parenthesis.

0

Thank you for all your answers and comments.

It seems like the q is the argument passed in and x is just there for representing an integer (redundantly).

It'd have been better if the code just used the integer 1 as:

int foo (int q) {
    return (q + 1);
AliceX
  • 51
  • 4