Exploring Functions in Python

Eylem Aytas
4 min readJun 7, 2023

--

Python is a powerful programming language that appeals to both beginners and experienced developers. One of the key reasons behind Python’s success is its rich standard library and numerous useful features. Functions are one such feature.

Functions are an essential part of Python programming as they allow you to reuse code, organize your program, and make it more modular. By defining functions, passing arguments, and returning values, you can create efficient and maintainable code.

In this blog post, we will explore how functions are defined, called, and used in Python. We’ll cover important concepts such as function parameters, return values, local and global variables.

Defining and Calling Functions:

To define a function, you start with the keyword def, followed by the name of the function and parentheses. If the function accepts any parameters, you can include them within the parentheses. After the closing parenthesis, you use a colon to indicate the start of the function's code block. The code block should be indented to distinguish it from the rest of the code.

Here’s an example that demonstrates the definition of a simple function:

def greet(name):
print("Hello, " + name + "!")

# Calling the function
greet("Eylem") # Output : Hello, Eylem!

Creating a function with optional parameter:

In Python, function parameters can have default values assigned to them. Default parameters allow you to specify a default value that will be used if no argument is provided for that parameter when the function is called. This provides flexibility and convenience in function usage.

Here’s a simple example to illustrate function parameters with default values:

def greet(name = ‘Eylem’, age = 25):
print(f"Hello, {name}! You are {age} years old.")
greet() #Output : Hello, Eylem! You are 25 years old.

In this example, we have a variable name with the value "Eylem" and a variable age with the value 25. We use an f-string by prefixing the string with the letter 'f'. Inside the string, we enclose the variables within curly braces {} to perform string interpolation.

Creating a function with a return statement:

The return statement allows you to specify the value that the function should return when it is called.

Here’s a simple example to illustrate creating a function with a return statement:

def add(a, b):
return a + b

result = add(5, 6)
print(result) #Output : 11

In this example, we define a function called add that takes two parameters, a and b. Inside the function, we use the return statement to specify that the function should return the sum of a and b. When we call the function add(5, 6), it returns the value 11, which is then stored in the result variable. Finally, we print the value of result, which is 11.

Calling a function within another function:

This feature allows you to reuse existing functions and combine their functionality. You can call a function from inside another function by simply using the function name followed by parentheses and any required arguments.

Here’s a simple example to demonstrate calling a function within another function:

def greet(name):
return "Hello, " + name + "!"

def greet_and_inform(name):
message = greet(name)
print(message)
print("Welcome to my blog post!")

greet_and_inform("Eylem")

#Output:
#Hello, Eylem!
#Welcome to my blog post!

The greet function takes a parameter name and returns a greeting message. The greet_and_inform function also takes a parameter name, and it calls the greet function within its code block, passing name as an argument. It then prints the returned message from greet function and adds an additional message.

When we call greet_and_inform("Eylem"), it triggers both functions. First, the greet function is called, which returns the greeting message "Hello, Eylem!". Then, the greet_and_inform function prints that message along with "Welcome to my blog post!".

Recursive Functions:

Recursive functions in Python are functions that call themselves within their own definition. They are used to solve problems by breaking them down into smaller instances of the same problem. Recursive functions have a base case, which is a condition that determines when to stop the recursive calls.

Here’s a simple example of a recursive function:

def countdown(n):
if n <= 0:
print("Go!")
else:
print(n)
countdown(n - 1)

countdown(3)
#Output:
# 3
# 2
# 1
# Go!

In this example, the countdown function takes a number n as input. If n is less than or equal to 0, it prints "Go!" and stops the recursion. Otherwise, it prints the value of n and calls itself with n - 1 as the argument. This continues until the base case is reached.

Global and Local Variables in Python:

Scopes in Python determine variable accessibility. There are three types: global, local, and nested.

Example:

def my_function():
x = 10 # Local variable
print(x)

x = 5 # Global variable
my_function() # Output: 10
print(x) # Output: 5

Lambda Functions in Python:

Lambda functions in Python are anonymous functions that can be defined in a single line. They are typically used for simple and concise operations without the need for a full function definition.

Here’s a very short example:

addition = lambda x, y: x + y
result = addition(4, 5)
print(result) # Output: 9

In this example, the lambda function addition takes two arguments, x and y, and returns their sum. It is defined in a single line using the lambda keyword. We can then call the lambda function by passing the arguments in parentheses, just like any other function.

Lambda functions are useful when you need a simple, short-lived function without the need for a formal function definition. They are commonly used in functional programming paradigms and as arguments for higher-order functions like map() and filter().

Thanks for reading! ❤

--

--