Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3693302
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:21:42+00:00 2026-05-19T04:21:42+00:00

Suppose I have a function with optional named arguments but I insist on referring

  • 0

Suppose I have a function with optional named arguments but I insist on referring to the arguments by their unadorned names.

Consider this function that adds its two named arguments, a and b:

Options[f] = {a->0, b->0};  (* The default values. *)
f[OptionsPattern[]] := 
  OptionValue[a] + OptionValue[b]

How can I write a version of that function where that last line is replaced with simply a+b?
(Imagine that that a+b is a whole slew of code.)

The answers to the following question show how to abbreviate OptionValue (easier said than done) but not how to get rid of it altogether: Optional named arguments in Mathematica

Philosophical Addendum: It seems like if Mathematica is going to have this magic with OptionsPattern and OptionValue it might as well go all the way and have a language construct for doing named arguments properly where you can just refer to them by, you know, their names. Like every other language with named arguments does. (And in the meantime, I’m curious what workarounds are possible…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:21:43+00:00Added an answer on May 19, 2026 at 4:21 am

    Here is the final version of my answer, containing the contributions from the answer by Brett Champion.

    ClearAll[def];
    SetAttributes[def, HoldAll];
    def[lhs : f_[args___] :> rhs_] /; !FreeQ[Unevaluated[lhs], OptionsPattern] :=
       With[{optionNames = Options[f][[All, 1]]},
         lhs := Block[optionNames, optionNames = OptionValue[optionNames]; rhs]];
    def[lhs : f_[args___] :> rhs_] := lhs := rhs;
    

    The reason why the definition is given as a delayed rule in the argument is that this way we can
    benefit from the syntax highlighting. Block trick is used because it fits the problem: it does not interfere with possible nested lexical scoping constructs inside your function, and therefore there is no danger of inadvertent variable capture. We check for presence of OptionsPattern since this code wil not be correct for definitions without it, and we want def to also work in that case.
    Example of use:

    Clear[f, a, b, c, d];
    Options[f] = {a -> c, b -> d};
    (*The default values.*)
    def[f[n_, OptionsPattern[]] :> (a + b)^n]
    

    You can look now at the definition:

    Global`f
    f[n$_,OptionsPattern[]]:=Block[{a,b},{a,b}=OptionValue[{a,b}];(a+b)^n$]
    
    f[n_,m_]:=m+n
    
    Options[f]={a->c,b->d}
    

    We can test it now:

    In[10]:= f[2]
    Out[10]= (c+d)^2
    
    In[11]:= f[2,a->e,b->q]
    Out[11]= (e+q)^2
    

    The modifications are done at “compile – time” and are pretty transparent. While this solution saves
    some typing w.r.t. Brett’s, it determines the set of option names at “compile-time”, while Brett’s – at “run-time”. Therefore, it is a bit more fragile than Brett’s: if you add some new option to the function after it has been defined with def, you must Clear it and rerun def. In practice, however, it is customary to start with ClearAll and put all definitions in one piece (cell), so this does not seem to be a real problem. Also, it can not work with string option names, but your original concept also assumes they are Symbols. Also, they should not have global values, at least not at the time when def executes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function that performs some task (this is in Python pseudocode):
Suppose I have a function in a single threaded program that looks like this
Suppose I have a function that tries to protect a global counter using this
Of course most languages have library functions for this, but suppose I want to
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a function def app2(environ, start_response) If I know that a server
Suppose I have a function named caller, which will call a function named callee:
Suppose I have a function f that takes a vector v and returns a
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
I have a function I've written that was initially supposed to take a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.