0

I want to call a function within a loop. The function takes three parameters. I want two of them to stay unchanged within a loop while only the third is affected through my loop.

Edited Version : Here is a sample code:

void Function(int v1, int v2, int v3) {};

int main(int argc, char** argv) { 
    int a;
    int b;

    for (int i = 0; i < 5; ++i) {
        Function(int a, int b, i)
    }
}

This is the error I get when I compile this code :

error: 
      expected expression Function(int v1, int v2, i)....

Is there a way to realize my idea?

J. Doe
  • 305
  • 1
  • 11
xava
  • 293
  • 1
  • 2
  • 16

3 Answers3

4

You're using the wrong syntax, and that's why you are getting the error.

When declaring a function, you specify the type of the parameters (and don't forget the return type):

void Function(int v1, int v2, int v3) {
    // your function body here
};

When invoking a function, you simply pass the parameters:

Function(1, 2, 3);

In your case, the compiler thought you were trying to declare another function, while in the body of another.

So, in the end, and assuming v1 and v2 are already defined, you need to write your main as

int main () { 

  // don't forget to define v1 and v2
  int v1 = 10;
  int v2 = 20;   

  for (int i=0; i <5; i++) {
    Function(v1, v2, i)
  }
}

Hope this helps!

mystery_doctor
  • 404
  • 2
  • 11
2

You forgot, I think you don't know, a host of things. Keep track of the comments. However, you have to read a C book for beginners.

//whereIsReturnTypeofTheFunction ???
void Function(int v1, int v2, int v3) {};

int main () {

    // you need variables to be passed to Function
    int a;
    int b;

    // for condition is vetted by looking i, not int, in the middle
    for (int i=0; i <5; i++) {
        Function(a,  b, i); 

    }
}
1

Do something like

void Function(int v1, int v2, int v3) { };

int main () { 
  int v1 = 0;
  int v2 = 0;
  for (int i = 0; i < 5; i++) {
    Function(v1, v2, i);
  }
}

Some problems with your code:

for (int i=0; int <5; i++)

Here you use int instead of i for the run condition.

Function(int v1, int v2, i)

Here you declare v1 and v2, but they are undefined (they have a unknown value). Declare and initialize them before the loop. Also no semicolon at the end.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
izlin
  • 2,129
  • 24
  • 30