Defining and Calling a Function

Defining and Calling a Function

Defining and Calling a Function

Defining and Calling a Function: 

To define a function in Python, you use the def keyword, followed by the function name and the function parameters (if any). Here’s an example:

				
					console.log( 'Code is Poetry' );# Define a function that adds two numbers 
def add_numbers(x, y):
 sum = x + y 
return sum 

				
			

In this example, we define a function called add_numbers that takes two parameters x and y. The function computes the sum of x and y, stores the result in a variable called sum, and then returns the value of sum.

It’s important to note that the body of the function is indented. In Python, indentation is used to indicate the scope of the code block. The code that is indented under the function definition is part of the function body.

To call a function in Python, you simply write the function name followed by parentheses and any arguments (if any) that the function requires. For example:

				
					console.log( 'Code is Poetry' );# Define a function that adds two numbers
def add_numbers(x, y):
    sum = x + y
    return sum

# Call the add_numbers function
result = add_numbers(3, 5)
print(result)   # Output: 8

				
			

In this example, we define a function called add_numbers that takes two parameters x and y. We then call the add_numbers function with the arguments 3 and 5 by writing the function name followed by parentheses and the argument values inside the parentheses. The result of the function call is stored in a variable called result. We then print the value of result, which is 8.

If a function doesn’t require any arguments, you simply call it by writing the function name followed by parentheses, like this:

				
					console.log( 'Code is Poetry' );# Define a function that prints a message
def print_message():
    print("Hello, World!")

# Call the print_message function
print_message() # Output: Hello, World!

				
			

# Output: Hello, World!

In this example, we define a function called print_message that doesn’t take any arguments. We then call the print_message function by writing the function name followed by parentheses.

It’s important to note that the function must be defined before it can be called. If you try to call a function that hasn’t been defined yet, you’ll get a NameError.

In Python, everything is passed by reference, but the terms “pass by reference” and “pass by value” have a slightly different meaning in Python than in other programming languages.

In Python, when you pass an object (such as a list, dictionary, or class instance) to a function, a reference to the object is passed to the function. This means that if the function modifies the object (e.g. by changing the values in a list), the changes will be reflected in the original object, even after the function returns.

Here’s an example to illustrate this:

				
					# Define a function that modifies a list
 def modify_list(my_list):
 my_list.append(4) 
my_list[0] = 99            # Call the function with a list argument
 my_list = [1, 2, 3] 
modify_list(my_list) 
print(my_list)

				
			

# Output: [99, 2, 3, 4]

In this example, we define a function called modify_list that takes a list parameter my_list. The function appends the value 4 to the list and changes the first element to 99. We then call the modify_list function with a list argument my_list, which is modified by the function. When we print my_list after calling the function, we can see that the changes made inside the function are reflected in the original list.

However, it’s important to note that when you pass immutable objects (such as numbers or strings) to a function, a copy of the object is passed to the function. This means that any modifications made to the object inside the function will not affect the original object.

Here’s an example to illustrate this:

				
					my_number += 1        # Call the function with a number argument my_number = 1
modify_number(my_number) 
print(my_number) 

				
			

# Output: 1

In this example, we define a function called modify_number that takes a number parameter my_number. The function increments the value of my_number by 1. We then call the modify_number function with a number argument my_number, which is not modified by the function. When we print my_number after calling the function, we can see that the value of my_number is still 1.

Pass by reference and Pass  by value:

In Python, everything is passed by reference, but the terms “pass by reference” and “pass by value” have a slightly different meaning in Python than in other programming languages and the behavior of passing mutable and immutable objects can sometimes make it appear like pass by value.

When an immutable object (such as an integer, float, or string) is passed to a function, a copy of the object is created and passed to the function. This means that any modifications made to the object inside the function will not affect the original object. This behavior is similar to pass by value.

				
					def increment(x):
    x += 1
    print(x)

a = 10
increment(a)   # Output: 11
print(a)       # Output: 10

				
			

In this example, we define a function increment that takes an integer parameter x. Inside the function, we increment the value of x by 1 and print the value of x. We then call the increment function with an integer argument a, which is not modified by the function. When we print a after calling the function, we can see that the value of a is still 10.

When a mutable object (such as a list or dictionary) is passed to a function, a reference to the object is passed. This means that any modifications made to the object inside the function will affect the original object. This behavior is similar to pass by reference.

				
					def add_item(lst, item):
    lst.append(item)
    print(lst)

my_list = [1, 2, 3]
add_item(my_list, 4)   # Output: [1, 2, 3, 4]
print(my_list)         # Output: [1, 2, 3, 4]

				
			

In this example, we define a function add_item that takes two parameters, lst and item. Inside the function, we append item to lst and print the new value of lst. We then call the add_item function with a list argument my_list, which is modified by the function. When we print my_list after calling the function, we can see that the changes made inside the function are reflected in the original list.

In summary, all function arguments in Python are passed by reference, but the behavior of passing mutable and immutable objects can sometimes make it appear like pass by value.

Join To Get Our Newsletter
Spread the love