Module 4.1. - Functions

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

There are at least four basic types of functions in Python:

- built-in functions which are an integral part of Python (such as the print() function). You can see a complete list of Python built-in functions at https://docs.python.org/3/library/functions.html. the ones that come from - pre-installed modules (you'll learn about them in the Python Essentials 2 course) - user-defined functions which are written by users for users - you can write your own functions and use them freely in your code, the lambda functions (you'll learn about them in the Python Essentials 2 course.)

There are two, very important, catches. Here's the first of them:

1. You mustn't invoke a function which is not known at the moment of invocation. Remember - Python reads your code from top to bottom. It's not going to look ahead in order to find a function you forgot to put in the right place ("right" means "before invocation".) 2. The second catch sounds a little simpler: You mustn't have a function and a variable of the same name. Assigning a value to the name message causes Python to forget its previous role. The function named message becomes unavailable. Fortunately, you're free to mix your code with functions - you're not obliged to put all your functions at the top of your source file.

Decomposition

A good and attentive developer divides the code (or more accurately: the problem) into well-isolated pieces, and encodes each of them in the form of a function. This considerably simplifies the work of the program, because each piece of code can be encoded separately, and tested separately. The process described here is often We can now state the second condition: if a piece of code becomes so large that reading and understating it may cause a problem, consider dividing it into separate, smaller problems, and implement each of them in the form of a separate function. This leads us directly to the third condition: if you're going to divide the work among multiple programmers, decompose the problem to allow the product to be implemented as a set of separately written functions packed together in different modules.

Python's preinstalled modules

Where do the functions come from? - a lot of functions, very useful ones, but used significantly less often than built-in ones, are available in a number of modules installed together with Python; the use of these functions requires some additional steps from the programmer in order to make them fully accessible (we'll tell you about this in a while);

Your first function How do you make such a function? You need to define it. The word define is significant here. This is what the simplest function definition looks like: def function_name(): function_body It always starts with the keyword def (for define) next after def goes the name of the function (the rules for naming functions are exactly the same as for naming variables) after the function name, there's a place for a pair of parentheses (they contain nothing here, but that will change soon) the line has to be ended with a colon; the line directly after def begins the function body - a couple (at least one) of necessarily nested instructions, which will be executed every time the function is invoked; note: the function ends where the nesting ends, so you have to be careful. We're ready to define our prompting function. We'll name it message - here it is: def message(): print("Enter a value: ") The function is extremely simple, but fully usable. We've named it message, but you can label it according to your taste. Let's use it.

def message(): print("Enter a value: ") print("We start here.") message() print("We end here.") output: We start here. Enter a value: We end here. -------------------------------------------------- The function is extremely simple, but fully usable. We've named it message, but you can label it according to your taste. Let's use it. Our code contains the function definition now: def message(): print("Enter a value: ") print("We start here.") print("We end here.") Note: we don't use the function at all - there's no invocation of it inside the code. When you run it, you see the following output: We start here. We end here. This means that Python reads the function's definitions and remembers them, but won't launch any of them without your permission. We've modified the code now - we've inserted the function's invocation between the start and end messages: def message(): print("Enter a value: ") print("We start here.") message() print("We end here.") The output looks different now: We start here. Enter a value: We end here.

You can define your own function using the def keyword and the following syntax:

def your_function(optional parameters): # the body of the function ----------------------------------------------- You can define a function which doesn't take any arguments, e.g.: def message(): # defining a function print("Hello") # body of the function message() # calling the function ----------------------------------------------- You can define a function which takes arguments, too, just like the one-parameter function below: def hello(name): # defining a function print("Hello,", name) # body of the function name = input("Enter your name: ") hello(name) # calling the function

Built-In Functions

from Python itself - numerous functions (like print()) are an integral part of Python, and are always available without any additional effort on behalf of the programmer; we call these functions

a function

is a block of code that performs a specific task when the function is called (invoked). You can use functions to make your code reusable, better organized, and more readable. Functions can have parameters and return values.

Your first function Take a look at the snippet in the editor. It's rather simple, but we only want it to be an example of transforming a repeating part of a code into a function. The messages sent to the console by the print() function are always the same. Of course, there's nothing really bad in such a code, but try to imagine what you would have to do if your boss asked you to change the message to make it more polite, e.g., to start it with the phrase "Please,". It seems that you'd have to spend some time changing all the occurrences of the message (you'd use a clipboard, of course, but it wouldn't make your life much easier). It's obvious that you'd probably make some mistakes during the amendment process, and you (and your boss) would get a bit frustrated. Is it possible to separate such a repeatable part of the code, name it and make it reusable? It would mean that a change made once in one place would be propagated to all the places where it's used. Of course, such a code should work only when it's explicitly launched. Yes, it's possible. This is exactly what functions are for.

print("Enter a value: ") a = int(input()) print("Enter a value: ") b = int(input()) print("Enter a value: ") c = int(input()) ---------------------------------- def message(): print(" Please enter a value: ") message() a = int(input()) message() b = int(input()) message() c = int(input())


Ensembles d'études connexes

Bonus Chapter A Working within the Legal Environment

View Set

Chapter 27 programmable controllers

View Set

Fizx First Fall Final Study Guide

View Set