Functions in python

Functions in python are one of the fundamental concepts. Functions are created to perform a specific task. Functions contain a block of code that gets executed when it is called. It’s good practice to create different functions for different tasks. Also if the same task has to be done, you just have to make a call to an already created function that can do the task. Functions are helpful to organize code. Functions in python have to be defined first and then you can make a call to functions below function code-lines.

Example:

def show_msg():
    print("Hi,")
    print("Welcome to python tutorial.")


show_msg()

Output:

Hi,
Welcome to python tutorial.

Parameters to functions:

You can pass parameters to functions that can be used in functions. When defining a function, we use parameters. When calling functions, we use arguments.

Example:

In the below code, the show_msg() function is called twice with different parameters.

def show_msg(name, country):
    print(f"Hi, {name}")
    print(f"Welcome to {country}.")


show_msg("John", "US")
show_msg("Jane", "UK")

Output:

Hi, John
Welcome to US.
Hi, Jane
Welcome to UK.

Note that in the above code, arguments are passed as positional parameters which means you have to pass arguments in the same order of parameters are used.

If you want to pass arguments in any order, then you can use keyword arguments.
You can learn keyword arguments below.

Using keyword arguments:

If you want to pass arguments in any order when calling to a function, then you have to use keyword arguments. When using keyword arguments, the position doesn’t matter. By using keyword arguments, readability is improved. keyword arguments are used mostly in cases where you are passing numerical arguments.

Example:

Notice that, in below code country argument is passed firstly, and then name argument is passed. But they are accepted in the right order by functions as their parameters. This can be done using keyword arguments.

def show_msg(name, country):
    print(f"Hi, {name}")
    print(f"Welcome to {country}.")


show_msg(country="US", name="John")
show_msg(country="UK", name="Jane")

Output:

Hi, John
Welcome to US.
Hi, Jane
Welcome to UK.

**Note:

If you want to use both positional and keyword arguments when making calls to a function, then you must use positional arguments firstly, and then you have to use keyword arguments.

Returning values from functions:

You can also return values from functions that can be the result of a specific task.

Example:

In the below example, the addition of two numbers is returned.

def addition(num1, num2):
    return num1 + num2


result = addition(2, 8)
print(result)

Output:

10

Summary:

  1. Functions are created to perform a specific task. Functions contain a block of code that gets executed when it is called.
  2. Functions are helpful to organize code.
  3. You can pass parameters to functions that can be used in functions. When defining a function, we use parameters. When calling function, we use arguments.
  4. Arguments by default get sent as positional arguments i.e in position order. If you want to pass arguments in any order when calling to functions in python, then you have to use keyword arguments.
  5. You can also return values from functions that can be a result of a specific task.

Check out Recommended Posts:

if, elif and else in python

Lists in python

Tuples in python

Dictionary in python

Loops in python

Variables in python

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments