Constant numbers as parameters

Hello guys. How should I pass constants previoulsy defened as parameters of a funtion. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdlib>
#include <iostream>
#define apples 10
#define oranges 40

void f1 (int apples, int oranges)
{
coutz<<apples<<" & "<<oranges<<endl;
}

int main ()
{
}


But it returns errors. What am I doing wrong?
Sorry, in line 8 is "cout" instead of "coutz". The error persists.
put std:: before cout and endl, or put using namespace std; after your includes.

Also after preprocessing, this will resolve to int f1(int 10, int 40) {/**/}. This is obviously causing syntax errors.
Last edited on
1
2
3
4
5
#define apples 10
#define oranges 40

void f1 (int apples, int oranges)
{


You can't do this. The #defines are polluting your namespace. The compiler will see this code and do this:

1
2
3
4
5
#define apples 10
#define oranges 40

void f1 (int 10, int 40)  // <-nonsense!  error!
{


Because when it sees "apples", it replaces it with whatever apples was #defined as.

This is exactly why you should avoid using #define. Use actual constants instead:

1
2
3
4
5
const int apples = 10;  // much better!
const int oranges = 40;

void f1 (int apples, int oranges)  // <-now this is OK
{
Topic archived. No new replies allowed.