Introduction to Functions in Python

April 13, 2018 Oclemy Python Function 1 minute, 9 seconds

At the core of programming languages is the idea of functions.

However, languages tend to treat them differently.

In most programming languages, the attitute towards a function is as a way of encapsulating code. They take some parameters, execute code and return data and that's it.

However, python as well as many modern languages do take the notion of function much more than just a code capsule. They treat functions as full objects. As objects they can be passed around, wrapped in other functions or replaced by new implementations.

In short functions are also types.

The built-in function type forms the basis of functions in Python.

Checking Function Type

In fact we can check the type of a given function using the type() function.

def greet():
    print('Hello')

print(type(greet))

Result

<class 'function'>

Function Arguments/Parameters

Functions always can take arguments. And this is one of the secrets of their power. However, python takes this power to the next level in that it allows us pas keyword arguments and even arguments that won't be know until runtime when the function is invoked.

For example, the following simple example allows us specify an argument that will only be entered at runtime.

def greet(name):
    return 'Hello ' + name

name=input("Enter You Name to receive a Greeter: ")
print(greet(name))

Result

Enter You Name to receive a Greeter: Oclemy
Hello Oclemy

Comments