DEV Community

Ahmed Gouda
Ahmed Gouda

Posted on • Updated on • Originally published at ahmedgouda.hashnode.dev

Python Functions

Most applications, regardless of the programming language they are written in, are broken up into smaller blocks known as functions, and Python is no different.

So let's start by defining a function.

# define a basic function
def func1():
    print("I am a function")

func1()
print(func1())
print(func1)
Enter fullscreen mode Exit fullscreen mode

Output:

I am a function
I am a function
None
<function func1 at 0x0000025C054DBF70>
Enter fullscreen mode Exit fullscreen mode

So functions are defined with the def keyword. The colon indicates the start of the function scope block.


A couple things to notice here. In some cases, in other programming languages, this colon will be replaced in say Java or C with curly braces to indicate scope. That's not how Python works. Python uses a colon, and then the next line is indented, it's up to you how many spaces you want to indent something.

In the result, you can see that I am a function got printed, and then the same string followed by the word None, and then finally this string 0x0000025C054DBF70. So in the first case, the function is being called directly, which executes the contents of the function, causing the print statement to print the string. In the second case, the function is also being called inside the print function, so the output is the same as in the first case, but then the outer print statement executes, and since our function doesn't return a value, Python evaluates the return value to be the Python constant of none, and then just prints the string representation of that. In the last case, the function itself is not being executed at all since we're not including those little parentheses that would call the function. We're just printing the value of the function definition itself, which evaluates to this string 0x0000025C054DBF70. This just demonstrates that functions themselves are objects that can be passed around to other pieces of Python code.

Function with arguments

# function that takes arguments
def func2(arg1, arg2):
    print(arg1, arg2)

# function that returns a value
def cube(x):
    return x * x * x

func2(10, 20)
print(func2(10, 20))
print(cube(3))
Enter fullscreen mode Exit fullscreen mode

Output:

10 20
10 20
None
27
Enter fullscreen mode Exit fullscreen mode

You can see that on the first line, 10 and 20 get printed with a space in between, and that's similar to the first result, and then the second line, we've got 10 and 20 being printed again with a value of none, and again that's happening because there's no return value from func2. And then for the cube function, when we print the cube of three, we get the value of 27. So in this case the function does return a value, and that's what gets printed.

Function with default value for an argument

# function with default value for an argument
def power(num, x = 1):
    result = 1
    for i in range(x):
        result *= num   #result*=num is same as result=result*num
    return result

print(power(2))
print(power(2, 3))
print(power(x = 3, num = 2))
Enter fullscreen mode Exit fullscreen mode

Output:

2
8
8
Enter fullscreen mode Exit fullscreen mode

Function power basically takes a number and raises it to the given power. And in the function definition, I've got x is equal to one, so this assigns a default value for that argument.


Here print(power(2)) I'm calling the function power, but I'm not giving it a value for x, so x is going to default to one.


And here print(power(x = 3, num = 2)) I'm reversing the order in which the arguments are called. So Python lets you call functions with their named parameters along with their value, and when you do this, the Python interpreter figures out which arguments to supply the values to. You don't have to call the function with the arguments in a particular order, if you simply supply the names along with the values.

Function with variable number of arguments

#function with variable number of arguments
def multi_add(*args):
    result = 0
    for x in args:
        result += x
    return result

print(multi_add(1, 2, 3))
print(multi_add(1, 2, 3, 10))
print(multi_add(1, 2, 3, 10, 16))
Enter fullscreen mode Exit fullscreen mode

Output:

6
16
32
Enter fullscreen mode Exit fullscreen mode

What I've done here is define a function called multi_add, and the star character means I can pass in a variable number of arguments. The function then loops over each argument and adds them all to a running total, which is then returned. You can combine a variable argument list with a set of formal arguments, but just keep in mind that the variable argument list always has to be the last parameter.

Top comments (0)