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

Class 12 Python Functions Notes

This document provides an overview of functions in Python, including definitions, syntax, and examples for built-in and user-defined functions. It covers key concepts such as parameters, arguments, positional and keyword arguments, as well as local and global variables. The document emphasizes the importance of functions in reducing code repetition and enhancing readability.

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 views5 pages

Class 12 Python Functions Notes

This document provides an overview of functions in Python, including definitions, syntax, and examples for built-in and user-defined functions. It covers key concepts such as parameters, arguments, positional and keyword arguments, as well as local and global variables. The document emphasizes the importance of functions in reducing code repetition and enhancing readability.

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

Functions - Class 12 Computer Science (Python)

Introduction to Functions
Definition: A function is a reusable block of code that performs a specific task.
Syntax:
def function_name():
pass
Example Question: Write a function to print Hello.
Solution:
def hello():
print('Hello')
hello()
Output:
Hello

Need and Advantages


Definition: Functions reduce repetition and improve readability.
Syntax:
-
Example Question: Add numbers repeatedly using one function.
Solution:
def add(a,b):
print(a+b)
add(2,3)
Output:
5

Built-in Functions
Definition: Predefined Python functions.
Syntax:
len(x), print(x)
Example Question: Find length of 'Python'.
Solution:
print(len('Python'))
Output:
6

User-defined Functions
Definition: Functions created by the programmer.
Syntax:
def name():
Example Question: Create greet().
Solution:
def greet():
print('Good Morning')
greet()
Output:
Good Morning

Defining a Function (def)


Definition: Use def keyword.
Syntax:
def square(n):
Example Question: Square of 4.
Solution:
def square(n):
print(n*n)
square(4)
Output:
16

Calling a Function
Definition: Execute using its name.
Syntax:
function()
Example Question: Call hello().
Solution:
def hello():
print('Hi')
hello()
Output:
Hi

Parameters and Arguments


Definition: Parameters are variables; arguments are passed values.
Syntax:
def f(x):
Example Question: Display a name.
Solution:
def show(name):
print(name)
show('Jisoo')
Output:
Jisoo

Positional Arguments
Definition: Matched by order.
Syntax:
f(a,b)
Example Question: Add 5 and 7.
Solution:
def add(a,b): print(a+b)
add(5,7)
Output:
12

Default Arguments
Definition: Uses default if omitted.
Syntax:
def f(x=1):
Example Question: Country default.
Solution:
def c(n='India'): print(n)
c()
Output:
India

Keyword Arguments
Definition: Pass by parameter name.
Syntax:
f(a=1)
Example Question: Student.
Solution:
def s(name,age): print(name,age)
s(age=17,name='Jisoo')
Output:
Jisoo 17

*args
Definition: Multiple positional args.
Syntax:
def f(*a):
Example Question: Sum.
Solution:
def t(*n): print(sum(n))
t(1,2,3)
Output:
6

**kwargs
Definition: Multiple keyword args.
Syntax:
def f(**k):
Example Question: Print dict.
Solution:
def d(**x): print(x)
d(a=1,b=2)
Output:
{'a': 1, 'b': 2}

return
Definition: Returns value.
Syntax:
return x
Example Question: Return square.
Solution:
def sq(n): return n*n
print(sq(5))
Output:
25

Functions without return


Definition: No explicit return; returns None.
Syntax:
-
Example Question: Observe None.
Solution:
def g(): print('Hi')
x=g();print(x)
Output:
Hi
None

Local Variables
Definition: Accessible only inside function.
Syntax:
x=1 inside fn
Example Question: Local x.
Solution:
def f():
x=10;print(x)
f()
Output:
10

Global Variables
Definition: Declared outside function.
Syntax:
global x
Example Question: Modify global.
Solution:
x=0
def u():
global x;x+=1
u();print(x)
Output:
1

You might also like