Return Values and Parameters in Python
Python function-ஐ சரியாக புரிந்து கொள்ள இரண்டு words ரொம்ப முக்கியம்:
Parameter A parameter is a variable written in the function definition to receive a value from the function call. தமிழில்: Function call-லிருந்து வரும் value-ஐ receive செய்ய function definition-ல் எழுதப்படும் variable தான் Parameter.
Return Values and Parameters in Python
Python function-ஐ சரியாக புரிந்து கொள்ள இரண்டு words ரொம்ப முக்கியம்:
1def add(a, b): 2 return a + b
1result = add(10, 20) 2print(result)
Parameter
A parameter is a variable written in the function definition to receive a value from the function call.
Function call-லிருந்து வரும் value-ஐ receive செய்ய function definition-ல் எழுதப்படும் variable தான் Parameter.
1def add(a, b): 2 return a + b
30
இரண்டும் parameters.
a
b
Number 1 → [ ]
Number 2 → [ ]
இந்த empty places தான் parameters.
Function
def add(a, b)
a = first value வாங்கும் இடம்
b = second value வாங்கும் இடம்
add(10, 20)
a = 10
b = 20
Function Definition
def add(a, b):
↑ ↑
ParametersCall:
add(10, 20)
↑ ↑
Arguments
இது மிகவும் முக்கியமான exam concept.
return a + b add(10, 20)
a, b = Parameters
add(10, 20)
10, 20 = Arguments
P = Placeholder A = Actual value Parameter = Placeholder Argument = Actual value
add()
def add():
a = 10
b = 20
print(a + b)30
ஆனால் மீண்டும் 50 + 60 calculate செய்ய வேண்டுமென்றால் function-க்குள் values change செய்ய வேண்டும்.
Parameters பயன்படுத்தினால்:
1def add(a, b): 2 print(a + b)
add(10, 20) add(50, 60) add(100, 200)
add(10, 20) add(50, 60) add(100, 200)
30 110 300
ஒரே function different values-க்கு வேலை செய்கிறது.
இதுதான்:
Reusability
1def multiply(x, y): 2 print(x * y)
20
multiply(5, 4)
20
multiply(5, 4)
x and y parameters.
Function call.
Function ஒரு parameter மட்டும் அல்ல.
Multiple parameters வைத்துக்கொள்ளலாம்.
def student(name, age, mark):
print(name)
print(age)
print(mark)student("Ravi", 20, 85)
name = "Ravi" age = 20 mark = 85
Parameter இல்லாமலும் function உருவாக்கலாம்.
print("Hello")
greet()
Parameters = 0
Arguments = 0
இந்த function
print(a + b)
normally two arguments expect செய்கிறது.
add(10, 20)
add(10)
Because one required argument missing.
print(a + b) add(10)
Missing required positional argument
a = 10 b = ? b-க்கு value இல்லை.
Normal arguments order அடிப்படையில் parameter-க்கு assign ஆகும்.
def student(name, age):
print(name)
print(age)student("Ravi", 20)
name = Ravi age = 20
இதனை Positional Arguments என்கிறோம்.
1def subtract(a, b): 2 print(a - b)
5
subtract(10, 5)
5
But:
subtract(5, 10)
-5
ஏன்?
Position change ஆகிவிட்டது.
Parameter name சொல்லி value கொடுக்கலாம்.
def student(name, age):
print(name)
print(age)student(age=20, name="Ravi")
Ravi 20
இங்கே order change ஆனாலும் problem இல்லை.
Because parameter names explicitly given.
Parameter-க்கு default value கொடுக்கலாம்.
print("Hello", name)
greet()
Hello Student
Call with argument:
greet("Ravi")
Hello Ravi
name = default parameter value
Student
use ஆகும்.
Passed value
use ஆகும்.
Required parameter first.
Default parameter after it.
def student(name, age=18):
print(name, age)def student(name="Ravi", age):
non-default parameter cannot normally follow a default parameter in this form.
use செய்யலாம்.
*args
1def numbers(*x): 2 print(x)
(10, 20, 30, 40)
numbers(10, 20, 30, 40)
(10, 20, 30, 40)
Tuple
ஆக collect ஆகும்.
1def total(*numbers): 2 print(sum(numbers))
60
total(10, 20, 30)
60
**kwargs
multiple keyword values collect செய்யும்.
print(data)
student(name="Ravi", age=20, mark=90)
{'name': 'Ravi', 'age': 20, 'mark': 90}
*args → Tuple
**kwargs → Dictionary
Memory:
- → many positional values
** → many keyword values
Return Value
A return value is the result sent back by a function to the place where the function was called.
Function தனது processing முடிந்த பிறகு result-ஐ function call நடந்த இடத்துக்கு திருப்பி அனுப்புவது Return Value.
இந்த 30 தான் return value.
1def function_name(parameters): 2 return value
1def add(a, b): 2 return a + b
1def add(a, b): 2 return a + b
1result = add(10, 20)
1print(result)
30
30
அதாவது return screen-ல் print செய்யாது.
return a + b
Value-ஐ caller-க்கு back அனுப்பும்.
இது மிக முக்கியமான concept.
print()
def add(a, b):
print(a + b)
add(10, 20)
30
Screen-ல் result காட்டுகிறது.
1return 2def add(a, b): 3 return a + b
1x = add(10, 20) 2print(x)
30
30
இங்கே 30 முதலில் x-க்கு வருகிறது.
| print() | return |
|---|---|
| Screen-ல் காட்டும் | Caller-க்கு value அனுப்பும் |
| Display purpose | Function result purpose |
| Value reuse difficult | Returned value reuse செய்யலாம் |
| Function continue ஆகலாம் | return function-ஐ end செய்கிறது |
Using print
print(x * x) a = square(5) print(a)
25 None
Why None?
Because function explicit return value கொடுக்கவில்லை.
1Using return 2def square(x): 3 return x * x
1a = square(5)
1print(a)
25
25
1a = 25
None
return செய்யும்.
1def test(): 2 print("Hello")
1x = test()
1print(x)
Hello None
Hello None
return a + b
result = add(5, 10)
result = 15
Returned value variable-ல் store செய்யலாம்.
print(result * 2)
return a + b
= add(10, 20)
60
1result = 30 230 × 2 = 60
30
Variable compulsory இல்லை.
return a + b
print(add(10, 20))
30
Returned function value another calculation-க்குள் பயன்படுத்தலாம்.
return x * x
= square(5) + 10
print(result)
35
25 + 10 = 35
return execute ஆனதும் function execution immediately ends.
def test():
print("A")
return
print("B")test()
A
B execute ஆகாது.
Anything after return in that execution path will not run.
return 100
x = test()
x = 100
Python
return
None
1def test(): 2 return
1print(test())
None
None
Python function multiple values return செய்யலாம்.
return a + b, a - b
x, y = calculate(10, 5)
print(x) print(y)
15 5
return a + b, a - b
(15, 5)
Python அதை tuple ஆக return செய்கிறது.
print(result) print(type(result))
return a + b, a - b
= calculate(10, 5)
(15, 5)
<class 'tuple'>
x, y = calculate(10, 5)
x = 15 y = 5
This is tuple unpacking.
def check_number(x)
return "Positive"
return "Negative"
print(check_number(10))
Positive
Sometimes function-ஐ early stop செய்ய return பயன்படுத்தலாம்.
def check_age(age):
if age < 18:
return "Not Eligible"return "Eligible"
print(check_age(15))
Not Eligible
First return execute ஆனதும் function ends.
1def cube(number): 2 return number ** 3
number = 4
4³
64
1answer = cube(4)
1answer = 64
25
return "Pass"
return "Fail" status = result(75) print(status)
Exam point-ஆக commonly parameters / arguments குறித்து கேட்கப்படும் forms:
add(10, 20)
10 → a
20 → b
return a + b
Position decides mapping.
student(age=20, name="Ravi")
print(name, age)
Parameter name decides mapping.
print(power(5))
return x ** p
25
1p = 2
1print(power(5, 3))
125
125
Argument overrides default.
print(total(10, 20, 30))
return sum(numbers)
60
print(display(name="Ravi", age=20))
return details
{'name': 'Ravi', 'age': 20}
Parameters normally function-ன் local variables போல behave செய்கின்றன.
1def show(x): 2 print(x)
show(10)
x function execution-க்குள் value receive செய்கிறது.
Function outside:
1print(x)
என்றால் x separately defined இல்லையென்றால் error வரும்.
Python-ல் arguments pass செய்யும் behavior-ஐ simple-ஆக:
Function parameter object-ஐ reference செய்கிறது.
1def change(x): 2 x = 100
1a = 10 2change(a)
1print(a)
10
10
Function-க்குள் x = 100 என்று reassign செய்ததால் outside a change ஆகவில்லை.
இந்த concept later mutable/immutable objects படிக்கும்போது மிகவும் முக்கியம்.
Function caller மற்றும் function body-க்கு இடையிலான input connection
என்று புரிந்துகொள்ளுங்கள்.
return என்பது function body மற்றும் caller-க்கு இடையிலான output connection.
Caller
│
├── Argument
↓
Parameter
↓
Function
↓
Return
↓
Caller
| Parameter | Return Value |
|---|---|
| Function-க்கு input | Function-இலிருந்து output |
| Definition-ல் written | return மூலம் sent |
| Argument receives | Result gives back |
| Function start side | Function end/result side |
Tamil memory:
Argument கொடுக்கிறோம் → Parameter வாங்குகிறது → Function வேலை செய்கிறது → Return result திருப்பிக் கொடுக்கிறது.
Parameter and Argument same என்று நினைப்பது
Related concepts தான், ஆனால் exact same அல்ல.
a, b = parameters. add(10, 20) 10,20 = arguments.
print means return
Wrong understanding.
print(a + b)
This displays.
return a + b
This returns.
Code after return will execute
return 10
print("Hello")print("Hello") execute ஆகாது.
Too few arguments
return a + b
add(10)
b missing.
Too many arguments
return a + b
add(10, 20, 30)
Function only two required positional parameters expects.
Expecting return without storing or using it
return a + b
add(10, 20)
Function returns 30, but this script does not display it automatically.
print(add(10, 20))
x = add(10, 20) print(x)
1. Parameter appears in?
Function definition
1. Argument appears in?
Function call
1. Which keyword sends result back?
return
1. Function without explicit return gives?
None
1. Does return terminate current function execution?
Yes
1. Multiple values returned by Python are commonly packed as?
Tuple
1. Default parameter means?
A parameter already has a predefined value.
1. *args generally stores?
Tuple
1. **kwargs generally stores?
Dictionary
1. print() and return same?
No
What is a parameter?
A parameter is a variable in the function definition that receives an argument when the function is called.
What is an argument?
An argument is the actual value supplied to a function during a function call.
What does return do?
return sends a result back to the caller and terminates the current function execution.
What happens if a function has no return statement?
Python implicitly returns:
None
Can a Python function return multiple values?
Yes.
return a, b
They are normally packed into a tuple.
Can returned values be used in expressions?
Yes.
1result = square(5) + 10
A None
A None
Answer: A followed by None
Answer: B
Answer: C
Answer: B
Answer: C
def add(a, b):
return a + b
print(add(15, 20)) 35
Identify parameters and arguments:
def multiply(x, y):
return x * y
multiply(4, 5)Answer:
def get_name():
return "Python"
x = get_name()
print(x) Python
def calculate(a, b):
return a + b, a * b
x, y = calculate(5, 3)
print(x)
print(y) 8 15
Parameter → Function definition variable
Argument → Actual value during call
Return → Result sent back
Most important example
return a + b
result = add(10, 20)
a, b → Parameters
10, 20 → Arguments
30 → Return Value
result → Returned value stored here
Parameter என்பது function definition-ல் input value receive செய்யப்படும் variable. Argument என்பது function call செய்யும்போது கொடுக்கப்படும் actual value. return statement function உருவாக்கிய result-ஐ caller-க்கு திருப்பி அனுப்புகிறது. Function-ஐ ஒரு machine போல நினைத்தால்: Arguments are input, Parameters receive input, function processes it, and Return Value is the output.