0% found this document useful (0 votes)
2 views2 pages

Class 12 Python Functions Theory

The document provides an overview of functions in Class 12 Computer Science, detailing their definition, syntax, and advantages such as reducing repetition and improving readability. It covers built-in and user-defined functions, how to call them, and explains parameters, arguments, and variable-length arguments. Additionally, it distinguishes between local and global variables, highlighting their accessibility within functions.

Uploaded by

jishanraza811
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Class 12 Python Functions Theory

The document provides an overview of functions in Class 12 Computer Science, detailing their definition, syntax, and advantages such as reducing repetition and improving readability. It covers built-in and user-defined functions, how to call them, and explains parameters, arguments, and variable-length arguments. Additionally, it distinguishes between local and global variables, highlighting their accessibility within functions.

Uploaded by

jishanraza811
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class 12 Computer Science - Functions (Theory)

Introduction
A function is a reusable block of code that performs a specific task.
Syntax:
def function_name():
pass
Example:
def greet():
print('Hello')
Need & Advantages
Reduces repetition, improves readability and maintenance.
Syntax:
-
Example:
Reuse same function many times.
Built-in Functions
Functions already provided by Python.
Syntax:
print(x), len(s), input()
Example:
print(len('Hello'))
User-defined Functions
Functions created using def.
Syntax:
def name():
Example:
def add(a,b):
return a+b
Calling
Execute a function using its name.
Syntax:
name()
Example:
greet()
Parameters & Arguments
Parameters receive values; arguments are passed.
Syntax:
def f(x):
Example:
f(10)
Positional Arguments
Matched by position.
Syntax:
f(a,b)
Example:
f(2,3)
Default Arguments
Parameter has default value.
Syntax:
def f(x=5):
Example:
f(); f(10)
Keyword Arguments
Pass using parameter names.
Syntax:
f(a=1,b=2)
Example:
f(b=2,a=1)
Variable-length
Accept multiple arguments.
Syntax:
def f(*args, **kwargs):
Example:
f(1,2,3)
Return
Returns value to caller.
Syntax:
return value
Example:
return a+b
Without Return
Performs task but returns None.
Syntax:
def show():
Example:
print('Hi')
Local Variables
Accessible only inside function.
Syntax:
def f(): x=5
Example:
Global Variables
Declared outside; usable globally.
Syntax:
x=10
Example:
global x

You might also like