Python Function Types TRB CSI › Python Programming › Function types

Python Function Types

A function is a reusable block of code that performs a specific task. தமிழில்: ஒரு குறிப்பிட்ட வேலை செய்யும் statements-ஐ ஒரு பெயருக்குள் group செய்து, அதை தேவையான போது call செய்து reuse செய்வது Function.

Free Intermediate 18 min 158 cards 33 programs 10 MCQs v3

Python-ல் Function என்றால் ஒரு specific work செய்ய எழுதப்பட்ட reusable code block.

ஒரு function-ஐ ஒருமுறை define செய்து, தேவையான அளவு பல முறை call செய்யலாம்.

Simple idea:

Function = ஒரு வேலைக்கான தனி code block

Program
Example
Python 2 lines
1def greet():
2    print("Hello")
Explanation
Core idea

இங்கே greet() என்பது ஒரு function.

Definition
Meaning

A function is a reusable block of code that performs a specific task.

ஒரு குறிப்பிட்ட வேலை செய்யும் statements-ஐ ஒரு பெயருக்குள் group செய்து, அதை தேவையான போது call செய்து reuse செய்வது Function.

Explanation
Why function is needed
Core idea

Same code மீண்டும் மீண்டும் எழுத வேண்டும்.

Function இல்லாமல்
print("Welcome")
print("Welcome")
print("Welcome")
Function பயன்படுத்தினால்
def welcome():
    print("Welcome")
Detail 03
welcome()
welcome()
welcome()
இதனால்
  • Code repetition குறையும்
  • Program clean ஆகும்
  • Error fixing easy
  • Reusability கிடைக்கும்
  • Program modules-ஆக divide செய்யலாம்
  • பெரிய program easy to understand
Concept
Main concept
Context

Function-ஐ ஒரு machine போல நினைத்துக்கொள்ளுங்கள்.

  1. Function
  2. Processing
Explanation
Example
Core idea
  1. 2, 3
  2. add()
  3. 2 + 3
  4. 5
Program
Python 2 lines
1def add(a, b):
2    return a + b
Program
Python 1 lines
1print(add(2, 3))
Verified output
5
Learning Run Mode — this is the output the author verified, not a live execution.
Output
5
Explanation
Function types
Core idea

Python function types-ஐ இரண்டு different ways-ல் classify செய்யலாம்.

Detail 01

Classification 1 - Function உருவாக்கப்பட்ட விதம்

Explanation
Core idea
  • 1. Built-in Function
  • 2. User-defined Function
  • 3. Anonymous / Lambda Function
  • 4. Recursive Function
Explanation
Core idea

Classification 2 - Arguments மற்றும் Return Value அடிப்படையில்

Explanation
Core idea
  • 1. No Argument, No Return Value
  • 2. Argument, No Return Value
  • 3. No Argument, Return Value
  • 4. Argument, Return Value
Explanation
Core idea

இந்த இரு classifications-ஐ confuse செய்யக்கூடாது.

Definition
Meaning

Python already provide செய்திருக்கும் ready-made functions தான் Built-in Functions.

நாமே define செய்ய வேண்டியதில்லை.

print()
input()
len()
type()
int()
float()
str()
sum()
max()
min()
range()
Program
Example
Python 1 lines
1print("Python")
Explanation
Core idea

இங்கே print() நாமே உருவாக்கவில்லை.

Detail 01

Python already அதை provide செய்துள்ளது.

Detail 02

அதனால்:

Program
Python 1 lines
1print() = Built-in Function
Explanation
Core idea

name = "Python"

Program
Python 1 lines
1print(len(name))
Verified output
6
Learning Run Mode — this is the output the author verified, not a live execution.
Output
6
Program
இங்கே
Python 1 lines
1len()

ஒரு built-in function.

Important
  1. Built-in function-க்கு def எழுத தேவையில்லை.

  2. Wrong idea

    Built-in function = user உருவாக்குவது

  3. Correct

    Built-in function = Python already provide செய்வது

Definition
Meaning

Programmer தனது requirement-க்கு ஏற்ப உருவாக்கும் function தான் User-defined Function.

def

keyword பயன்படுத்தப்படுகிறது.

Syntax
Python 2 lines
1def function_name():
2    statements
Program
Example
Python 2 lines
1def greet():
2    print("Welcome")
Explanation
Core idea

Function define செய்துவிட்டோம்.

Detail 01

ஆனால் இது உடனே execute ஆகாது.

