Defining Functions in Python

Defining Functions in Python

To write a function in Python, we use def keyword, and the name of our function. Below it, we should include a doscring that will tell the users of the function shortly what is the function for. Then, we write statements to execute. There are no curly braces ({}) like in C, but we use indentation. Thanks to that, we get a block of instructions.

def func_name(param_list):
    “””docstring.”””
    statement_to_execute

Let’s write a real function called sum():
def sum(a, b):
“””Summing two integers.”””

num_1 = int(a)
num_2 = int(b)
sum = num_1 + num_2
print(‘The sum of both numbers:’, sum)

print(‘Your first number: ‘)
l1 = input()
print(‘Your second number: ‘)
l2 = input()
sum(l1, l2)

I’ve defined the function that is named sum. It accepts two parameters (a and b). They can be used only in the scope of this function. They are local variables.

Then, we have a doctring:
“””Summing two integers.”””
It can explain the purpose of our function.

Our function changed two strings into integers, then we can get their sum.

num_1 = int(a)
num_2 = int(b)
sum = num_1 + num_2

int() is a built-in function for changing into integer other data types.

The changed values are assigned to two variables (num_1 and num_2), and then they are  added. The value is assigned to variable called sum.  And we display it with the use of print built-in function.

print(‘The sum of both numbers:’, sum)

Our function is defined, but not executed. All statements in this function will be executed after having called it. So definition isn’t the same what execution.

print(‘Your first number: ‘)
l1 = input()
print(‘Your second number: ‘)
l2 = input()
sum(l1, l2)

A prompt for an input is displayed. We can write the first number and then the second. But these inputs are string, so we needed the function int() to convert them into integers.

Now, the last line, our function is executed that is we write the name of our function and the parenthesis including two arguments. The arguments will be assigned to the parameters in the following way:

l1 = a

l2 = b

Parameters not always should be variables: they can be fixed by us:

sum(‘234′, ’29’)

The second part of this article:

3 thoughts on “Defining Functions in Python

  1. blenz3

    You might want to consider adding that functions in Python are objects. For example, if I define the following functions:

    def f(h):
    h():

    def g():
    print “hello”

    Then the following occurs:
    >>>f(g)
    hello

    It’s a nice trick and really valuable for a lot of use applications.

    Reply
  2. relocation moving

    Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s