0% found this document useful (0 votes)
1 views3 pages

Class 12 Python Functions Notes

The document provides an overview of functions in Python, including definitions, syntax, and examples for various types such as built-in, user-defined, and those using parameters and arguments. It explains concepts like positional arguments, default arguments, and the use of *args and **kwargs. Additionally, it covers local and global variables, as well as the return statement and functions without return values.

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)
1 views3 pages

Class 12 Python Functions Notes

The document provides an overview of functions in Python, including definitions, syntax, and examples for various types such as built-in, user-defined, and those using parameters and arguments. It explains concepts like positional arguments, default arguments, and the use of *args and **kwargs. Additionally, it covers local and global variables, as well as the return statement and functions without return values.

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

Introduction to Functions

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


Syntax:
def function_name():
pass
Example Question: Define a function that prints 'Hello World'.

Need and Advantages


Definition: Functions reduce code duplication, improve readability, debugging and maintenance.
Syntax:
-
Example Question: Write two advantages of functions.

Built-in Functions
Definition: Predefined functions provided by Python.
Syntax:
print(x)
len(s)
Example Question: Use len() to find length of 'Python'.

User-defined Functions
Definition: Functions created by the programmer.
Syntax:
def greet():
print('Hi')
Example Question: Create a function to display your name.

Defining a Function (def)


Definition: Use def keyword to define a function.
Syntax:
def add(a,b):
return a+b
Example Question: Define a function to add two numbers.

Calling a Function
Definition: Execute a function using its name.
Syntax:
add(2,3)
Example Question: Call a function twice.

Parameters and Arguments


Definition: Parameters are variables in definition; arguments are values passed.
Syntax:
def f(x): ...; f(5)
Example Question: Differentiate parameter and argument.

Positional Arguments
Definition: Matched by order.
Syntax:
add(2,3)
Example Question: Write a function using positional arguments.

Default Arguments
Definition: Parameters with default values.
Syntax:
def show(c='India'):
Example Question: Create a function with a default argument.

Keyword Arguments
Definition: Arguments passed by parameter name.
Syntax:
show(name='Ram')
Example Question: Call a function using keyword arguments.

*args
Definition: Accepts any number of positional arguments.
Syntax:
def f(*args):
Example Question: Write a function using *args.

**kwargs
Definition: Accepts any number of keyword arguments.
Syntax:
def f(**kwargs):
Example Question: Write a function using **kwargs.

Return Statement
Definition: Returns a value to the caller.
Syntax:
return value
Example Question: Write a function that returns the square of a number.

Functions without return


Definition: Perform work but return None implicitly.
Syntax:
def hello(): print('Hi')
Example Question: Write a function without return.

Local Variables
Definition: Declared inside a function; accessible only there.
Syntax:
def f(): x=10
Example Question: What is a local variable?

Global Variables
Definition: Declared outside functions; accessible globally.
Syntax:
x=10
Example Question: Explain the global keyword with an example.

You might also like