© 2026 schoolict.net (Ruwan Mangala Suraweera)
9.9 Functions: Built-in and User-definedශ්රිත: ගොඩනැගූ සහ පරිශීලක නිර්වචිත
Grade 13 A/L ICT · Lesson 913 ශ්රේණිය A/L ICT · පාඩම 9
In this videoමේ වීඩියෝවේ
- Functions are named reusable blocks.ශ්රිත නැවත භාවිත කළ හැකි කොටස් ය.
- Parameters receive arguments.පරාමිති arguments ලබා ගනී.
- return sends a value back.return අගයක් ආපසු යවයි.
- Default values fill missing arguments.පෙරනිමි අගයන් නැති arguments පුරවයි.
- Local variables live only inside the function.Local විචල්ය ශ්රිතය තුළ පමණි.
Notesසටහන්
Syllabus 9.9. A sub-program (function) is a named block that does one task. Python has built-in functions (len, max, round) and you can write user-defined functions with parameters, return values and default values. The scope of a variable decides where it can be used: local variables live only inside their function; global variables live for the whole program.විෂය නිර්දේශය 9.9. උප වැඩසටහනක් (ශ්රිතයක්) එක් කාර්යයක් කරන නම් කළ කොටසකි. Python හි ගොඩනැගූ ශ්රිත (len, max, round) ඇති අතර පරාමිති, ප්රතිදාන අගය සහ පෙරනිමි අගය සහිත පරිශීලක නිර්වචිත ශ්රිත ලිවිය හැක. විචල්යයක විෂය පථය එය භාවිත කළ හැකි තැන තීරණය කරයි: local විචල්ය ශ්රිතය තුළ පමණි; global විචල්ය සම්පූර්ණ වැඩසටහන පුරා ජීවත් වේ.
1. Structure of a function1. ශ්රිතයක ව්යුහය
- def name(parameters): starts the function; the indented body follows.def name(parameters): ශ්රිතය ආරම්භ කරයි; indent කළ ශරීරය පසුව.
- Parameters receive values; the values passed in a call are arguments.පරාමිති අගයන් ලබා ගනී; කැඳවීමේදී යවන අගයන් arguments ය.
- return sends a value back to the caller; without it the function returns None.return අගයක් කැඳවන්නාට යවයි; නැත්නම් None.
- A default value (price=100) is used when that argument is left out.පෙරනිමි අගය (price=100) argument එක නොදුන් විට භාවිත වේ.
🐍 Parameters, return values, defaults and scopeපරාමිති, ප්රතිදාන අගය, පෙරනිමි සහ විෂය පථය
count = 0 # global variable def total_price(qty, price=100): # price has a default value return qty * price # return value def add_visit(): global count count += 1 def show_local(): count = 99 # local: hides the global return count print(total_price(3)) print(total_price(3, 250)) add_visit(); add_visit() print("Local:", show_local(), "Global:", count) print(len("Lanka"), max(4, 9, 2), round(3.567, 1)) # built-in
▶ Outputප්රතිදානය
300 750 Local: 99 Global: 2 5 9 3.6
total_price(3) uses the default 100 → 300; total_price(3, 250) → 750. add_visit() runs twice and changes the global count to 2. show_local() makes a local count = 99 that disappears when the function ends, so it prints “Local: 99 Global: 2”. Built-ins give len(“Lanka”) = 5, max = 9, round(3.567, 1) = 3.6.total_price(3) පෙරනිමි 100 → 300; total_price(3, 250) → 750. add_visit() දෙවරක් global count 2 කරයි. show_local() හි local count = 99 ශ්රිතය අවසන් වූ විට නැති වේ; “Local: 99 Global: 2”. len = 5, max = 9, round = 3.6.
| Local variableLocal විචල්යය | Global variableGlobal විචල්යය |
|---|---|
| Created inside a functionශ්රිතය තුළ සාදයි | Created outside all functionsසියලු ශ්රිතවලින් පිටත |
| Lives only while the function runsශ්රිතය ධාවනය වන තුරු පමණි | Lives until the program endsවැඩසටහන අවසන් වන තුරු |
| Used only in that functionඑම ශ්රිතයේ පමණි | Readable everywhere; change it inside with globalසෑම තැනම කියවිය හැක; global මගින් වෙනස් කළ හැක |
Key Wordsප්රධාන පද
Sample Q&Aආදර්ශ ප්රශ්න සහ පිළිතුරු
1Built-in vs user-defined?ගොඩනැගූ සහ පරිශීලක?
2Parameter vs argument?පරාමිතිය සහ argument?
3total_price(3) returns?total_price(3)?
4Why does Global stay 2?Global 2 ඇයි?
5Lifetime of a local variable?local ආයු කාලය?
6Why use return values?return ඇයි?
MCQබහුවරණ ප්රශ්න
Choose the correct answer for each question.සෑම ප්රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.
1len, max and round arelen, max
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Built-in.ගොඩනැගූ.
2The value passed in total_price(3, 250) for price is an250
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
Argument.Argument.
3total_price(3) returnstotal_price(3)
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
3 × 100.3 × 100.
4total_price(3, 250) returnstotal_price(3, 250)
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
3 × 250.3 × 250.
5After two add_visit() calls the global count iscount
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E
Two increments.දෙවරක්.
6show_local() returnsshow_local
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Local value.local අගය.
7A variable that exists only while its function runs isශ්රිතය තුළ
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
Local.Local.
8round(3.567, 1) givesround
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E
One decimal.දශම 1.
9Which term means: “Named block of code that performs a task”?“කාර්යයක් කරන නම් කළ කේත කොටස” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
Sub-program – Named block of code that performs a task.උප වැඩසටහන – කාර්යයක් කරන නම් කළ කේත කොටස.
10Which term means: “Function provided by Python such as len or max”?“len, max වැනි Python සපයන ශ්රිතය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
Built-in function – Function provided by Python such as len or max.ගොඩනැගූ ශ්රිතය – len, max වැනි Python සපයන ශ්රිතය.
11Which term means: “Function written by the programmer with def”?“def මගින් ක්රමලේඛකයා ලියන ශ්රිතය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
User-defined function – Function written by the programmer with def.පරිශීලක නිර්වචිත ශ්රිතය – def මගින් ක්රමලේඛකයා ලියන ශ්රිතය.
12Which term means: “Variable in a function definition that receives a value”?“අගයක් ලබා ගන්නා ශ්රිත නිර්වචනයේ විචල්යය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
Parameter – Variable in a function definition that receives a value.පරාමිතිය – අගයක් ලබා ගන්නා ශ්රිත නිර්වචනයේ විචල්යය.
13Which term means: “Actual value passed to a function in a call”?“කැඳවීමේදී ශ්රිතයට යවන සැබෑ අගය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Argument – Actual value passed to a function in a call.Argument – කැඳවීමේදී ශ්රිතයට යවන සැබෑ අගය.
14Which term means: “Value sent back to the caller by return”?“return මගින් කැඳවන්නාට යවන අගය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
Return value – Value sent back to the caller by return.ප්රතිදාන අගය – return මගින් කැඳවන්නාට යවන අගය.
15Which term means: “Value a parameter takes when no argument is given”?“argument නොදුන් විට පරාමිතිය ගන්නා අගය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
Default value – Value a parameter takes when no argument is given.පෙරනිමි අගය – argument නොදුන් විට පරාමිතිය ගන්නා අගය.
16Which term means: “Part of a program where a variable can be used”?“විචල්යයක් භාවිත කළ හැකි වැඩසටහනේ කොටස” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
Scope – Part of a program where a variable can be used.විෂය පථය – විචල්යයක් භාවිත කළ හැකි වැඩසටහනේ කොටස.
17Which term means: “Variable that exists only inside its function”?“ශ්රිතය තුළ පමණක් පවතින විචල්යය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
Local variable – Variable that exists only inside its function.Local විචල්යය – ශ්රිතය තුළ පමණක් පවතින විචල්යය.
18Which term means: “Variable defined outside functions for the whole program”?“සම්පූර්ණ වැඩසටහනට ශ්රිතවලින් පිටත නිර්වචිත විචල්යය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
Global variable – Variable defined outside functions for the whole program.Global විචල්යය – සම්පූර්ණ වැඩසටහනට ශ්රිතවලින් පිටත නිර්වචිත විචල්යය.
19Which term means: “How long a variable exists in memory”?“විචල්යයක් මතකයේ පවතින කාලය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E
Lifetime – How long a variable exists in memory.ආයු කාලය – විචල්යයක් මතකයේ පවතින කාලය.
20Which term means: “Statement that runs a function by its name”?“නමින් ශ්රිතයක් ධාවනය කරන ප්රකාශය” යන්නෙන් අදහස් වන පදය කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E
Function call – Statement that runs a function by its name.ශ්රිත කැඳවීම – නමින් ශ්රිතයක් ධාවනය කරන ප්රකාශය.
MCQ answer keyMCQ පිළිතුරු
- C – Built-in.ගොඩනැගූ.
- A – Argument.Argument.
- C – 3 × 100.3 × 100.
- B – 3 × 250.3 × 250.
- E – Two increments.දෙවරක්.
- C – Local value.local අගය.
- D – Local.Local.
- E – One decimal.දශම 1.
- D – Sub-program – Named block of code that performs a task.උප වැඩසටහන – කාර්යයක් කරන නම් කළ කේත කොටස.
- B – Built-in function – Function provided by Python such as len or max.ගොඩනැගූ ශ්රිතය – len, max වැනි Python සපයන ශ්රිතය.
- A – User-defined function – Function written by the programmer with def.පරිශීලක නිර්වචිත ශ්රිතය – def මගින් ක්රමලේඛකයා ලියන ශ්රිතය.
- D – Parameter – Variable in a function definition that receives a value.පරාමිතිය – අගයක් ලබා ගන්නා ශ්රිත නිර්වචනයේ විචල්යය.
- C – Argument – Actual value passed to a function in a call.Argument – කැඳවීමේදී ශ්රිතයට යවන සැබෑ අගය.
- B – Return value – Value sent back to the caller by return.ප්රතිදාන අගය – return මගින් කැඳවන්නාට යවන අගය.
- A – Default value – Value a parameter takes when no argument is given.පෙරනිමි අගය – argument නොදුන් විට පරාමිතිය ගන්නා අගය.
- A – Scope – Part of a program where a variable can be used.විෂය පථය – විචල්යයක් භාවිත කළ හැකි වැඩසටහනේ කොටස.
- B – Local variable – Variable that exists only inside its function.Local විචල්යය – ශ්රිතය තුළ පමණක් පවතින විචල්යය.
- D – Global variable – Variable defined outside functions for the whole program.Global විචල්යය – සම්පූර්ණ වැඩසටහනට ශ්රිතවලින් පිටත නිර්වචිත විචල්යය.
- E – Lifetime – How long a variable exists in memory.ආයු කාලය – විචල්යයක් මතකයේ පවතින කාලය.
- E – Function call – Statement that runs a function by its name.ශ්රිත කැඳවීම – නමින් ශ්රිතයක් ධාවනය කරන ප්රකාශය.
Structured Questionsව්යුහගත ප්රශ්න
1Explain built-in and user-defined functions with examples.ශ්රිත වර්ග දෙක. (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
2Label the parts of def total_price(qty, price=100).කොටස් නම් කරන්න. (5 marksලකුණු 5)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
3Compare local and global variables (three points).local සහ global. (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
4Write a function area(l, w=1) that returns l × w and call it twice.area() ශ්රිතය. (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
5Explain why show_local() does not change the global count.show_local(). (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
🎮 Game🎮 ක්රීඩා
Summary & Downloadsසාරාංශය සහ බාගත කිරීම්
Key pointsප්රධාන කරුණු
- Functions are named reusable blocks.ශ්රිත නැවත භාවිත කළ හැකි කොටස් ය.
- Parameters receive arguments.පරාමිති arguments ලබා ගනී.
- return sends a value back.return අගයක් ආපසු යවයි.
- Default values fill missing arguments.පෙරනිමි අගයන් නැති arguments පුරවයි.
- Local variables live only inside the function.Local විචල්ය ශ්රිතය තුළ පමණි.
Downloadsබාගත කිරීම්
Resource Book Lesson Note Download Questions Syllabus Sinhala LMS Simulator Tool
