Tags

, , ,

At some point in the history of computing, programming languages (at least the high-level languages) became collections of pre-made objects for building a program, so almost every program in a high-level language consists of function calls, even if we’re not conscious of it. As an example:

def Hello():
print("Hello")

It’s a function calling another function someone else made called ‘print()‘. This subtle principle is often overlooked when there’s too much focus on syntax and not enough on what’s happening behind the scenes, but it’s an important one. Where functions are taught as a method of structuring code, I view them as entities in an object-oriented system.

On the surface, functions within our source code make things modular, so it becomes fairly easy to isolate whichever part of the software you want to maintain or upgrade. They also make life easier when there are several developers working on the same software, as functions can be assigned to specific people, individually tested then put together. This allows for methodical software development that involves unit and integration testing. But this approach is useful for other things also.

Passing a Variable
My first example (in Python) is of a function passing a variable to the main program:

#The function returns 'Hello World'
def HelloWorld():
return "Hello World"

# Call HelloWorld() function
print HelloWorld()

The ‘def‘ statement is important to note, because it’s simply defining HelloWorld() as a function, and execution actually begins with the first instruction outside it. A function must be defined before it’s called, as with C++.
The print() function calls HelloWorld(), which executes and returns ‘Hello World‘ to print().

Also note I’ve added comments. Even if the code is self-explanatory, it’s good practice to give functions descriptive names and add short descriptive comments. Especially if someone else is reviewing the code.

Another Basic Example
Here’s a program that passes a variable to a function and processes whatever’s returned:

#Function to calculate area
def area(w, h):
return w * h

#Get width and height
w = input("Enter width: ")
h = input("Enter height: ")

#Print the variables returned from area()
print area(w, h)

Structuring a Program
I’ve added the following example to show how a slightly larger program, that would otherwise be a clusterfrack of if-then statements, could be nicely structured using functions:

python-structured

Again, execution doesn’t start at the beginning of our code, as the ‘def functionName()‘ statements are used for defining MainMenu(), AddNumbers() and SubNumbers(). Execution actually started when MainMenu() was called at the last line.

Make your Own Python Module
So far I’ve posted a 101 on programming, but as the foundation for some more interesting stuff that goes beyond ‘learning to code’, some of it I’ll cover in a later post.
Even the most basic programs call native functions that exist outside the source file, and sometimes functions that exist in some module/library that’s been imported into the same memory space as the program, or dynamically linked to it. With Python it’s remarkably easy to create or own library of functions (a module) that could be re-used by other programs.

I’ve done this by first creating a source file called ‘MyCalc.py‘, containing just the first two functions from the previous example. However, this particular file isn’t a Python script, but a module, a shared object.
To show the module in action, I created the script itself:

from MyCalc import AddNumbers, SubNumbers

#Main menu
def MainMenu():
print("[1] Add")
print("[2] Subtract")
print " "
choice = input("Enter choice: ")

if choice == 1:
AddNumbers()
if choice == 2:
SubNumbers()
else:
exit()

MainMenu()

The first line should be familiar. It imports AddNumbers() and SubNumbers() from MyCalc. Of course, the ‘import MyCalc‘ statement can be used on its own, but that means copying an entire module into the program’s memory space.
Interestingly, the module is automatically compiled into bytecode when it’s imported, making it a shared object for dynamic linking. Now imagine the module is a DLL and the Python script an application – that’s more or less how software works. If the module had functions for physics calculations and graphics rendering, it could be something like the Unreal game engine.
Download example code here…

Advertisements