Is this considered defining the variable

Can i consider n to be declared already when the function bool prime is being declared as shown below???



#include<iostream>
#include <cmath>
using namespace std;

bool prime(int n);

int main(){
...................................
..............................
............................}

I believe you do not even need to write n. The compiler just wants to know the type of the argument being passed. This is for the declaration of 'prime' anyway.

Ex: you could write:

1
2
3

bool prime(int);
Last edited on
bool prime(int n);
This is function prototype and it do not declare variables. As Robert wrote it can be written without n variable.
To declare variable you need to do it in function implementation
bool prime(int n) { //function code }

And variable n will be declared in function prime, but not in other functions.

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

Using the above as an example. can i assume that int a and int b has been declared already when i type "int addition (int a, int b)"
Last edited on
I am guessing you mean if you can use them elsewhere.
Yes you can use the variables 'a' and 'b' outside of the functions addition as they are local variables to the function addition.
Yes you can use the variables 'a' and 'b' outside of the functions addition as they are local variables to the function addition.


???

How you can use local variables outside a function? Local variables excised only in local namespace. So they can't be used out of function. Only global variables can be used out of function.

qawsed51:
yes now it is not a function prototype, but implementation. So it is declared. And you can use it in that function.
@qawsed51

Can i consider n to be declared already when the function bool prime is being declared as shown below???

#include<iostream>
#include <cmath>
using namespace std;

bool prime(int n);

int main(){



Yes of course, n is declared.. But its scope ends at the end of enclosing function declarator. The variable has function prototype scope.


1
2
3
4
5
6
7
8
9
@qawsed51
int addition (int a, int b)
 {
 int r;
 r=a+b;
 return (r);
 }
 
Using the above as an example. can i assume that int a and int b has been declared already when i type "int addition (int a, int b)" 


In this case a and b are also declared and have block scope.
Last edited on
Thanks i understand it now.
Topic archived. No new replies allowed.