Functions in Python

Document
Image description

Functions

The functions in python can be defined as the reusable block of codes that perform a certain task. Before we start defining functions ourselves, we’ll look at some of Python’s built-in functions. Let’s start with the most well-known built-in function, called print:

            
>>> print('Hello, readers!')
 Hello, readers!
 >>> print(15)
 15
                
            
        

Creating python functions

In programming language there are certain syntax we need to follow and python is not an exception

    
def function_name:
  #statements
  return 
 
    

Advantages of using functions

Advantages of using functions

A Python function can be defined once and used many times. So it aids in code reuse: you don’t want to write the same code more than once. Functions are a great way to keep your code short, concise, and readable. By giving a function a well-chosen name, your code will become even more readable because the function name directly explains what will happen. This way, others (or future you) can read your code, and without looking at all of it, understand what it’s doing anyway because of the well-chosen function names.

Return values

A function can return a value. This value is often the result of some calculation or operation. In fact, a Python function can even return multiple values. We can make this more interesting by allowing an argument to be passed to our function. Again we define a function with def, but we add a variable name between the parentheses:

            
 >>> def say_hi(name):
      print('Hi', name)
>>> say_hi('chiku')
Hi chiku
            
        

Our function now accepts a value, which gets assigned to the variable name. We call such variables the parameter, while the actual value we provide (‘Chiku’) is called the argument.

Variable scope

The variable name only exists inside our function. We say that the variable’s scope name is limited to the function say_hi, meaning it doesn’t exist outside of it.

Scope

The visibility of a variable is called scope. The scope defines which parts of your program can see and use a variable.
If we define a variable at the so-called top level of a program, it is visible in all places.

                
>>> def say_hi():
...    print("Hi", name)
...    answer = "Hi"
...
>>> name = 'Chiku'
 >>> say_hi()
 Hi Chiku
 >>> print(answer)
Traceback (most recent call last):
 File "", line 1, in 
                
            

NameError: name 'answer' is not defined say_hi was able to use the variable name, as expected, because it’s a top-level variable: it is visible everywhere. However, answer, defined inside say_hi, is not known outside of the function and causes a NameError. Python gives us an informative and detailed error: “name ‘answer’ is not defined.”

Comments

Popular posts from this blog

Recursion In Python

Tkinter and Api

Sets in python