Function • A functionis a relationship or mapping between one or more inputs and a set of outputs. In mathematics, a function is typically represented like this: • Here, f is a function that operates on the inputs x and y. The output of the function is z. However, programming functions are much more generalized and versatile than this mathematical definition. In fact, appropriate function definition and use is so critical to proper software development that virtually all modern programming languages support both built-in and user-defined functions. • In programming, a function is a self-contained block of code that encapsulates a specific task or related group of tasks.
3.
The Importance ofPython Functions Virtually all programming languages used today support a form of user-defined functions, although they aren’t always called functions. In other languages, you may see them referred to as one of the following: • Subroutines • Procedures • Methods • Subprograms
5.
Types of function •There are three types of functions in Python: • Built-in functions, such as help() to ask for help, min() to get the minimum value, print() to print an object to the terminal,… You can find an overview with more of these functions here. • User-Defined Functions (UDFs), which are functions that users create to help them out; And • Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword.
6.
Functions vs. methods •A method refers to a function which is part of a class. You access it with an instance or object of the class. A function doesn’t have this restriction: it just refers to a standalone function. This means that all methods are functions, but not all functions are methods.
7.
Parameters vs. arguments •Parameters are the names used when defining a function or a method, and into which arguments will be mapped. In other words, arguments are the things which are supplied to any function or method call, while the function or method code refers to the arguments by their parameter names.
8.
User-Defined Functions (UDFs) •The four steps to defining a function in Python are the following: • Use the keyword def to declare the function and follow this up with the function name. • Add parameters to the function: they should be within the parentheses of the function. End your line with a colon. • Add statements that the functions should execute. • End your function with a return statement if the function should output something. Without the return statement, your function will return an object None.
10.
Component Meaning def Thekeyword that informs Python that a function is being defined <function_name> A valid Python identifier that names the function <parameters> An optional, comma-separated list of parameters that may be passed to the function : Punctuation that denotes the end of the Python function header (the name and
11.
def hello(): name =str(input("Enter your name: ")) if name: print ("Hello " + str(name)) else: print("Hello World") return hello()
12.
Argument Passing Positional Arguments •The most straightforward way to pass arguments to a Python function is with positional arguments (also called required arguments). In the function definition, you specify a comma- separated list of parameters inside the parentheses: >>> def f(qty, item, price): ... print(f'{qty} {item} cost ${price:.2f}') ... >>> f(6, 'bananas', 1.74) 6 bananas cost $1.74
13.
• Keyword Arguments Ifyou want to make sure that you call all the parameters in the right order, you can use the keyword arguments in your function call. You use these to identify the arguments by their parameter name. >>> f(qty=6, item='bananas', price=1.74) 6 bananas cost $1.74 Using keyword arguments lifts the restriction on argument order. Each keyword argument explicitly designates a specific parameter by name, so you can specify them in any order and Python will still know which argument goes with which parameter:
14.
• Default Parameters •If a parameter specified in a Python function definition has the form <name>=<value>, then <value> becomes a default value for that parameter. Parameters defined this way are referred to as default or optional parameters. >>> def f(qty=6, item='bananas', price=1.74): ... print(f'{qty} {item} cost ${price:.2f}') ...
• Modular programmingrefers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable subtasks or modules. Individual modules can then be cobbled together like building blocks to create a larger application. • There are several advantages to modularizing code in a large application: Simplicity: Rather than focusing on the entire problem at hand, a module typically focuses on one relatively small portion of the problem. If you’re working on a single module, you’ll have a smaller problem domain to wrap your head around. This makes development easier and less error-prone.
19.
• Maintainability: Modulesare typically designed so that they enforce logical boundaries between different problem domains. If modules are written in a way that minimizes interdependency, there is decreased likelihood that modifications to a single module will have an impact on other parts of the program. (You may even be able to make changes to a module without having any knowledge of the application outside that module.) This makes it more viable for a team of many programmers to work collaboratively on a large application. • Reusability: Functionality defined in a single module can be easily reused (through an appropriately defined interface) by other parts of the application. This eliminates the need to duplicate code. • Scoping: Modules typically define a separate namespace, which helps avoid collisions between identifiers in different areas of a program.
20.
Python Modules: • Thereare three different ways to define a module in Python: • A module can be written in Python itself. • A module can be written in C and loaded dynamically at run- time, like the re (regular expression) module. • A built-in module is intrinsically contained in the interpreter, like the itertools module.
21.
The import Statement •Module contents are made available to the caller with the import statement. The import statement takes many different forms, shown below. import <module_name> The simplest form is the one already shown above: import <module_name>
22.
from <module_name> import <name(s)> •An alternate form of the import statement allows individual objects from the module to be imported directly into the caller’s symbol table: • from <module_name> import <name(s)>