Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

The print() function

Look at the line of code below:

print ("Hello, World!")

The word print that you can see here is a function name. That doesn't mean that wherever the word appears it is always a function name. The meaning of the word comes from the context in which the word has been used. 

You've probably encountered the term function many times before, during math classes. You can probably also list several names of mathematical functions, line sine or log. 

Python functions, however, are more flexible, and can contain more content than their mathematical siblings. 

A function (in this context) is a separate part of the computer code able to: 

  • cause some effect (e.g., send text to the terminal, create a file, draw an image, play a sound, etc.); this is something completely unheard of in the world of mathematics. 
  • evaluate a value (e.g., the square root of a value or the length of a given text) and return it as the function's result; this is what makes Python functions the relatives of mathematical concepts. 

Morever, many of Python functions can do the above two things together. 

Where do functions come from? 

  • They may come from Python itself; the print function is one of this kind; suhc a function is an added value received together with Python and its environment (it is built-in); you don't have to do anything special (e.g., ask anyone for anything) if you want to make use of it
  • they may come from one or more of Python's add-ons name modules; some of the modules come with Python, others may require separate installation - whatever the case, they all need to be explicitily connected with your code (we'll show you how to do that soon)
  • You can  write them yourself, placing as many functions as you want and need inside your program to make it simpler, cleare and more elegant

The name of the function should be significant (the name of the print function is self-evident)

Of course, if you're going to make sure of any already existing function, you have no influence on its name, but when you start writing your own functions, you should considet your choice of names. 

 

The print () function

As we said before, a function may have: 

  • an effect
  • result

There's also a third, very important, function component - the argument(s).

Mathematical functions usually take one argument, e.g., sin(x) takes an x, which is the measure of an angle. 

Python functions, on the other hand, are more versatile. Depending on the individual needs, they may accept any number of arguments - as many as necessary to perform their tasks. Note: any number includes - zero -some Python functions don't need any argument

print("Hello, World!")

In spite of the number of needed/provided arguments, Python functions strongly demand the presence of a pair of parentheses - opening and closing ones respectively. 

If you want to deliver one of more arguments to a function, you place them inside the parentheses. If you're going to use a function which doesn't take any argument, you still have to have the parentheses. 

Note: to distinguish ordinary words from function names, place a pair of empty parentheses after their names, even if corresponding function wants one or more arguments. This is a standard convention. 

The function we're talking about here is print()

Does the print() function in our example have any arguments? 

Of course it does, but what are they?