1

Define a function coloredPolygon to create a RegularPolygon of a particular color and number of sides. Use OptionsPattern in the definition to accept two options: color and sides. The first option will be used to specify the color of the polygon and the second option sides will be used to specify the number of sides. Use Options to set the default values for the two options. Set the default color to be Orange and default number of sides to be 3.

Here is my tentative code (with faults):

coloredPolygon := RegularPolygon [color, sides];
Options [coloredPolygon] {color ->Orange, sides->3}
coloredPolygon[] 

Getting this error message:

enter image description here

  • You could do: rp[n_ : 3, col_ : Red] := Graphics[{EdgeForm[{col, Thick}], FaceForm[None], RegularPolygon[n]}] – ubpdqn Sep 12 '21 at 10:23

1 Answers1

1
ClearAll[coloredPolygon]

Options[coloredPolygon] = {color -> Orange, sides -> 3};

coloredPolygon[opts:OptionsPattern[]] :=
  {OptionValue[color], RegularPolygon[OptionValue[sides]]};

coloredPolygon[] 

enter image description here

coloredPolygon[sides -> 5] 

enter image description here

Graphics @ coloredPolygon[sides -> 7, color -> Red] 

enter image description here

Note: You can use both s and "s" to refer to option s; that is, sides -> something and "sides" -> something (similarly, both color -> somecolor and "color" -> somecolor) work:

coloredPolygon[sides -> 7, color -> Red] 

enter image description here

coloredPolygon["color" -> Red, "sides" -> 7] 

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • @kgir Is it not strange that Options command with coloredPolygon used before defining the function coloredPolygon in the next step. – Splendid Digital Solutions Sep 12 '21 at 10:47
  • 1
    @SplendidDigitalSolutions, Options[foo] =... can be before or after foo is defined. – kglr Sep 12 '21 at 10:58
  • @kgir This feature must be because unlike old programming languages like BASIC, this is not a procedural programming in which the second step relies on the first step and ordering is important. – Splendid Digital Solutions Sep 12 '21 at 11:02
  • @SplendidDigitalSolutions, you might find the info in Option Value >> Details useful. – kglr Sep 12 '21 at 11:07
  • 4
    @SplendidDigitalSolutions You have asked several questions that are quizzes or exercises from Wolfram-U courses. This is not the forum for asking those questions. They should be asked on the Wolfram Community site post corresponding to the course. The point of a quiz or exercise is for you to solve it, not ask someone else to solve it for you. – Rohit Namjoshi Sep 12 '21 at 18:41