TECHQueenUnacademy
Computer Science
Notes: Working With Function Grade: 12th
Functions:
A function is a block of code that is used to perform a specific task.
Functions can be used to modularize code, making it easier to read,
understand, and maintain.
Types of Functions:
There are three types of functions in Python:
● Built-in functions: These are functions that are provided by the
Python language. Some examples of built-in functions are print(),
len(), and sum().
● Functions defined in modules: These are functions that are defined
in a module. Modules are files that contain Python code. To use a
function that is defined in a module, you must first import the module.
● User-defined functions: These are functions that are defined by the
programmer. User-defined functions can be used to perform any task
that can be performed by a built-in function or a function defined in a
module.
Creating User-Defined Functions:
To create a user-defined function, you use the def keyword. The syntax for
creating a user-defined function is as follows:
Code snippet
def function_name(parameters):
# body of the function
● def keyword: This keyword is used to define a function.
Lovejeet Arora
TECHQueenUnacademy
● function_name: This is the name of the function.
● parameters: These are the arguments that are passed to the
function.
● body of the function: This is the code that is executed when
the function is called.
Arguments and Parameters:
Arguments are the values that are passed to a function when it is called.
Parameters are the variables that are used to receive the arguments that
are passed to a function.
Default Parameters:
Default parameters are values that are assigned to parameters if no
arguments are passed to the function. The syntax for defining a default
parameter is as follows:
Code snippet
def function_name(parameter1, parameter2=value):
# body of the function
In this example, the parameter parameter2 has a default value of value.
If no argument is passed to parameter2 when the function is called, then
the value of parameter2 will be value.
Positional Parameters:
Positional parameters are parameters that are passed to a function in the
order that they are defined. The syntax for passing positional parameters is
as follows:
Code snippet
function_name(argument1, argument2)
Lovejeet Arora
TECHQueenUnacademy
In this example, argument1 is passed to the parameter parameter1 and
argument2 is passed to the parameter parameter2.
Function Returning Value(s):
Functions can return values. The value that is returned by a function is the
value that is assigned to the variable that is used to call the function. The
syntax for returning a value from a function is as follows:
Code snippet
def function_name(parameters):
# body of the function
return value
In this example, the value of value is returned from the function.
Flow of Execution:
The flow of execution in a function is controlled by the statements that are
used in the function. The statements in a function are executed in the order
that they are written.
Scope of a Variable:
The scope of a variable is the part of the program where the variable can
be used. There are two types of scopes in Python: global scope and local
scope.
● Global scope: The global scope is the scope of all variables that are
defined outside of any function.
● Local scope: The local scope is the scope of all variables that are
defined inside a function.
Variables that are defined in the global scope can be used in any scope.
Variables that are defined in a local scope can only be used in the local
scope and in any nested scopes. ****
Lovejeet Arora