Call செய்ய வேண்டும்

greet()

Program
Python 2 lines
1def greet():
2    print("Welcome to Python")
Verified output
Welcome to Python
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

greet()

Output
Welcome to Python
Concept
Very important concept

print("Welcome")

இது function-ஐ create செய்கிறது.

greet()

இது function-ஐ execute செய்கிறது.

def = Define
()  = Call
Flow
  1. Program starts
  2. Function definition stored
  3. greet() call
  4. Control goes inside function
  5. Statements execute
  6. Control returns back
Definition
Meaning

ஒரு function-க்கு normal பெயர் இல்லாமல், short expression-ஆக எழுதப்படும் function தான் Lambda Function.

Anonymous Function

என்றும் சொல்வார்கள்.

Explanation
lambda arguments

expression

Program
Example
Python 1 lines
1square = lambda x: x * x
Program
Python 1 lines
1print(square(5))
Verified output
25
Learning Run Mode — this is the output the author verified, not a live execution.
Output
25
Comparison
Normal function vs lambda

return x * x

square = lambda x: x * x
இரண்டின் result same.

Concept
  • Small function
  • One expression
  • Temporary function
  • map()
  • filter()
  • sorted()
  • போன்ற இடங்களில் useful.
Important
  1. Lambda-வில் பொதுவாக

    return

  2. எழுத வேண்டியதில்லை.

  3. Expression result automatically return ஆகும்.

Explanation
Example
Core idea

இதன் result automatic return.

lambda x

x + 10

Definition
Meaning

ஒரு function தன்னைத் தானே call செய்தால் அது Recursive Function.

  • Function calls itself
  • =
  • Recursion
Program
Python 3 lines
1def count(n):
2    if n == 0:
3        return
Program
Python 2 lines
1print(n)
2    count(n - 1)
Verified output
5
4
3
2
1
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

count(5)

Output
5
4
3
2
1
Explanation
How recursion works
First

count(5)

Inside

print 5

call count(4)

Then

print 4

call count(3)

Then

print 3

call count(2)

Finally

count(0)

Base condition

if n == 0

return

Function stops.

Important
Very important
  1. Recursive function-ல் Base Case மிகவும் important.

  2. Base case இல்லையென்றால் function தொடர்ந்து தன்னை call செய்து

    RecursionError

  3. வரலாம்.

Memory Trick

Recursive Function
=
Self Call + Base Case

Explanation
Function types based on arguments and return
Core idea

இப்போது exam-ல் மிகவும் முக்கியமான classification பார்க்கலாம்.

ஒரு function

input வாங்குகிறதா?
output return செய்கிறதா?

Detail 02

இதைத்தான் வைத்து 4 types.

Explanation
Type 1
Core idea

No Arguments and No Return Value

இதில் function

Input வாங்காது
Output return செய்யாது

Syntax
Python 2 lines
1def function_name():
2    statements
Program
Example
Python 2 lines
1def greet():
2    print("Hello")
Verified output
Hello
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

greet()

Output
Hello
Concept

Function-க்கு data outside-லிருந்து கொடுக்கவில்லை.

greet()

brackets empty.

Return value-உம் இல்லை.

அதனால்:

No Argument
No Return

Real Life Example
Real time example
print("Bell Ringing")

ring_bell()

Bell function-க்கு input தேவையில்லை.

Just action செய்கிறது.

Explanation
Type 2
Core idea

Arguments and No Return Value

Detail 01

Function input வாங்கும்.

Detail 02

ஆனால் result-ஐ return மூலம் caller-க்கு அனுப்பாது.

Program
Python 2 lines
1def add(a, b):
2    print(a + b)
Verified output
30
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

add(10, 20)

Output
30
Concept

a, b

are parameters.

add(10, 20)

10, 20

are arguments.

Function result print செய்கிறது.

ஆனால் return செய்யவில்லை.

Important
Very important
  1. print()

  2. மற்றும்

    return

  3. same இல்லை.

  4. print()

  5. Screen-ல் காட்டும்.

  6. return

  7. Function result-ஐ caller-க்கு back அனுப்பும்.

Real Life Example
Example
print(a + b)

x = add(10, 20)

print(x)
Output
30
None

ஏன் None?

Because function return செய்யவில்லை.

Explanation
Type 3
Core idea

No Arguments but Return Value

Detail 01

Function input caller-லிருந்து வாங்காது.

Detail 02

ஆனால் result return செய்யும்.

