({{, (RefLink[AskFunction, paclet:ref/AskFunction])[body] 
evaluates body, interactively asking for values specified by (RefLink[Ask, paclet:ref/Ask])[…] and related constructs. }, {, (RefLink[AskFunction, paclet:ref/AskFunction])[{"(Subscript[key, 1])"->(Subscript[spec, 1]),"(Subscript[key, 2])"->(Subscript[spec, 2]),…},body]
specifies how values associated with the (Subscript[key, i]) should be asked for.}})

OpenersNotesSection(Spacer1)Details and Options

The following special constructs can appear in the body of an (RefLink[AskFunction, paclet:ref/AskFunction]):

(RefLink[AskFunction, paclet:ref/AskFunction])[…][] evaluates in a notebook, displaying a form every time it encounters a construct that requires asking for a value.

(RefLink[CloudDeploy, paclet:ref/CloudDeploy])[AskFunction[…]] yields a (RefLink[CloudObject, paclet:ref/CloudObject]) corresponding to an active version of the (RefLink[AskFunction, paclet:ref/AskFunction]).

(RefLink[AskFunction, paclet:ref/AskFunction])[…][<|"(Subscript[key, k])"->(Subscript[input, k])|>] represents an (RefLink[AskFunction, paclet:ref/AskFunction]) where the value associated with "(Subscript[key, k])" is taken to be (Subscript[input, k]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (12)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Set up an (RefLink[AskFunction, paclet:ref/AskFunction]):

ask=AskFunction[Ask["x"->"Number"]+Ask["y"->"Number"]];

Activate it and fill in the inputs:

ask[]

Or deploy it to the cloud:

CloudDeploy[ask]

You can programmatically provide known values:

ask2 = ask[<|"x" -> "3"|>]

Now only the second question will be asked:

ask2[]

OpenersExampleSection1(Spacer1)Scope  (3)

Provide a first argument to (RefLink[AskFunction, paclet:ref/AskFunction]) to specify how to ask for values:

CloudDeploy[AskFunction[<|"x" -> "Number", "y" -> "Number"|>, Ask["x"]^Ask["y"]]]

(RefLink[AskFunction, paclet:ref/AskFunction]) uses the evaluation semantics of the Wolfram Language to skip the questions that are not needed:

CloudDeploy[AskFunction[If[Ask[{"married", "Are you married?"}-> {"Yes" -> True, "No" -> False}], Ask[{"spouse-name", "What is the name of your spouse?"} -> "String"]]; Style["Thank you for taking our survey!", Bold, 36]]]

Selecting No skips the question about the spouse name:

Use the same syntax as (RefLink[FormFunction, paclet:ref/FormFunction]) and (RefLink[FormObject, paclet:ref/FormObject]) to specify the appearance of the questions:

CloudDeploy[AskFunction[Ask["n" -> <|"Interpreter" -> Restricted["Integer", {1, 20}], "Control" -> Slider|>]!]]

OpenersExampleSection2(Spacer1)Properties & Relations  (7)

Use (RefLink[AskFunction, paclet:ref/AskFunction]) in conjunction with (RefLink[Ask, paclet:ref/Ask]) for simple questions or to get the value associated with a key:

CloudDeploy[AskFunction[Ask[{"x", "Input a number"} -> "Integer"]; Ask["x"]^2]]

(RefLink[AskConfirm, paclet:ref/AskConfirm]) can be used to rewind the evaluation to the point before a value was known:

CloudDeploy[AskFunction[Ask[{"sum", "what is 2+2?"}-> "Number"]; If[Ask["sum"]≠4, AskConfirm["sum", "are you sure 2+2 = `value`?"]; "wrong", "correct"]]]

Use (RefLink[AskAppend, paclet:ref/AskAppend]) to accumulate inputs:

CloudDeploy[AskFunction[AskDisplay["Enter three countries"];Do[AskAppend["country"->"Country"],{3}];Ask["country"]]]

Use (RefLink[AskDisplay, paclet:ref/AskDisplay]) to display messages:

CloudDeploy[AskFunction[AskDisplay["Welcome to TaxReturnPro™!"];Ask[{"income", "How much did you make this year?"}-> "CurrencyAmount"];AskDisplay[ If[Ask["income"] > (QuantityPrefix[1 (10^6), "$" , US dollars, "USDollars"]), "whoa!","puny…"]]]]

(RefLink[AskTemplateDisplay, paclet:ref/AskTemplateDisplay]) takes a function or a template to be applied to the current values:

CloudDeploy[AskFunction[Ask[{"n", "How many sides?"} -> Restricted["Integer", {3, Infinity}]]; AskTemplateDisplay[Graphics[RegularPolygon[#n]]&]]]

Use (RefLink[AskedQ, paclet:ref/AskedQ]) to verify if a key has been asked:

CloudDeploy[AskFunction[<|{"name", "What's your name?"} -> "String"|>,While[!AskedQ["name"], Ask["name"]]]]

(RefLink[AskedValue, paclet:ref/AskedValue]) returns the value without causing a form to be displayed:

CloudDeploy[AskFunction[If[Ask[{"age", "How old are you?"} -> "Number"]≥ 21, Ask[{"drink", "Do you drink?"} ->{"Yes" -> True, "No" -> False}]]; Replace[AskedValue["drink"],{ _Missing :> "not applicable", True -> "drinking", False -> "not drinking"}]]]

OpenersExampleSection3(Spacer1)Possible Issues  (1)

When (RefLink[AskFunction, paclet:ref/AskFunction]) is deployed in the cloud, the body is reevaluated at each visit on a clean kernel. Because of this, functions that return random values might return different values at each step:

CloudDeploy[AskFunction[a=RandomInteger[{1,10}];AskDisplay[a];AskDisplay[a]]]

A possible workaround is using (RefLink[SeedRandom, paclet:ref/SeedRandom]) to initialize the pseudorandom number generator with a seed that depends on previous values or on values such as (RefLink[$RequesterAddress, paclet:ref/$RequesterAddress]):

CloudDeploy[AskFunction[
Ask[{"name", "What's your name?"}-> "String"];SeedRandom[Ask["name"]];a=RandomInteger[{1,10}];AskDisplay[a];AskDisplay[a]
]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Ask, paclet:ref/Ask])  ▪  (RefLink[FormFunction, paclet:ref/FormFunction])  ▪  (RefLink[Input, paclet:ref/Input])  ▪  (RefLink[AskAppend, paclet:ref/AskAppend])  ▪  (RefLink[AskConfirm, paclet:ref/AskConfirm])  ▪  (RefLink[AskedValue, paclet:ref/AskedValue])  ▪  (RefLink[AskDisplay, paclet:ref/AskDisplay])  ▪  (RefLink[AskTemplateDisplay, paclet:ref/AskTemplateDisplay])  ▪  (RefLink[AskState, paclet:ref/AskState])  ▪  (RefLink[AskedQ, paclet:ref/AskedQ])  ▪  (RefLink[CloudDeploy, paclet:ref/CloudDeploy])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Creating Form Interfaces & Apps, paclet:guide/CreatingFormsAndApps])}, {▪(RefLinkPlain[Setting Up User Interactions, paclet:guide/SettingUpUserInteractions])}, {▪(RefLinkPlain[Cloud Functions & Deployment, paclet:guide/CloudFunctionsAndDeployment])}, {▪(RefLinkPlain[Custom Interface Construction, paclet:guide/CustomInterfaceConstruction])}})}})