define a function

I'm trying to define the function below in the main function:


1
2
3
4
5
6
7
  float distance(float x, float y, float pitch)
     {
      float r;  //local variable
      r = sqrt(x * x + y * y);
      Bangle = arctan2(pitch*(y/r) + x , pitch*(x/r) -y);
      return (cos(Bangle),sin(Bangle));
     }

I'm getting an error : a function-definition is not allowed here before ‘{’ token
{
^
I'm not rely sure if I should write it in this way or not?!
any suggestion.
Last edited on
Yo cannot define a function inside another function. Common practice is to declare the prototype above main and implement it below main.
1
2
3
4
5
6
7
8
9
10
11
12
13
// Prototype
float distance(float x, float y, float pitch);

int main()
{
  // code here
}

// Implementation
float distance(float x, float y, float pitch)
{
  // code here
}
Sorry,you mean i have to write the prototype before main function.
the rest in main function
1
2
3
4
5
6
7
8
9
float distance(float x, float y, float pitch);
int main()
{

     float r;  //local variable
      r = sqrt(x * x + y * y);
      Bangle = arctan2(pitch*(y/r) + x , pitch*(x/r) -y);
      return (cos(Bangle),sin(Bangle));
}
Last edited on
Sorry,you mean i have to write the prototype before main function.
the rest in main function


No, he means the prototype above main, and the function definition below main. It's clear in the code he provided is it not?
Last edited on
again I have to define the variables in side the main():
1
2
3
4
float r,x,y,Bangle,pitch;

arctan2 should be written in c++ as  atan2 right?
Last edited on
There's a problem in your return
From what I understand you've already got a cos() and sin() function, so there's no problem with that. But in your return you have
 
return (cos(Bagle), sin(Bagle));

But you only return 2 arguments, whereas distance() needs 3. It should be like this:

 
return (cos(Bagle), sin(Bagle), pitch);
in this case after edited what you told me like this:
1
2
3
4
5
6
7
8
9
10
float distance(float x, float y, float pitch);
int main()
{

     float r;  //local variable
      r = sqrt(x * x + y * y);
      Bangle = arctan2(pitch*(y/r) + x , pitch*(x/r) -y);
      return (cos(Bangle),sin(Bangle),pitch);
}

now i can use the value : Bangle in another part of my code?!
Wait, what are you trying to return there? You've defined your main function to return an int (as all main functions must do). So why are you trying to return a float?

Also, why are you using the comma operator on line 8 there? Do you understand what the comma operator actually does in C++?
I don't know, you mean the x,y, pitch should be int.
I don't know what dose the comma means?!

The comma operator means each term in the expression is evaluated. Then the final value of the entire expression is simply the part after the last comma (the rest is ignored).

I'm trying to understand the meaning of 'pitch' in the function
float distance(float x, float y, float pitch)
In terms of geometry I assume x and y represent a 2-D coordinate. But what would pitch mean?
you can ignore the pitch value( its a constant ) we can ignore it from our calculation, the new version look like this:


1
2
3
4
5
6
7
8
9
10

float distance(float x, float y);
int main()
{

     float r;  //local variable
      r = sqrt(x * x + y * y);
      Bangle = arctan2((y/r) + x ,(x/r) -y);
      return (cos(Bangle),sin(Bangle));
}


this definition to use the value of cos(Bangle) and sin(Bangle) in the return
Last edited on
Ok thanks for the clarification.

You code has the definition of the function inside main(). It should be inside the body of the function itself, as in the example given by Thomas1965 above. Note the name of the arctangent function is atan2() as previously mentioned.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
float distance(float x, float y);  // function prototype declaration

int main()
{
    float d = distance(3, 4);      // example function call
    
}

float distance(float x, float y)   // function definition
{
    float r = sqrt(x * x + y * y);
    double Bangle = atan2((y/r) + x ,(x/r) -y);
    double c = cos(Bangle);
    double s = sin(Bangle);
    return ????   // I don't know what goes here.
}


See tutorial on comma operator here:
http://www.cplusplus.com/doc/tutorial/operators/

names of trig functions detailed here:
http://www.cplusplus.com/reference/cmath/

@Chervil
I need to ask you private question, according to last discussion last month?
Last edited on
Please feel free to ask any question in the forum. My responses are no better and no worse whether in public or private. But in public, the whole forum community both benefits and is able to contribute.
@chervil
Okay , the point is if you still remember you helped me to rewrite python code to c++ last month. I want to add the function above into it (the form above I wrote it similar to python syntax). I don't understand all the part of the code you have written
Last edited on
Thanks for the clarification. I'm looking into it.
@ chervil
I post the code her to see what I have deleted/replace with this new function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

T=100 
N=10000
dt=float(T)/N 

D= 1 
DII=10

def Bfield(x,y,pitch): #pitch value in for loop is const.  
  r = np.sqrt(x**2. + y**2.)
  Bangle =np.arctan2 (pitch*(y/r) + x, pitch*(x/r) -y)
  return [np.cos(Bangle), np.sin(Bangle)]

n= 1000
seed(2)
finalpositions=[]

for number in range(0, 100):
  
  finalpositions.append([])
  x=[]
  y=[]
  x.append(10.) 
  y.append(0.)#0 or 20 for example , to check if the diffusion is isotropic or anisotropic

  for i in range(n):
      #localB = Bfield(x[-1],y[-1])
      localB = Bfield(x[-1],y[-1],0.8)

      s = np.random.normal(0, 1, 2) 
      deltaX= localB[0]*np.sqrt(2*DII*dt)*s[0] - localB[1]*np.sqrt(2*D*dt)*s[1]	  
      deltaY= localB[1]*np.sqrt(2*DII*dt)*s[0] + localB[0]*np.sqrt(2*D*dt)*s[1]
      x.append(x[-1] + deltaX)
      y.append(y[-1] + deltaY)
  finalpositions[-1].append(x)
  finalpositions[-1].append(y)


allxes = [] 
allyes = []


for p in finalpositions:
  allxes.append(p[0][-1])
  allyes.append(p[1][-1])# 
 
p1, = plt.plot(allxes, allyes, 'o') '
 

Last edited on
Based on offline discussion as well as previous related questions, this is what I'd suggest:
1
2
3
4
5
6
7
void distance( double x, double y, double pitch, double result[2] )
{
    double r = sqrt(x * x + y * y);
    double Bangle = atan2(pitch*(y/r) + x, pitch*(x/r) - y);
    result[0] = cos(Bangle);
    result[1] = sin(Bangle);
}



To call the function, define an array which will hold the result
 
    double b[2];    // result will be stored here 


and call the function as required
 
    distance(x, y, pitch, b);    // call the function  

Topic archived. No new replies allowed.