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

Dart functions તેના best new tricks સાથે: => arrow bodies, optional [positional] અને {named} parameters defaults સાથે, અને lexical scoping, જ્યાં nested functions તેમના birth ની આસપાસના variables ને remember કરે છે.

12 min read · 9 cards · 2 checks

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


Theory

Flutter પહેલાં last stop

એક Dart topic બાકી છે, અને તે એ છે જે Unit 3 દરેક single line માં use કરશે: functions.

અડધું તમે already 3 languages થી own કરો છો: define, call, pass arguments, return. બીજું અડધું genuinely new equipment છે: one-line arrow form, parameters જે callers by name address કરે છે, અને scoping rule જે function ને તેના birthplace ના variables ને carry કરવા દે છે.

શા માટે urgency? કોઈ પણ Flutter file ને open કરો અને તમે જોશો named arguments screen ને fill કરતા. આ lesson એ તેમનો passport છે.

Theory

Defining અને calling (અને એક syllabus note)

full form એ Java છે lighter shoes સાથે:

int seatsLeft(int total, int booked) {

return total - booked;

}

અને જ્યારે body એ single expression હોય, Dart arrow form ને offer કરે છે:

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

Same function, one line: => એટલે "return this expression".

(Honest note: તમારું syllabus line વાંચે છે "deleting function": કોઈ such Dart concept exist નથી કરતું; તેને defining function તરીકે read કરો, જે exam certainly means કરે છે.)

Practical

Arrows, optionals, named parameters, અને closure

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

// [optional positional] default સાથે:
String daySlot(String event, [int day = 1]) => '$event (day $day)';

// {named} parameters defaults સાથે: Flutter નું 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() counter ને see કરે છે, તેની બાજુમાં born
  var counter = 0;
  void register() {
    counter++;
  }
  register();
  register();
  print('Registrations: $counter');                 // 2
}

Theory

ત્રણ ways arguments ને receive કરવા

  • Required positional: ticket('Riya'): order meaning ને carry કરે છે, જેમ અત્યાર સુધી દરેક language માં
  • Optional positional, square brackets માં defaults સાથે: daySlot('Rangoli', 2): may be omitted, હજુ by position
  • Named, braces માં defaults સાથે: called as ticket('Aman', day: 2, event: 'Robo Race'): any order, call site પર self-documenting

શા માટે Flutter એ named ને chose કર્યું: widget 10 optional settings ને લે છે, અને Text('Hi', style: ..., maxLines: 2) configuration જેમ reads થાય છે, commas નું puzzle નહીં. દરેક Unit 3 constructor આ pattern છે.

Quiz

listing ના ticket function ને આપેલ, print(ticket('Riya')); શું output કરે છે?

  1. Riya: Garba Night, day 1: named parameters ના defaults fill in થાય છે
  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 in થાય છે

Named parameters જે defaults સાથે declared છે તે optional છે: તેમને omit કરો અને defaults ('Garba Night', 1) step in કરે છે: એ precisely છે શું 10-setting Flutter constructors ને 2 arguments સાથે callable બનાવે છે. Option B named parameters ને required તરીકે treats કરે છે: Dart માં possible, પણ ફક્ત explicit required keyword સાથે, જે આ declaration use નથી કરતું. Option C defaults ને exist નથી કરતા imagine કરે છે; option D string interpolation ને fail થતું imagine કરે છે. Follow-up worth self-testing: ticket('Aman', day: 2) આપે છે Aman: Garba Night, day 2: names કોઈ પણ order માં જાય છે, દરેક independently optional.

Think first

function જે તેના home ને remember કરે છે

listing માં, register() counter ને modify કરે છે, variable OUTSIDE itself. બે runs પછી main 2 print કરે છે. rule ને state કરો જે આને permit કરે છે, અને recall કરો કે તમે same behaviour ને ક્યાં meet કર્યું હતું BCA405-01 માં, પછી tap.

Show the answer

Lexical scoping: nested function તે scope ના variables ને see (અને modify) કરી શકે છે જ્યાં તે DEFINED થઈ હતી: register counter ની બાજુમાં born થઈ હતી, તેથી counter તેની reach માં life માટે છે. તમે આને BCA405-01 માં meet કર્યું હતું name વગર: દરેક AJAX handler અને Node callback જે variables ને function ની આસપાસથી use કરતું હતું તે lexical scoping હતું work પર (JavaScript rule ને share કરે છે). તે enormously matters કરે છે Flutter માં: દરેક onPressed callback widget ના surrounding state સુધી exactly આ રીતે reaches કરે છે. Function + remembered surroundings = closure: term worth writing margin માં.

Watch out

Function-writing slips

Arrow સાથે braces: => EXPRESSION ને લે છે: => { return x; } લખવું બે વાર wrong છે: braces એ block body નો અર્થ કરે છે, કોઈ arrow needed નહીં.

Named parameters માટે Positional call: ticket('Aman', 'Robo Race') compile નથી થતું: braces-declared parameters ને ફક્ત BY NAME address કરવામાં આવે છે.

Bracket confusion: [optional positional] vs {named}: square position ને keep કરે છે, curly names ને gain કરે છે: declaration styles ને mix up કરવું એ standard 1-mark ambush છે.

Theory

Unit 2 closes: તમે Dart બોલો છો

Inventory: શા માટે Dart exists કરે છે (JIT/AOT), તે કેવી રીતે runs કરે છે (3 doors), તેની syntax (accents noted) અને હવે તેના functions arrows, named parameters અને closures સાથે: exact toolkit જે Flutter code ને written છે. આ line ને read કરો અને જુઓ કે શું તે already parse થાય છે: ElevatedButton(onPressed: register, child: Text('Book')): constructor 2 NAMED arguments સાથે, એક function, એક widget. તે parse થાય છે. તમે ready છો: Unit 3, Flutter itself.

Summary

Key takeaways

  • Define/call જેમ Java માં; => single expression ને return કરે છે (કોઈ braces નહીં, કોઈ return keyword નહીં).
  • Required positional; [optional positional] defaults સાથે; {named} defaults સાથે, called any-order by name.
  • Named parameters એ Flutter નું house style છે: constructors configuration જેમ reads થાય છે.
  • Defaults omitted parameters ને fill કરે છે: ticket('Riya') = Riya: Garba Night, day 1; seatsLeft(350, 310) = 40.
  • Lexical scoping: nested functions તેમના birth scope ના variables ને see અને modify કરે છે (closures): BCA405-01 ના callbacks, named at last.
  • Syllabus નું 'deleting function' એ DEFINING તરીકે reads થાય છે: કોઈ delete-a-function exist નથી કરતું.
  • Memory hook: arrows return કરે છે, braces name કરે છે, functions home ને remember કરે છે.

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