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

  • SEARCH
  • Home
  • 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 855923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:09:25+00:00 2026-05-15T08:09:25+00:00

How do I get the number of arguments passed to a function, such as

  • 0

How do I get the number of arguments passed to a function, such as Plus[2,3,4,5] has 4 arguments passed to it. I was thinking it may involve the use of the function Length and getting the arguments into a list. The intention is to iterate an operation based on the number of arguments for a function. There is probably a simple solution or function but I haven’t come across it yet. Any other ways or suggestions are welcome as well?

  • 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-15T08:09:25+00:00Added an answer on May 15, 2026 at 8:09 am

    Here’s one way:

    In[1]:= foo[args___] := Length[{args}]
    
    In[2]:= foo[1,2,3,4]
    Out[2]= 4
    

    When you define a function like this, the pattern args___ (with 3 trailing underscores) will match a Sequence of 0 or more things. You can’t use Length on a Sequence and have anything sensible happen, so you should wrap args in a List (the {}) first.

    However, belisarius is correct. For a lot of iterative operations, it will be easier and more efficient to use built-in higher-order functions like Map and Fold.

    EDIT to add: Due to way that Mathematica expressions are built on top of bounds-checked arrays, Length is O (1) in time. This might lead you to believe that foo also has O (1) complexity, but you would be wrong. Due to the way pattern-matching works, all of the elements matched by args will be copied into the new List that you then pass to Length, making the complexity O (N). This isn’t necessarily a huge problem, because using really huge argument lists with a function almost invariably means using Apply, which does an O (N) copy anyway, but it’s something you should know.

    EDIT again to add: There’s another way to do this using Length directly on the expression being evaluated (like most of Mathematica’s list-oriented functions, Length can be used on expressions with any head, not just lists). Nothing is copied because no sequences are matched and given new heads, and the function which is having its arguments counted need not have any special attributes like HoldAll. Nonetheless, it is a sleazy hack that exploits a quirk in the pattern-matching machinery by introducing side-effects where side-effects really don’t belong, so I would use it with extreme caution, if at all:

    Module[{n},
     expr : foo[___] /; (n = Length[Unevaluated[expr]]; True) :=
      n]
    

    The variable n could be global, but Module will create (or at least do a good job faking) lexical closures, so you can at least keep your variables local.

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

Sidebar

Related Questions

No related questions found

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.