Program
Python 2 lines
1def get_number():
2    return 100
Program
Python 1 lines
1x = get_number()
Program
Python 1 lines
1print(x)
Verified output
100
Learning Run Mode — this is the output the author verified, not a live execution.
Output
100
Concept

get_number()

arguments இல்லை.

return 100

மூலம் value back அனுப்புகிறது.

Flow
  1. get_number()
  2. Function execute
  3. return 100
  4. 100 comes back
  5. x = 100
Explanation
Type 4
Core idea

Arguments and Return Value

Detail 01

இது most useful function type.

Function

Input வாங்கும்
+
Output return செய்யும்

Program
Python 2 lines
1def add(a, b):
2    return a + b
Program
Python 1 lines
1result = add(10, 20)
Program
Python 1 lines
1print(result)
Verified output
30
Learning Run Mode — this is the output the author verified, not a live execution.
Output
30
Flow
  1. 10, 20
  2. add(a, b)
  3. a + b
  4. return 30
  5. result = 30
Explanation
Why this type is important
Arguments + Return Value function
  • reusable
  • flexible
  • testing easy
  • result வேறு calculation-க்கு use செய்யலாம்
Program
Example
Python 2 lines
1def add(a, b):
2    return a + b
Program
Python 1 lines
1x = add(10, 20)
Program
Python 1 lines
1print(x * 2)
Verified output
60
Learning Run Mode — this is the output the author verified, not a live execution.
Output
60

Because returned value can be reused.

Comparison
Four types comparison
TypeArgumentReturn
No Argument, No ReturnNoNo
Argument, No ReturnYesNo
No Argument, ReturnNoYes
Argument, ReturnYesYes
Memory Trick
Input = Argument
Output = Return
  • No Input + No Output
  • Input + No Output
  • No Input + Output
  • Input + Output
Comparison
Parameter vs argument
Context

இந்த concept மிகவும் முக்கியம்.

a, b = Parameters

add(10, 20)

10, 20 = Arguments
Easy Memory
Parameter = Function definition
Argument  = Function call
Concept
Return concept

return ஒரு function-ன் result-ஐ caller-க்கு அனுப்பும்.

Program
Example
Python 2 lines
1def square(x):
2    return x * x
Program
Call
Python 1 lines
1a = square(5)
Program
Equivalent idea
Python 1 lines
1a = 25
Important

return execute ஆனதும் function immediately end ஆகும்.

Program
Example
Python 4 lines
1def test():
2    print("A")
3    return
4    print("B")
Verified output
A
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

test()

Output
A

B execute ஆகாது.

Explanation
Return multiple values
Core idea

Python function multiple values-ஐ return செய்யலாம்.

Program
Example
Python 2 lines
1def calculate(a, b):
2    return a + b, a - b
Explanation
Core idea

x, y = calculate(10, 5)

Program
Python 2 lines
1print(x)
2print(y)
Verified output
15
5
Learning Run Mode — this is the output the author verified, not a live execution.
Output
15
5

Technically Python multiple values-ஐ tuple form-ல் return செய்கிறது.

Explanation
Default argument function
Core idea

Function arguments-க்கு default value கொடுக்கலாம்.

def greet(name="Student")

print("Hello", name)

Detail 02
greet()
greet("Ravi")
Output
Hello Student
Hello Ravi
Concept

default value use ஆகும்

provided value use ஆகும்

Explanation
Keyword argument
Core idea

Argument-ஐ parameter name மூலம் pass செய்யலாம்.

def student(name, age)

print(name, age)

Detail 02

student(age=20, name="Ravi")

Output
Ravi 20

இதனை:

Keyword Argument

என்று சொல்வோம்.

Explanation
Positional argument
Normal order-ல் arguments pass செய்வது

student("Ravi", 20)

Here

Ravi → name

20 → age

இது

Positional Argument

Explanation
Variable length argument
Core idea

use செய்யலாம்.

எத்தனை arguments வரும் என்று தெரியவில்லை என்றால்

*args

Program
Example
Python 2 lines
1def add(*numbers):
2    print(numbers)
Verified output
(10, 20, 30)
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

add(10, 20, 30)

Output
(10, 20, 30)

*args usually tuple collect செய்கிறது.

Explanation
Keyword variable argument
Keyword arguments பலவற்றை collect செய்ய

**kwargs

Program
Example
Python 2 lines
1def student(**data):
2    print(data)
Verified output
{'name': 'Ravi', 'age': 20}
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

