Introduction to Functions in Python

| Page Views: 36

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

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post