Functions in Python - Summary Notes
1. Functions in Python
• A function is a collection of statements that perform a specific task.
• Functions help break a large program into smaller, manageable parts.
• They improve code reusability and readability.
• Functions can be reused in other programs as library functions.
• Python has three types of functions: Built-in, Modules, and User-defined.
2. Built-in Functions
• Predefined functions available in Python’s standard library.
• They make programming easier and more efficient.
• Common built-in functions include:
• 1. int(), float(), str() – Type conversion functions.
• 2. input() – Takes input from the user.
• 3. eval() – Evaluates an expression in string format.
• 4. min(), max(), abs() – Mathematical and comparison functions.
• 5. type(), len(), round(), range() – Type checking, length, rounding, and sequence
functions.
3. Python Modules
• A module is a Python file (.py) that contains function and variable definitions.
• Modules allow dividing a program into separate files for better organization.
• Once created, modules can be imported and reused in other programs.
• Two ways to import modules: using 'import' and 'from...import' statements.
4. Common Python Modules
• math module – Provides mathematical functions like sqrt(), pow(), ceil(), floor(), sin(),
cos(), etc.
• string module – Provides string manipulation functions such as upper(), lower(), find(),
join(), isalpha(), etc.
• random module – Used to generate random numbers using functions like random(),
randint(), randrange(), choice(), and shuffle().
5. User-Defined Functions
• Functions created by the user for specific tasks.
• Defined using the 'def' keyword followed by function name and parameters.
• Syntax: def function_name(parameters):
• They can be created with or without arguments and with or without return values.
• Python also allows multiple return values using tuples.
6. Parameters and Arguments
• Parameters are variables listed inside the parentheses in the function definition.
• Arguments are values passed during the function call.
• Python supports four types of arguments:
• 1. Positional Arguments – Order matters.
• 2. Default Arguments – Have predefined values if not passed.
• 3. Keyword Arguments – Passed using parameter names.
• 4. Variable-length Arguments – Use *args to pass multiple arguments.
7. Scope of Variables
• Scope defines where a variable can be accessed in the program.
• Two types of scope:
• 1. Global Scope – Variables declared outside all functions.
• 2. Local Scope – Variables declared inside a function.
8. Recursion
• Recursion occurs when a function calls itself.
• It requires a base condition to stop the recursive calls.
• Each recursive call creates new memory for local variables.
• Advantages: reduces code length and improves readability.
• Drawbacks: can use more memory and be slower if not handled properly.
9. Flow of Execution in Functions
• Program execution starts from the top line.
• When a function is called, control passes to that function’s body.
• After executing the function, control returns to the calling point.
• Functions make code modular and manageable.