# Create a function that takes two parameters and returns one output?

**URL:** <https://community.wolfram.com/t/create-a-function-that-takes-two-parameters-and-returns-one-output/11349>\
**Category:** Questions\
**Created:** [September 30, 2017, 9:07pm UTC](https://community.wolfram.com/t/create-a-function-that-takes-two-parameters-and-returns-one-output/11349 "2017-09-30T21:07:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhaykin](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@akhaykin](https://community.wolfram.com/u/akhaykin)\
**Post date:** [September 30, 2017, 9:07pm UTC](https://community.wolfram.com/t/create-a-function-that-takes-two-parameters-and-returns-one-output/11349/1 "2017-09-30T21:07:05Z")

</div>

Hello Wolfram Community,

I have a function that takes two parameters and is essentially supposed to return one of two things: “line Crossed” or “no line Crossed”. However with this current function I am getting Null as my output. Why is this and how can I achieve my desired output. Here is the code:

```
needleCross[length_, distance_] := {
  Block[
   {midPoint = RandomReal[distance], 
    rotationAngle = RandomReal[{-Pi/2, Pi/2}]},
   
   xCoordLinePoints = {midPoint - (Cos[rotationAngle]*length/2), 
     midPoint + (Cos[rotationAngle]*length/2)}];
  
  If[xCoordLinePoints[[1]] <= 0 || xCoordLinePoints[[2]] >= distance, 
   Print["Line Crossed"], Print["No Line Crossed"]]}

```

any advice would help!

Thanks

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [September 30, 2017, 10:51pm UTC](https://community.wolfram.com/t/create-a-function-that-takes-two-parameters-and-returns-one-output/11349/2 "2017-09-30T22:51:07Z")

</div>

Welcome to Wolfram Community! Please make sure you know the rules: [https://wolfr.am/READ-1ST](https://wolfr.am/READ-1ST)

The rules explain how to format your code properly. If you do not format code, it may become corrupted and useless to other members. Please **EDIT** your posts and make sure code blocks start on a new paragraph and look framed and colored like this.

```
int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

```

 ![ScreenShot2017-04-28at9.43.33AM.png](https://global.discourse-cdn.com/wolfram/original/3X/f/3/f3367c082cbad32b79855f70628ea173f845653e.png)

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [September 30, 2017, 11:44pm UTC](https://community.wolfram.com/t/create-a-function-that-takes-two-parameters-and-returns-one-output/11349/3 "2017-09-30T23:44:13Z")

</div>

Hi,

when I run it it does print out the result and gives the output Null.

If you only want the output you might want to use:

```
needleCross[length_, distance_] := 
{Block[{midPoint = RandomReal[distance], rotationAngle = RandomReal[{-Pi/2, Pi/2}]}, 
 xCoordLinePoints = {midPoint - (Cos[rotationAngle]*length/2), midPoint + (Cos[rotationAngle]*length/2)}];
 If[xCoordLinePoints[[1]] <= 0 || xCoordLinePoints[[2]] >= distance, "Line Crossed", "No Line Crossed"]}[[1]];

```

So now you can simulate Buffon’s needle experiment to estimate Pi. We know that \pi \approx \frac{2 \cdot length}{distance \cdot P}. P is the probability to hit a line. So lets get an estimate for P.

If your function is correct then

```
M = 1000000; P = N[(Count[Table[needleCross[1, 1.2], {M}], "Line Crossed"]/M), 10]

```

should give you an estimate of the probability that a needle crosses a line. Then an estimate of Pi should be

```
2./(1.2*P)

```

which in my case gives 3.13967, 3.1468, 3.13999, 3.13775, 3.14344 for a couple of different runs.

If we assume someone really keen on the experiment would run it 400 times on 8000 needles. Then we would get:

```
estimatesP = Monitor[Table[M = 8000; P = N[(Count[Table[needleCross[1, 1], {M}], "Line Crossed"]/M), 10], {i, 1, 400}], i];
piestimates = 2./(1*#) & /@ estimatesP;
dist = SmoothKernelDistribution[piestimates]; Plot[
 PDF[dist, x], {x, 3.03, 3.28}, 
 Epilog -> Line[{{Mean[dist], 0}, {Mean[dist], 20}}], 
 Filling -> Bottom, PlotTheme -> "Marketing", 
 LabelStyle -> Directive[Bold, Medium], ImageSize -> Large]

```

 ![ScreenShot2017-10-01at00.59.45.png](https://global.discourse-cdn.com/wolfram/original/3X/6/8/684b303fd6bed73fa2bc6768247cbb39b3b82b11.png)

The expected value is:

```
Mean[SmoothKernelDistribution[piestimates]]

```

or equivalently

```
Mean[piestimates]

```

which equals 3.14029.

If we assume that the values are Gaussian distributed we can also represent that:

```
Plot[PDF[EstimatedDistribution[piestimates, 
   NormalDistribution[\[Mu], \[Sigma]]], x], {x, 3.03, 3.28}, 
 Epilog -> Line[{{Mean[dist], 0}, {Mean[dist], 20}}], 
 Filling -> Bottom, PlotTheme -> "Marketing", 
 LabelStyle -> Directive[Bold, Medium], ImageSize -> Large]

```

 ![ScreenShot2017-10-01at01.04.26.png](https://global.discourse-cdn.com/wolfram/original/2X/f/fc9862d3f1cfde9a7ffc60102c220898f77fb289.png)

Cheers,

Marco
