Javatpoint Logo
Javatpoint Logo

How to call a function in Python?

Functions are an essential part of programming in Python, allowing you to encapsulate code into reusable blocks. Understanding how to call a function correctly is fundamental to writing effective Python code. In this article, we will explore the various ways to call functions in Python, including passing arguments, returning values, and handling exceptions.

1. Function Basics

Before diving into calling functions, let's review the basics of defining a function in Python:

This defines a simple function called greet that prints "Hello, world!". To call this function, you simply use its name followed by parentheses:

Output:

Hello, world!

2. Function Arguments

Functions in Python can also accept arguments. Here's an example of a function that takes a name as an argument and greets the person by name:

To call this function with an argument, you pass the argument inside the parentheses:

Output:

Hello, Alice!

3. Returning Values

Functions can also return values using the return statement. For example, let's create a function that calculates the square of a number and returns the result:

To use the returned value, you can assign it to a variable:

Output:

25

4. Default Arguments

You can also define functions with default arguments. These arguments have a default value that is used if the function is called without providing a value for that argument. For example:

Output:

 Hello, world!

Output:

Hello, Bob!

5. Variable Number of Arguments

Python functions can accept a variable number of arguments using the *args syntax. This allows you to pass a varying number of arguments to the function. For example:

Output:

15

6. Keyword Arguments

Functions can also accept keyword arguments using the **kwargs syntax. This allows you to pass a variable number of keyword arguments to the function. For example:

Output:

name: Alice
age: 30
city: New York

7. Calling Functions from Functions

You can call one function from another function. This allows you to reuse code and create more modular programs. For example:

Output:

Hello, Alice!
Hello, Bob!
Hello, Charlie!

8. Handling Exceptions

When calling functions, it's important to handle exceptions that may occur. For example, if you try to convert a string to an integer, you may encounter a ValueError if the string is not a valid integer:

Output:

123

Output:

Invalid input!

Conclusion

In this article, we've covered the basics of calling functions in Python, including passing arguments, returning values, and handling exceptions. Functions are a powerful tool in Python that allow you to write more readable, modular, and maintainable code. By mastering the art of calling functions, you can take your Python programming skills to the next level.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA