Why do we create a function in python?

In programming, a function is a reusable block of code that executes a certain functionality when it is called.

Functions are integral parts of every programming language because they help make your code more modular and reusable.

In this article, I will show you how to define a function in Python and call it, so you can break down the code of your Python applications into smaller chunks.

I will also show you how arguments and the return keyword works in Python functions.

Basic Syntax for Defining a Function in Python

In Python, you define a function with the def keyword, then write the function identifier (name) followed by parentheses and a colon.

The next thing you have to do is make sure you indent with a tab or 4 spaces, and then specify what you want the function to do for you.

def functionName():
    # What to make the function do

Basic Examples of a Function in Python

Following the basic syntax above, an example of a basic Python function printing “Hello World” to the terminal looks like this:

def myfunction():
    print("Hello World")

To call this function, write the name of the function followed by parentheses:

myfunction()

Next, run your code in the terminal by typing python filename.py to show what you want the function to do:

Why do we create a function in python?

Another basic example of subtractig 2 numbers looks like this:

def subtractNum():
    print(34 - 4)

subtractNum()
# Output: 30

Arguments in Python Functions

While defining a function in Python, you can pass argument(s) into the function by putting them inside the parenthesis.

The basic syntax for doing this looks as shown below:

def functionName(arg1, arg2):
    # What to do with function
    

When the function is called, then you need to specify a value for the arguments:

functionName(valueForArg1, valueForArg2)

Here's an example of arguments in a Python function:

def addNum(num1, num2):
    print(num1 + num2)
addNum(2, 4)

# Output: 6

In the example above:

  • I passed 2 arguments into the function named addNum
  • I told it to print the sum of the 2 arguments to the terminal
  • I then called it with the values for the 2 arguments specified

N.B.: You can specify as many arguments as you want.

How to Use the Return Keyword in Python

In Python, you can use the return keyword to exit a function so it goes back to where it was called. That is, send something out of the function.

The return statement can contain an expression to execute once the function is called.

The example below demonstrates how the return keyword works in Python:

def multiplyNum(num1):
    return num1 * 8

result = multiplyNum(8)
print(result)

# Output: 64

What’s the code above doing?

  • I defined a function named multiplyNum and passed it num1 as an argument
  • Inside the function, I used the return keyword to specify that I want num1 to be multiplied by 8
  • After that, I called the function, passed 8 into it as the value for the num1 argument, and assigned the function call to a variable I named result
  • With the result variable, I was able to print what I intended to do with the function to the terminal

Conclusion

In this article, you learned how to define and call functions in Python. You also learned how to pass arguments into a function and use the return keyword, so you can be more creative with the functions you write.

If you find this article helpful, don’t hesitate to share it with your friends and family.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Why Functions?

Functions are an essential part of most programming languages. Functions are reusable pieces of code that can be called using a function's name. Functions can be called anywhere in a Python program, including calling functions within other functions.

Functions provide a couple of benefits:

  • Functions allow the same piece of code to run multiple times

  • Functions break long programs up into smaller components

  • Functions can be shared and used by other programmers

Every function has a function name. The function name is used when the function is called in a program. Calling a function means running a function.

Functions can receive input from the program. The input provided to a function is called input arguments or just arguments. Arguments are the code passed to a function as input.

Functions can produce output. We say a function returns output to the program. The output of a function can be assigned to a variable for use in a program.

Below is an example calling Python's pow() a function:

In the function call above, the function name is pow. pow is the power function. The pow function raises a number to a power. The input arguments are the numbers 3 and 2. The function output is assigned to the variable out. In this example, the function returns the value 9 (3 raised to the 2 power, 3^2 = 9).

Why do we create functions in?

Functions enable programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task. Once a function is created, the details of how it works can almost be forgotten about.

What is a function and why it is used in Python?

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

How do you create a function in Python?

How to Create User-Defined Functions in Python.
Use the def keyword to begin the function definition..
Name your function..
Supply one or more parameters. ... .
Enter lines of code that make your function do whatever it does. ... .
Use the return keyword at the end of the function to return the output..

Why do we define a function?

A function is defined as a relation between a set of inputs having one output each. In simple words, a function is a relationship between inputs where each input is related to exactly one output. Every function has a domain and codomain or range. A function is generally denoted by f(x) where x is the input.