Lesson 6 of 47
In Progress

Functions

Yasin Cakal

Welcome to the Data Structures and Algorithms with Python course! In this course, we will learn about the basic concepts of data structures and algorithms and how to implement them in Python. In this article, we will be focusing on functions in Python.

Functions are an essential part of Python programming and provide a powerful and flexible way of constructing programs. Functions allow us to break down complex tasks into smaller, more manageable pieces which can be easily reused, tested, and debugged. In this article, we will learn about the fundamentals of functions and how to create them in Python. We will also look at some of the built-in functions available in Python and how to use them.

What is a Function?

A function is a reusable block of code that performs a specific task. It can take any number of arguments, perform some processing, and return an output. In Python, functions are defined using the “def” keyword followed by the function name and parentheses containing the parameters.

For example, here is a simple function that takes two arguments and returns the sum of them:

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

This function can now be called with two arguments and it will return the sum of them.

sum(2, 3)
# Output: 5

The function can also be called with different arguments and it will return a different result.

sum(4, 5)
# Output: 9

Using functions has many advantages. First, it makes the code more organized and readable. It also helps to reduce the amount of code needed to achieve a task. For example, if we need to perform a certain task multiple times, we can write a function to do it once and then call the function multiple times. This eliminates the need to rewrite the same code over and over again.

Another advantage of using functions is that they can be tested and debugged individually. This makes it much easier to find and fix any bugs that might be present in the code.

Finally, functions make it easy to reuse code. Once a function is written, it can be used in any other program without having to rewrite it. This is especially useful when creating large and complex programs.

Creating Functions

Now that we understand what a function is and why it is useful, let’s look at how to create one in Python. As mentioned earlier, functions are defined using the “def” keyword followed by the function name and parentheses containing the parameters. The body of the function is indented and contains the code that will be executed when the function is called.

For example, here is a function that takes two numbers as arguments and returns their sum:

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

This function can now be called with two arguments and it will return the sum of them.

sum(2, 3)
# Output: 5

The body of the function can also contain multiple lines of code. Here is a more complex example:

def multiply(a, b):
result = a * b
return result

This function takes two numbers as arguments, multiplies them together, and returns the result.

multiply(2, 3)
# Output: 6

Built-in Functions

Python also provides a number of built-in functions that can be used to perform common tasks. These functions are already defined and can be called directly without having to define them first.

For example, the “print()” function can be used to print a value to the console.

print(2)
# Output: 2

The “len()” function can be used to get the length of a string or list.

len('Hello World!')
# Output: 12

The “min()” and “max()” functions can be used to get the minimum and maximum values from a list.

min([1, 2, 3, 4])
# Output: 1

max([1, 2, 3, 4])
# Output: 4

There are many other built-in functions available in Python which can be used to simplify common tasks.

Passing Arguments

In Python, arguments can be passed to a function in two ways: by position or by keyword. When passing arguments by position, the order in which the arguments are passed must match the order in which they were defined in the function.

For example, here is a function that takes two numbers as arguments and returns their sum:

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

This function can be called with two arguments and the result will be the sum of the two numbers.

sum(2, 3)
# Output: 5

The arguments can also be passed in a different order and the result will still be the same.

sum(3, 2)
# Output: 5

When passing arguments by keyword, the order in which the arguments are passed does not matter. Instead, the argument names must match the parameter names defined in the function.

For example, here is a function that takes two numbers as arguments and returns their sum:

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

This function can be called with two arguments and the result will be the same as before.

sum(a=2, b=3)
# Output: 5

The arguments can also be passed in a different order and the result will still be the same.

sum(b=3, a=2)
# Output: 5

Default Arguments

Python allows functions to have default arguments. When a default argument is specified, it will be used if no value is passed for that argument when the function is called.

For example, here is a function that takes two numbers as arguments and returns their sum. The second argument has a default value of 0.

def sum(a, b=0):
return a + b

This function can be called with one argument and the result will be the same as before.

sum(2)
# Output: 2

The second argument can also be passed with a different value and the result will be different.

sum(2, 3)
# Output: 5

This is a useful feature because it allows us to create functions with fewer arguments and still get the same result.

Conclusion

In this article, we have learned about functions in Python and how to create them. We have also seen some of the advantages of using functions and some of the built-in functions available in Python. Finally, we have looked at how to pass arguments to a function and how to use default arguments. Coding

Exercises

Write a function that takes two numbers as arguments and returns the difference of them.

def difference(a, b):
return a - b

Write a function that takes a list as an argument and returns the maximum value in the list.

def max_value(lst):
return max(lst)

Write a function that takes a string as an argument and returns the number of characters in the string.

def num_chars(str):
return len(str)

Write a function that takes a list as an argument and returns the average value of the list.

def average(lst):
return sum(lst) / len(lst)

Write a function that takes two strings as arguments and returns the concatenation of the two strings.

def concat(str1, str2):
return str1 + str2