Tweeted twitter.com/StackMma/status/835655706590216192
2 fixed grammar, improved formating
source | link

I have defined a function as follows:

fDistance[pointA_, pointB_: {0, 0}, metric_: "taxi"] := 
 Module[{interval}, 
  If[metric == "taxi", 
   interval = 
    Abs[pointB[[1]] - pointA[[1]]] + Abs[pointB[[2]] - pointA[[2]]], 
   interval = 
    Sqrt[(pointB[[1]] - pointA[[1]])^2 + (pointB[[2]] - 
        pointA[[2]])^2]]; interval]

Then

fDistance[{3, 4}]
(*7; ok*)

fDistance[{3, 4}, {1, 2}, "euclid"]
(*2 Sqrt[2]; ok*)

But

fDistance[{3, 4}, "euclid"]
(*error message*)

How can I avoid that Mathematica assign the string literal 'euclid' to the variable pointB? It should have returned 5 as the output below shows

fDistance[{3, 4}, {0, 0}, "euclid"]
(*5*)

Is isAnd as a second question: Is it possible to call the function as followsin the following manner

fDistance[pointB={1, 2}, metric='normal', pointA={3, 4}]

that is, its arguments are paired with a keyword instead of being in the correct order according to the function denition?

Thank you very much.

I have defined a function as follows:

fDistance[pointA_, pointB_: {0, 0}, metric_: "taxi"] := 
 Module[{interval}, 
  If[metric == "taxi", 
   interval = 
    Abs[pointB[[1]] - pointA[[1]]] + Abs[pointB[[2]] - pointA[[2]]], 
   interval = 
    Sqrt[(pointB[[1]] - pointA[[1]])^2 + (pointB[[2]] - 
        pointA[[2]])^2]]; interval]

Then

fDistance[{3, 4}]
(*7; ok*)

fDistance[{3, 4}, {1, 2}, "euclid"]
(*2 Sqrt[2]; ok*)

But

fDistance[{3, 4}, "euclid"]
(*error message*)

How can I avoid that Mathematica assign the string literal 'euclid' to the variable pointB? It should have returned 5 as the output below shows

fDistance[{3, 4}, {0, 0}, "euclid"]
(*5*)

Is is possible to call the function as follows

fDistance[pointB={1, 2}, metric='normal', pointA={3, 4}]

that is, its arguments are paired with a keyword?

I have defined a function as follows:

fDistance[pointA_, pointB_: {0, 0}, metric_: "taxi"] := 
 Module[{interval}, 
  If[metric == "taxi", 
   interval = 
    Abs[pointB[[1]] - pointA[[1]]] + Abs[pointB[[2]] - pointA[[2]]], 
   interval = 
    Sqrt[(pointB[[1]] - pointA[[1]])^2 + (pointB[[2]] - 
        pointA[[2]])^2]]; interval]

Then

fDistance[{3, 4}]
(*7; ok*)

fDistance[{3, 4}, {1, 2}, "euclid"]
(*2 Sqrt[2]; ok*)

But

fDistance[{3, 4}, "euclid"]
(*error message*)

How can I avoid that Mathematica assign the string literal 'euclid' to the variable pointB? It should have returned 5 as the output below shows

fDistance[{3, 4}, {0, 0}, "euclid"]
(*5*)

And as a second question: Is it possible to call the function in the following manner

fDistance[pointB={1, 2}, metric='normal', pointA={3, 4}]

that is, its arguments are paired with a keyword instead of being in the correct order according to the function denition?

Thank you very much.

1
source | link

define a function with optional arguments

I have defined a function as follows:

fDistance[pointA_, pointB_: {0, 0}, metric_: "taxi"] := 
 Module[{interval}, 
  If[metric == "taxi", 
   interval = 
    Abs[pointB[[1]] - pointA[[1]]] + Abs[pointB[[2]] - pointA[[2]]], 
   interval = 
    Sqrt[(pointB[[1]] - pointA[[1]])^2 + (pointB[[2]] - 
        pointA[[2]])^2]]; interval]

Then

fDistance[{3, 4}]
(*7; ok*)

fDistance[{3, 4}, {1, 2}, "euclid"]
(*2 Sqrt[2]; ok*)

But

fDistance[{3, 4}, "euclid"]
(*error message*)

How can I avoid that Mathematica assign the string literal 'euclid' to the variable pointB? It should have returned 5 as the output below shows

fDistance[{3, 4}, {0, 0}, "euclid"]
(*5*)

Is is possible to call the function as follows

fDistance[pointB={1, 2}, metric='normal', pointA={3, 4}]

that is, its arguments are paired with a keyword?