Creating Own Function in Python

Python is a high-level programming language that is known for its simplicity and ease of use. One of the great features of Python is its ability to Creating Own Function in Python. A function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and more complex, functions make it more organized and manageable.

In Python, we create a function using the “def” keyword, followed by the function name and a set of parentheses. Within the parentheses, we specify the arguments (if any) that the function takes. The function body starts with a colon and contains one or more statements. The statements within the function should be indented. Here’s a simple example of a function in Python:

def greet(name):
print("Hello, " + name + ". How are you today?")
greet("John")

In this example, we have created a function named “greet“. It takes one argument, “name“. The function body consists of a single print statement that greets the person by name. To call the function, we simply call its name followed by a set of parentheses.

Functions in Python can also return values. To return a value from a function, we use the “return” keyword, followed by the value we want to return. Here’s an example of a function that returns a value:

def square(number):
return number * number

result = square(4)
print(result)

In this example, we have created a function named “square” that takes one argument, “number“. The function body consists of a single return statement that returns the square of the number passed as an argument. To call the function, we assign the result to a variable and then print it.

Functions in Python can also take multiple arguments. To specify multiple arguments, we simply list them within the parentheses, separated by commas. Here’s an example of a function that takes multiple arguments:

def add(num1, num2):
return num1 + num2
result = add(4, 5)
print(result)

In this example, we have created a function named “add” that takes two arguments, “num1” and “num2“. The function body consists of a single return statement that returns the sum of the two numbers passed as arguments. To call the function, we assign the result to a variable and then print it.

Functions in Python can also have default arguments. A default argument is an argument that takes a default value if no value is specified when the function is called. To specify a default argument, we simply assign a value to the argument in the function definition. Here’s an example of a function with a default argument:

def greet(name, greeting="Hello"):
print(greeting + ", " + name + ". How are you today?")
greet("John")
greet("Jane", "Hi")

In this example, we have created a function named “greet” that takes two arguments, “name” and “greeting“. The second argument, “greeting“, has a default value of “Hello“. To call the function, we can either pass both arguments or just pass the first argument. If we pass just the first argument, the second argument will take its default value.

Leave a Reply

Your email address will not be published. Required fields are marked *