home > 比較編 > Perl, PHP, Python, Ruby > functions >

ForNext

Only Do What Only You Can Do

151. function with private state

VBScript

JScript

Perl

use feature state;

sub counter {
  state $i = 0;
  ++$i;
}

print counter() . "\n";

PHP

function counter() {
  static $i = 0;
  return ++$i;
}

echo counter();


Python

# state not private:
def counter():
  counter.i += 1
  return counter.i

counter.i = 0
print(counter())

Ruby

none







PowerShell

Scala

F#

C

C++

C++Builder

VC++

C#

Java

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system