fbpx
1- Introduction
2- Python Basic
3- Learning Python
4- Python Literals
5- Arithmetic operators
6- Function INPUT() and STRING operations
7- Comparison Operators and Conditions
8- Loops in Python
9- Logic and bit operators
10- Lists and Arrays in Python
11- Functions
12- Tuples, Dictionaries
13- Conclusion
14- Practice Exams

L11.1 What are functions in Python

Function

A function is a block of code which only runs when it is called. You are aware of print() function and you have used it at various occasions earlier.

Now, we are taking this concept further. In this lesson, you’ll learn how to write your own functions, and how to use them. We’ll write several functions together, from the very simple to the rather complex

Few things to remember about functions

  •  If a particular fragment of the code begins to appear in more than one place, consider the possibility of isolating it in the form of a function 
  • A well-written function should be viewed entirely in one glance. Beautification of your code program. 😉
  • A good and attentive developer (you) 🙂 divides the code into well-isolated pieces, and encodes each of them in the form of a function.
  • 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 is called decomposition.

Sources of functions

Functions comes mainly from three places:

  • 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 built-in functions;
  • from Python’s preinstalled modules – 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.
  • directly from your code – you can write your own functions, place them inside your code, and use them freely;

Let’s write your first function

You need to define your first function. Take a look at following syntax.

def function_name():
    function_body

Things to remember:

  • 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
  • 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.
  • It should be manually invoked functionname() such as print(). If you don’t invoke it, it won’t work.

Try below code. What’s wrong with ?

def message():
    print("Enter a value: ")

print("We start here.")
print("We end here.")

The problem with above code is that it doesn’t invoke your function message(). Look at below code and notice how we invoke our function.

def message():
    print("Enter a value: ")

print("We start here.")
message()
print("We end here.")

Remember following:

  • You mustn’t have a function and a variable of the same name.
  • If you invoke a function before defining it, you will receive an error.
  • You can write function anywhere in your program. It is not necessary like some other popular programming languages to put all your functions at the top of your source file

0
Wanna ask a question or say something? Go aheadx
()
x
Scroll to Top