Python-ல் Function என்றால் ஒரு specific work செய்ய எழுதப்பட்ட reusable code block.
ஒரு function-ஐ ஒருமுறை define செய்து, தேவையான அளவு பல முறை call செய்யலாம்.
Simple idea:
Function = ஒரு வேலைக்கான தனி code block
A function is a reusable block of code that performs a specific task. தமிழில்: ஒரு குறிப்பிட்ட வேலை செய்யும் statements-ஐ ஒரு பெயருக்குள் group செய்து, அதை தேவையான போது call செய்து reuse செய்வது Function.
Python-ல் Function என்றால் ஒரு specific work செய்ய எழுதப்பட்ட reusable code block.
ஒரு function-ஐ ஒருமுறை define செய்து, தேவையான அளவு பல முறை call செய்யலாம்.
Simple idea:
Function = ஒரு வேலைக்கான தனி code block
1def greet(): 2 print("Hello")
இங்கே greet() என்பது ஒரு function.
A function is a reusable block of code that performs a specific task.
ஒரு குறிப்பிட்ட வேலை செய்யும் statements-ஐ ஒரு பெயருக்குள் group செய்து, அதை தேவையான போது call செய்து reuse செய்வது Function.
Same code மீண்டும் மீண்டும் எழுத வேண்டும்.
print("Welcome")
print("Welcome")
print("Welcome")def welcome():
print("Welcome")welcome() welcome() welcome()
Function-ஐ ஒரு machine போல நினைத்துக்கொள்ளுங்கள்.
1def add(a, b): 2 return a + b
1print(add(2, 3))
5
5
Python function types-ஐ இரண்டு different ways-ல் classify செய்யலாம்.
Classification 1 - Function உருவாக்கப்பட்ட விதம்
Classification 2 - Arguments மற்றும் Return Value அடிப்படையில்
இந்த இரு classifications-ஐ confuse செய்யக்கூடாது.
Python already provide செய்திருக்கும் ready-made functions தான் Built-in Functions.
நாமே define செய்ய வேண்டியதில்லை.
print() input() len() type() int() float() str() sum() max() min() range()
1print("Python")
இங்கே print() நாமே உருவாக்கவில்லை.
Python already அதை provide செய்துள்ளது.
அதனால்:
1print() = Built-in Function
name = "Python"
1print(len(name))
6
6
1len()
ஒரு built-in function.
Built-in function-க்கு def எழுத தேவையில்லை.
Built-in function = user உருவாக்குவது
Built-in function = Python already provide செய்வது
Programmer தனது requirement-க்கு ஏற்ப உருவாக்கும் function தான் User-defined Function.
def
keyword பயன்படுத்தப்படுகிறது.
1def function_name(): 2 statements
1def greet(): 2 print("Welcome")
Function define செய்துவிட்டோம்.
ஆனால் இது உடனே execute ஆகாது.
greet()
1def greet(): 2 print("Welcome to Python")
Welcome to Python
greet()
Welcome to Python
print("Welcome")
இது function-ஐ create செய்கிறது.
greet()
இது function-ஐ execute செய்கிறது.
def = Define () = Call
ஒரு function-க்கு normal பெயர் இல்லாமல், short expression-ஆக எழுதப்படும் function தான் Lambda Function.
Anonymous Function
என்றும் சொல்வார்கள்.
expression
1square = lambda x: x * x
1print(square(5))
25
25
return x * x
square = lambda x: x * x
இரண்டின் result same.
return
எழுத வேண்டியதில்லை.
Expression result automatically return ஆகும்.
இதன் result automatic return.
x + 10
ஒரு function தன்னைத் தானே call செய்தால் அது Recursive Function.
1def count(n): 2 if n == 0: 3 return
1print(n) 2 count(n - 1)
5 4 3 2 1
count(5)
5 4 3 2 1
count(5)
print 5
call count(4)
print 4
call count(3)
print 3
call count(2)
count(0)
Base condition
return
Function stops.
Recursive function-ல் Base Case மிகவும் important.
RecursionError
வரலாம்.
Recursive Function
=
Self Call + Base Case
இப்போது exam-ல் மிகவும் முக்கியமான classification பார்க்கலாம்.
input வாங்குகிறதா?
output return செய்கிறதா?
இதைத்தான் வைத்து 4 types.
No Arguments and No Return Value
Input வாங்காது
Output return செய்யாது
1def function_name(): 2 statements
1def greet(): 2 print("Hello")
Hello
greet()
Hello
Function-க்கு data outside-லிருந்து கொடுக்கவில்லை.
greet()
brackets empty.
Return value-உம் இல்லை.
அதனால்:
No Argument
No Return
print("Bell Ringing")
ring_bell()Bell function-க்கு input தேவையில்லை.
Just action செய்கிறது.
Arguments and No Return Value
Function input வாங்கும்.
ஆனால் result-ஐ return மூலம் caller-க்கு அனுப்பாது.
1def add(a, b): 2 print(a + b)
30
add(10, 20)
30
a, b
are parameters.
add(10, 20)
10, 20
are arguments.
Function result print செய்கிறது.
ஆனால் return செய்யவில்லை.
print()
return
same இல்லை.
print()
Screen-ல் காட்டும்.
return
Function result-ஐ caller-க்கு back அனுப்பும்.
print(a + b) x = add(10, 20) print(x)
30 None
ஏன் None?
Because function return செய்யவில்லை.
No Arguments but Return Value
Function input caller-லிருந்து வாங்காது.
ஆனால் result return செய்யும்.
1def get_number(): 2 return 100
1x = get_number()
1print(x)
100
100
get_number()
arguments இல்லை.
return 100
மூலம் value back அனுப்புகிறது.
Arguments and Return Value
இது most useful function type.
Input வாங்கும்
+
Output return செய்யும்
1def add(a, b): 2 return a + b
1result = add(10, 20)
1print(result)
30
30
1def add(a, b): 2 return a + b
1x = add(10, 20)
1print(x * 2)
60
60
Because returned value can be reused.
| Type | Argument | Return |
|---|---|---|
| No Argument, No Return | No | No |
| Argument, No Return | Yes | No |
| No Argument, Return | No | Yes |
| Argument, Return | Yes | Yes |
Input = Argument Output = Return
இந்த concept மிகவும் முக்கியம்.
a, b = Parameters
add(10, 20)
10, 20 = Arguments Easy Memory Parameter = Function definition Argument = Function call
return ஒரு function-ன் result-ஐ caller-க்கு அனுப்பும்.
1def square(x): 2 return x * x
1a = square(5)
1a = 25
return execute ஆனதும் function immediately end ஆகும்.
1def test(): 2 print("A") 3 return 4 print("B")
A
test()
A
B execute ஆகாது.
Python function multiple values-ஐ return செய்யலாம்.
1def calculate(a, b): 2 return a + b, a - b
x, y = calculate(10, 5)
1print(x) 2print(y)
15 5
15 5
Technically Python multiple values-ஐ tuple form-ல் return செய்கிறது.
Function arguments-க்கு default value கொடுக்கலாம்.
print("Hello", name)
greet()
greet("Ravi")Hello Student Hello Ravi
default value use ஆகும்
provided value use ஆகும்
Argument-ஐ parameter name மூலம் pass செய்யலாம்.
print(name, age)
student(age=20, name="Ravi")
Ravi 20
இதனை:
Keyword Argument
என்று சொல்வோம்.
student("Ravi", 20)
Ravi → name
20 → age
Positional Argument
use செய்யலாம்.
*args
1def add(*numbers): 2 print(numbers)
(10, 20, 30)
add(10, 20, 30)
(10, 20, 30)
*args usually tuple collect செய்கிறது.
**kwargs
1def student(**data): 2 print(data)
{'name': 'Ravi', 'age': 20}
student(name="Ravi", age=20)
{'name': 'Ravi', 'age': 20}
**kwargs dictionary form-ல் collect செய்கிறது.
Functions │ ├── Built-in │ ├── User-defined │ ├── Lambda │ └── Recursive
User-defined Function │ ├── No Argument + No Return ├── Argument + No Return ├── No Argument + Return └── Argument + Return
Arguments │ ├── Positional ├── Keyword ├── Default ├── *args └── **kwargs
| Built-in | User-defined |
|---|---|
| Python already provides | Programmer creates |
| print() | def add(): |
| len() | def greet(): |
| Ready to use | Need to define |
| Normal Function | Lambda Function |
|---|---|
| Uses def | Uses lambda |
| Has normal function name | Often anonymous |
| Multiple statements possible | Single expression |
| return can be written | Expression result auto returned |
| return | |
|---|---|
| Displays result | Sends result back |
| Screen output | Function output |
| Cannot directly reuse printed value | Returned value can be reused |
1def a(): 2 print(10)
25
return 10
x = b() store செய்யலாம்.
Define but not call
Call செய்யவில்லை.
greet()
Wrong indentation
print("Hello")
print("Hello")
Missing colon
def greet()
Confusing argument and parameter
a, b = parameters. add(10, 20) 10, 20 = arguments.
Thinking print is return
print(a + b)
This prints result.
It does not return it.
D = Define
C = Call add(10, 20) R = Return return a + b
Function define செய்ய keyword?
def
Python ready-made functions?
Built-in functions
Programmer creates?
User-defined function
Anonymous function?
Lambda function
Function calling itself?
Recursive function
Function definition values?
Parameters
Function call values?
Arguments
Result caller-க்கு அனுப்ப?
return
*args collect as?
Tuple
**kwargs collect as?
Dictionary
What are the main types of functions in Python?
Built-in functions
User-defined functions
Lambda functions
Recursive functions
User-defined functions may also be classified based on arguments and return values.
Can a function exist without arguments?
Yes.
print("Hello")
Can a function return no value?
Yes.
None
implicitly.
Difference between parameter and argument?
Parameter is written in function definition.
Argument is supplied during function call.
Can Python return more than one value?
Yes.
return a, b
They are effectively returned as a tuple.
def hello():
print("Hello")
hello() def show(name):
print(name)
show("Ravi") def get_value():
return 50
x = get_value()
print(x) def square(x):
return x * x
print(square(5)) 25
Python Function என்பது ஒரு specific task செய்ய reusable code block. Python-ல் Built-in, User-defined, Lambda மற்றும் Recursive functions முக்கியமான types. User-defined functions arguments மற்றும் return value அடிப்படையில் நான்கு வகையாக பிரிக்கப்படுகின்றன: No Argument-No Return, Argument-No Return, No Argument-Return, Argument-Return. Function concept புரிய முக்கியமான மூன்று words: Parameter, Argument, Return.