0 ratings 0% found this document useful (0 votes) 14 views 2 pages Functions Notes
The document provides an overview of functions in Python, explaining their purpose, types, and syntax. It categorizes functions into built-in, module-defined, and user-defined functions, detailing how to define and call them. Additionally, it discusses argument types, function scopes, and the lifetime of variables.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save functions notes For Later { FUNCTIONS
© Quick Revision Notes ,
1. A funetion is a way to modularise program and it also lends reusability to code.
2. Afunction isa named set of code or instructions that are executed only when the function is called orinvoked.
3. Functions can be categorized into the following types:
‘* Built-in Function: these are predefine functions which can be used in any Python program.
For example, the functions that work on strings, dictionary, list, tuple, ete.
‘+ Functions defined in modules: Python has a large number of modules which contain functions that can
be used by importing the module in a program, Some of the modules of the Python are math, statistics,
random, ete.
* User defined functions(UDFs): These functions are defined by the programmer according to the need.
They are custom programs. All UDFs start witha def keyword. Syntax of Function definition is as follows:
def function-name(argument list):
statement (s)
return [expression]
Here,
+ Function name can be any valid identifier
+ Argument list is optional. tis simply alist of variables separated by commas.
+ The return statement is also optional It is used to return the value of a variable,
4,
Syntax of function call is as follows:
function-name (parameters)
Here, the parameter is optional, These are variables whose values are passed to a function, For example:
def show(a, b) :———»Function Header
print (a+b) +e Function Body
show (4,7) Funetion Call
5. The values being passed through a function call statement are called argument (or actual parameter or actual
argument),
6. The values received in the function definition /header are called parameter (or formal parameter of formal
argument),
7. Functions can modify only the mutable data types. Any change done to anon-mutable type or simple variables
ina function is not reflected back in the program.
Function (47 58. A function header cannot have expressions. It can have just names or identifiers. Python supports three
types of arguments/parameters:
Positional Argument: These arguments need to be passe
one to the parameters in the function header
the correct order: They are matched one on
Default Arguments: A parameter having default value in the function header is known as a default
parameter. When no value is provided for a parameter it takes the default value provided.
Keyword (or named) Arguments: These arguments are named and assigned values. They ean be placed
in any order in the function call.
Keyword
T
def add [ace add (a,b):
print (atb) | print (a+b) |_-quprint (a+b)
add (7,9) lero, 6 Jadata-1, b-9)
add(s, p) foutput 15] #output 13
add(s) 4 output 17
Jada() toutpatis |
| |
- 1
. Rules for combining all three types of statements
10.
‘An argument list must first contain positional (required) arguments followed by any keyword argument.
Ifa function contains default arguments, then the arguments to the right must be default also.
We cannot specify a vahie for an argnment mare than ance,
Functions can be of four types:
Functions with no arguments and no return type
Functions with arguments and no return type
Functions with no arguments but with return type
Functions with arguments and return type
Noargument,no | Noreturnstatement | Noarguments | Arguments and
return statement | i | _return statement
det demo(): [def demo(a,p): [def demo()+ [det demo(a, b)t
print ("hello") / print (a+b) aso return atb
deno() demo (x, y) return a demo ()
| LeedemoQ)
11
4B)
Part(s) of program within which a name is accessible, is called scope of the variable (name). There are two
kinds of scopes in Python:
Global Scope: A variable declared outside all the functions is called Global variable and it has global Scope.
Local Scope: A variable declared within a functionis called local variable and it has local scope.
@ Lifetime: The time for which a variable or name remains in memory is called lifetime of variable.
‘Touchpad Question Bank (CS 083)XI