student(name="Ravi", age=20)

Output
{'name': 'Ravi', 'age': 20}

**kwargs dictionary form-ல் collect செய்கிறது.

Explanation
Function classification complete
Python functions conceptually
Functions
│
├── Built-in
│
├── User-defined
│
├── Lambda
│
└── Recursive
User-defined functions-ஐ arguments/return அடிப்படையில்
User-defined Function
│
├── No Argument + No Return
├── Argument + No Return
├── No Argument + Return
└── Argument + Return
Arguments-ன் forms
Arguments
│
├── Positional
├── Keyword
├── Default
├── *args
└── **kwargs
Comparison
Built-in vs User-defined
Built-inUser-defined
Python already providesProgrammer creates
print()def add():
len()def greet():
Ready to useNeed to define
Comparison
Normal Function vs Lambda
Normal FunctionLambda Function
Uses defUses lambda
Has normal function nameOften anonymous
Multiple statements possibleSingle expression
return can be writtenExpression result auto returned
Comparison
Print vs Return
printreturn
Displays resultSends result back
Screen outputFunction output
Cannot directly reuse printed valueReturned value can be reused
Program
Example
Python 2 lines
1def a():
2    print(10)
Verified output
25
Learning Run Mode — this is the output the author verified, not a live execution.
Comparison
vs

return 10

x = b()
store செய்யலாம்.
Common Mistake
Error 1
Context

Define but not call

print("Hello")
No output.
Why?
Function define மட்டும் செய்தோம்.

Call செய்யவில்லை.

greet()

Common Mistake
Error 2
Context

Wrong indentation

print("Hello")

print("Hello")

Common Mistake
Error 3
Context

Missing colon

def greet()

Common Mistake
Error 4
Context

Confusing argument and parameter

a, b = parameters.
add(10, 20)
10, 20 = arguments.
Common Mistake
Error 5
Context

Thinking print is return

print(a + b)

This prints result.

It does not return it.

Memory Trick

D = Define

C = Call
add(10, 20)
R = Return
return a + b
Function = DefineCallReturn
TRB Point
  1. Function define செய்ய keyword?

  2. def

  3. Python ready-made functions?

  4. Built-in functions

  5. Programmer creates?

  6. User-defined function

  7. Anonymous function?

  8. Lambda function

  9. Function calling itself?

  10. Recursive function

  11. Function definition values?

  12. Parameters

  13. Function call values?

  14. Arguments

  15. Result caller-க்கு அனுப்ப?

  16. return

  17. *args collect as?

  18. Tuple

  19. **kwargs collect as?

  20. Dictionary

Interview Point
Context

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.

Explanation
Example
Core idea

return a, b

Detail 01

They are effectively returned as a tuple.

MCQ
138Which keyword defines a function?
Choose one answer
MCQ
139print() is:
Choose one answer
MCQ
140Function created by programmer is:
Choose one answer
MCQ
141Anonymous function uses:
Choose one answer
MCQ
142Function calling itself is:
Choose one answer
MCQ
143What does return do?
Choose one answer
MCQ
144In: def add(a, b): a and b are:
Choose one answer
MCQ
145In: add(10, 20) 10 and 20 are:
Choose one answer
MCQ
146*args generally collects:
Choose one answer
MCQ
147**kwargs generally collects:
Choose one answer
Practice
  1. No Argument, No Return
Practice
def hello():
    print("Hello")

hello()
Practice
  1. Argument, No Return
Practice
def show(name):
    print(name)

show("Ravi")
Practice
  1. No Argument, Return
Practice
def get_value():
    return 50

x = get_value()
print(x)
Practice
  1. Argument and Return
Practice
def square(x):
    return x * x

print(square(5))
Output
25
Summary
  1. Function என்பது reusable codeblock.
  2. Main types
  3. Built-in
  4. User-defined
  5. Lambda
  6. Recursive
  7. Arguments + Return அடிப்படையில்
  8. No Argument + No Return
  9. Argument + No Return
  10. No Argument + Return
  11. Argument + Return
  12. Most important concepts
  13. def → Function definedefine
  14. call → Function executeecute
  15. parameter → Definition-ல்
  16. argument → Call-ல்
  17. return → Result backack
  18. lambda → Short anonymous functionion
  19. recursion → Function calls itself
Explanation
Short description
Core idea

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.

Text size
17px
Theme
Contents
Print / PDF
Program