DART function: calling function, deleting function; passing arguments to function, lexical scoping

Dart functions अपनी best new tricks के साथ: => arrow bodies, defaults के साथ optional [positional] और {named} parameters, और lexical scoping, जहाँ nested functions अपने birth के around की variables याद रखते हैं।

12 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Flutter से पहले आख़िरी Stop

एक Dart topic बाकी है, और यही वह है जिसे Unit 3 हर single line में इस्तेमाल करेगी: functions।

इसका आधा आपके पास पहले से 3 languages से है: define कीजिए, call कीजिए, arguments pass कीजिए, return कीजिए। बाकी आधा genuinely new equipment है: एक one-line arrow form, वे parameters जिन्हें callers name से address करते हैं, और एक scoping rule जो एक function को अपनी जन्मभूमि की variables साथ carry करने देता है।

Urgency क्यों? कोई भी Flutter file खोलिए और आप screen भरे हुए named arguments देखेंगे। यह lesson उनका passport है।

Theory

Define और Call करना (और एक Syllabus Note)

पूरा form lighter shoes वाली Java है:

int seatsLeft(int total, int booked) {

return total - booked;

}

और जब body एक single expression हो, Dart arrow form offer करता है:

int seatsLeft(int total, int booked) => total - booked;

वही function, एक line: => का मतलब है "यह expression return करो"।

(Honest note: आपकी syllabus line पढ़ती है "deleting function": ऐसा कोई Dart concept exist नहीं करता: इसे एक function defining करना पढ़िए, जो exam certainly इसका मतलब रखता है।)

Practical

Arrows, Optionals, Named Parameters, और एक Closure

int seatsLeft(int total, int booked) => total - booked;   // arrow body

// [optional positional] with a default:
String daySlot(String event, [int day = 1]) => '$event (day $day)';

// {named} parameters with defaults: Flutter's house style
String ticket(String name, {String event = 'Garba Night', int day = 1}) {
  return '$name: $event, day $day';
}

void main() {
  print(seatsLeft(350, 310));                       // 40

  print(daySlot('Rangoli'));                        // Rangoli (day 1)
  print(daySlot('Rangoli', 2));                     // Rangoli (day 2)

  print(ticket('Riya'));                            // defaults fill in
  print(ticket('Aman', day: 2, event: 'Robo Race'));// any order, by name

  // Lexical scoping: register() sees counter, born beside it
  var counter = 0;
  void register() {
    counter++;
  }
  register();
  register();
  print('Registrations: $counter');                 // 2
}

Theory

Arguments Receive करने के तीन तरीके

  • Required positional: ticket('Riya'): order का मतलब है, अब तक की हर language की तरह
  • Optional positional, square brackets में defaults के साथ: daySlot('Rangoli', 2): omit हो सकता है, फिर भी position से
  • Named, braces में defaults के साथ: ticket('Aman', day: 2, event: 'Robo Race') की तरह call होता है: कोई भी order, call site पर self-documenting

Flutter ने named क्यों चुना: एक widget 10 optional settings लेता है, और Text('Hi', style: ..., maxLines: 2) configuration जैसा पढ़ता है, commas की puzzle जैसा नहीं। हर Unit 3 constructor यही pattern है।

Quiz

Listing के ticket function को देखते हुए, print(ticket('Riya')); क्या output करता है?

  1. Riya: Garba Night, day 1: named parameters के defaults fill होते हैं
  2. एक compile error: event और day supply नहीं हुए
  3. Riya: null, null
  4. Riya: event, day: parameter names literally print होते हैं
Show the answer

Riya: Garba Night, day 1: named parameters के defaults fill होते हैं

Defaults के साथ declared named parameters optional हैं: इन्हें omit कीजिए और defaults ('Garba Night', 1) step in करते हैं: यही exactly है जो 10-setting Flutter constructors को 2 arguments के साथ callable बनाता है। Option B named parameters को required की तरह treat करता है: Dart में possible, पर सिर्फ़ एक explicit required keyword के साथ, जिसे यह declaration इस्तेमाल नहीं करता। Option C imagine करता है defaults exist ही नहीं करते; option D imagine करता है string interpolation fail हो रहा है। Self-test के लायक follow-up: ticket('Aman', day: 2) देता है Aman: Garba Night, day 2: names किसी भी order में जाते हैं, हर एक independently optional।

Think first

Function जो अपना Home याद रखता है

Listing में, register() counter को modify करता है, एक variable जो खुद के OUTSIDE है। दो runs बाद main 2 print करता है। वह rule state कीजिए जो यह allow करता है, और याद कीजिए आप BCA405-01 में यही behaviour कहाँ मिले, फिर tap कीजिए।

Show the answer

Lexical scoping: एक nested function उस scope की variables देखता है (और modify कर सकता है) जहाँ यह DEFINED हुआ था: register counter के बगल में born हुआ, तो counter इसकी पहुँच में हमेशा के लिए है। आप यह BCA405-01 में बिना नाम के मिले: हर AJAX handler और Node callback जिसने अपने around के function की variables इस्तेमाल कीं वह lexical scoping काम कर रहा था (JavaScript यह rule share करता है)। यह Flutter में enormously matter करता है: हर onPressed callback exactly इसी तरह widget की surrounding state तक पहुँचता है। Function + remembered surroundings = एक closure: वह term margin में लिखने लायक।

Watch out

Function-Writing Slips

Braces के साथ Arrow: => एक EXPRESSION लेता है: => { return x; } लिखना दो बार ग़लत है: braces का मतलब है block body, arrow की ज़रूरत नहीं।

Named Parameters के लिए Positional Call: ticket('Aman', 'Robo Race') compile नहीं होता: braces-declared parameters सिर्फ़ NAME से address होते हैं।

Bracket Confusion: [optional positional] बनाम {named}: square position रखता है, curly names पाता है: declaration styles को mix up करना standard 1-mark ambush है।

Theory

Unit 2 बंद होती है: आप Dart बोलते हैं

Inventory: Dart क्यों exist करता है (JIT/AOT), यह कैसे चलता है (3 doors), इसकी syntax (accents noted) और अब इसके functions arrows, named parameters और closures के साथ: exact toolkit जिसमें Flutter code लिखा जाता है। यह line पढ़िए और देखिए क्या यह पहले से parse होता है: ElevatedButton(onPressed: register, child: Text('Book')): 2 NAMED arguments वाला एक constructor, एक function, एक widget। यह parse होता है। आप ready हैं: Unit 3, Flutter खुद।

Summary

Key takeaways

  • Java की तरह Define/call कीजिए; => एक single expression return करता है (कोई braces नहीं, कोई return keyword नहीं)।
  • Required positional; defaults के साथ [optional positional]; defaults के साथ {named}, name से any-order call होते हैं।
  • Named parameters Flutter की house style हैं: constructors configuration जैसे पढ़ते हैं।
  • Defaults omitted parameters fill करते हैं: ticket('Riya') = Riya: Garba Night, day 1; seatsLeft(350, 310) = 40।
  • Lexical scoping: nested functions अपने birth scope की variables देखते और modify करते हैं (closures): BCA405-01 के callbacks, आख़िरकार named।
  • Syllabus का 'deleting function' DEFINING की तरह पढ़ा जाता है: कोई delete-a-function exist नहीं करता।
  • Memory hook: arrows return करते हैं, braces name करते हैं, functions home याद रखते हैं।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Working with DART

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

DART function: calling function, deleting function; passing arguments to function, lexical scoping · Mobile Application Development - 2 (option B) · Gri-Learn