YouTip LogoYouTip

Functions Call By Name

Scala Function Call-by-Name | Tutorial

Tutorial -- Scala Tutorial Scala TutorialScala IntroductionScala Installation and Environment ConfigurationScala Basic SyntaxScala Data TypesScala Literals Scala Escape Characters Scala VariablesScala Access ModifiersScala OperatorsScala IF...ELSE StatementsScala LoopsScala Methods and FunctionsScala ClosuresScala StringsScala ArraysScala CollectionScala IteratorScala Classes and ObjectsScala TraitScala Pattern MatchingScala Regular ExpressionsScala Exception HandlingScala ExtractorScala File I/O Scala Loops Scala Closures

Scala Function Call-by-Name

Scala Functions Scala Functions Scala's interpreter has two ways when parsing function arguments:
  • Call-by-value: calculates the value of the parameter expression first, and then applies it inside the function;
  • Call-by-name: applies the unevaluated parameter expression directly inside the function
Before entering the function, the call-by-value method has already finished calculating the value of the parameter expression, while call-by-name calculates the value of the parameter expression inside the function. This causes a phenomenon: every time call-by-name is used, the interpreter will calculate the value of the expression once.
object Test {
← Functions Named ArgumentsScala Functions β†’