Page 451 - CITS - Computer Software Application -TT
P. 451
COMPUTER SOFTWARE APPLICATION - CITS
Functions & arguments
In Python, functions are blocks of organized, reusable code that perform a specific task.. They allow you to break
down your program into smaller, modular pieces, making your code more organized, easier to read, and easier
to maintain. Functions can take arguments, which are inputs that the function operates on, and they can return a
value as a result of their operation.
Here’s a breakdown of functions and arguments in Python:
1 Defining a Function:
• You define a function in Python using the def keyword followed by the function name and parentheses (). If the
function takes arguments, you list them inside the parentheses. The function body is then indented below the
definition. Here’s a basic example:
2 Function Arguments:
• Functions can take arguments, which are values passed into the function when it is called. These arguments
provide input data to the function for it to operate on. Arguments are specified within the parentheses following
the function name. There are different types of arguments in Python:
- Positional Arguments: These are arguments that are matched based on their position in the function call.
The order of the arguments matters.
- Keyword Arguments: These are arguments preceded by a keyword when calling a function. The order of
keyword arguments does not matter.
- Default Arguments: These are arguments that have a default value specified in the function definition. If the
caller doesn’t provide a value for these arguments, the default value is used.
- Variable-Length Arguments: Functions can accept a variable number of arguments. This is achieved using
*args for positional arguments and **kwargs for keyword arguments.
• Here’s an example illustrating these different types of arguments:
• (name, g) # Output: H)) Outp
3 Returning Values:
• Functions can return a value using the return keyword. This value can be of any data type, including numbers,
strings, lists, dictionaries, etc. If a function doesn’t explicitly return a value, it implicitly returns None.
Functions and arguments are fundamental concepts in Python programming, enabling code reuse, modularity, and
abstraction. They allow you to write efficient and maintainable code by breaking it down into smaller, manageable
parts.
438
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137