Functions in Python, basic use, Scope, basic use, parameters, returns

We will see how to use functions in Python, their syntax, as well as their importance for reusing code and passing data through arguments, returns, among others.

Functions are also another key piece in programming languages that allow you to easily reuse any code in the application. Throughout this chapter, we have used several functions that are part of Python such as len(), type(), among others; if we want to call the following operations several times:

a=5
b=6
print("Sum numbers")
c = a+b
print("res "+str(c))

With functions, we can easily group them, to do so, we place the reserved word def followed by the name of the function and some parentheses:

def sum():
  a=5
  b=6
  print("Sum numbers")
  c = a+b
  print("res "+str(c))

As in the case of conditionals and loops, they allow you to create blocks of code, but this time we can easily invoke them; the previous code block looks like:

def sum():
  a=5
  b=6
  print("Sum numbers")
  c = a+b
  print("res "+str(c))

And to invoke it, we use the name of the function, in this example, sum():

sum()

The advantage we have is that since everything is encapsulated (in a scope other than the global one), we can easily reuse the previous function as many times as we want by invoking said function, for example, if we want to invoke the body of the function 3 times, it we invoke 3 times:

sum()
sum()
sum()

Parameters

With parameters, we can make truly reusable blocks of code, for example, we can create a function that receives an argument to print, for example, a name:

def yourName(name):
    print(name)
    
yourName('andres')

You can specify the type:

def yourName(name:str):
  ***

What is recommended to avoid working with incorrect data, or also receiving several parameters, for example, to add them:

def sum(a:int, b:int, c:int):
  print("res "+str(a+b+c))
  
sum(1,2,3)

Or perform mathematical operations:

def operations(a:int, b:int, op:str):
  if op == '+':
    print("res "+str(a+b))
  elif op == '-':
    print("res "+str(a-b))
  elif op == '*':
    print("res "+str(a*b))
  elif op == '/':
    print("res "+str(a/b))
operations(1,2,'+')

Also, we can define default values:

def operations(a:int, b:int, op:str='*'):
  ***

And we can indicate the name of the parameters when invoking the function:

operations(b=2,a=5)

With the advantage that, we can place them in any order:

operations(a=5,b=2)

These functions can also return a value, so that we can then use it wherever we want. To do this, we can use the reserved word return:

def operations(a:int, b:int, op:str='*'):
  if op == '+':
    return a+b
  elif op == '-':
    return a-b
  elif op == '*':
    return a*b
  else:
    return a/b
    
n = operations(b=5,a=2)
print("res "+str(n))

Also the return type, in this example, would be a float (due to division):

def operations(a:int, b:int, op:str='*') -> float:
  if op == '+':
    return a+b
  elif op == '-':
    return a-b
  elif op == '*':
    return a*b
  else:
    return a/b

Therefore, you can see a function as a piece of code designed to perform a task and, if necessary, return a value resulting from the operation performed.

Later, when we want to return an HTML page to the user, functions are used that return said HTML pages, but we will see this later in the book.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.