From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23667 invoked by alias); 19 Dec 2014 05:34:34 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 19591 Received: (qmail 14711 invoked from network); 19 Dec 2014 05:34:24 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=PYxIXZlY c=1 sm=1 tr=0 a=FT8er97JFeGWzr5TCOCO5w==:117 a=kj9zAlcOel0A:10 a=q2GGsy2AAAAA:8 a=oR5dmqMzAAAA:8 a=-9mUelKeXuEA:10 a=A92cGCtB03wA:10 a=XnJXkEtF-vN3BZLP1YQA:9 a=CjuIK1q_8ugA:10 From: Bart Schaefer Message-id: <141218213450.ZM28822@torch.brasslantern.com> Date: Thu, 18 Dec 2014 21:34:50 -0800 In-reply-to: <54939F50.50102@gmx.com> Comments: In reply to Eric Cook "Could someone clarify how math functions work?" (Dec 18, 10:45pm) References: <54939F50.50102@gmx.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: Could someone clarify how math functions work? MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Dec 18, 10:45pm, Eric Cook wrote: } Subject: Could someone clarify how math functions work? } } zsh -c 'add() ( for arg; do (( n += arg )); done; print n: $n ); } functions -M add; print results: $(( add(1,2,3) ))' } } Outputs: } n: 6 } results: 3 } } where as: } zsh -c 'add() { local arg n; for arg; do (( n += arg )); done; print n: } $n }; functions -M add; print results: $(( add(1,2,3) ))' } } Outputs: } n: 6 } results: 6 } } Is that expected behavior? If so, could you explain why? The following might illustrate: zsh -c 'add() ( for arg; do (( n += arg )); done; print n: $n ); functions -M add; print results: $(( add(3,2,1) ))' zsh -c 'add() ( for arg; do (( n += arg )); done; print n: $n ); functions -M add; print results: $(( add(3,1,2) ))' When you define add() with parens ( ) around the function body, you are running the function body in a subshell. The "last arithmetical expression evaluated" IN THE CURRENT SHELL is the processing of the argument list of the call to add(), which is done left-to-right and is therefore "3" in the original example. When you define add() with braces { } you are running the function body in in the current shell, so the last expression is the last assignment in the for-loop body.