Friday, January 3, 2020

Variables, Functions and Procedures

Programming has become  a  must have skill in the modern era, it is now easier to say where
programming is not used than where it is used.

This blog will introduce concepts of programming using python, due to its syntax being more user friendly than most of the other languages.

Programming has two important aspects: data and operation we make with and to it.

In particular python thinks about data as an object, this means that all data in python is an object, this will be relevant later when we talk about functions and procedures.

Variables

The first thing we must be familiar with are variables, in programming variables can be seen as places where we "save" out data and this brings the two things we can do with variables:

1. We can give them a value.

2. We can access that value and use it.





So far so good, we now have data represented as objects in python and a place where we can store our data, so our next step is to do something with all those things.

Functions and Procedures

In python a function is like a mathematical function, we give it some values and it gives us value, what is particular about python is that we give objects to the function and it gives us an object.
Resultado de imagen para function diagram



The above image shows a way to define a function in python, we use def and give it a name and we give it as many parameters as we need, it does not matter that add's arguments have the same name that the variables x and y, the function call is just using the function as it was defined, we can give values to the function without having to store them in a variable first.

There is another way to define functions in python:



Lamba is a way to define an anonymous functions, it holds the idea of passing values and getting a result, it is important to notice that we assigned this function to a variable, this is a key aspect of python, functions are objects.

Other examples of this feature are:





Procedures have some similarities with functions, we gave them values but instead of returning a value they do something, it is not really accurate to say procedures do not return something though, they return a special value in Python called  None.



 In the next blog we will continue with if/else, switch, for and while statements.