How do I call a function in Python?

by Alex
How do I call a function in Python?

As you know, functions are blocks of instructions designed to perform certain tasks in programming. Functions allow you to break up large pieces of code into smaller chunks or modules. Later they can be called from anywhere. This allows you to reuse certain parts of a program and avoid repetition. Functions can be defined within classes, modules, nested functions, and so on. The main features of functions in Python:

  • Used to avoid repetitions in the code,
  • Use to divide code into smaller modules
  • Allow code to be hidden and create clarity for understanding modules,
  • Allow code to be reused and save memory,
  • Code from a function can only be executed by its name,
  • Simple syntax: def function_name(parameters):.

Rules for creating functions:

  1. Python uses the keyword def to declare a function.
  2. The function name must begin with a Latin alphabet character in any case or a lowercase underscore.
  3. Each function has a colon and an indent, after which the program code itself is written.
  4. Reserved keywords cannot be used as a function name.
  5. A function can have several parameters or none at all.

Creating a function in Python

To create, you need to write the keyword def. The syntax is as follows:

def function_name():
# function logic
return result # return value

Let’s create and call a real function in Python:


def my_fun():
print("How do you call a function in Python?")
my_fun() # function call

Conclusion: How to call a function in Python?

Calling a function in Python

After you create a function, you can call it by writing its name or assigning it to a variable:


def my_fun():
x = 22 ** 5
return x
# 1. Calling the function
my_fun()
# 2. Call the function and assign the result to a variable
a = my_fun()
# 3. Call the function and print the result
print(my_fun())

Let’s create a simple function that returns nothing and call it.


def my_fun():
print("Hello World")
print("function called")
my_fun()

Conclusion:

Hello World
This function is called

In this example, calling my_fun() resulted in two lines of output.

Calling nested functions in Python

One function inside another is a nested function. You can create nested functions using the same keyword def. After creating a function, you need to call both the external and internal function. Let’s create a program to figure this out with an example.


def out_fun():
print("Hello, this is an external function")
def in_fun():
print("Hi, this is an internal function")
in_fun()
out_fun()

Conclusion:

Hello, this is an external function
Hi, this is an internal function

Here in_fun() is defined inside out_fun(). To call in_fun() you must first call out_fun(). Then out_fun() will start executing, which will cause in_fun() to be called. The internal function will not be executed unless the external function is called. Another example. A program to output the result of adding two numbers using nested functions in Python.


def fun1():
a = 6
def fun2(b):
a = 4
print ("Sum of the internal function", a + b)
print("Value of external variable a", a)
fun2(4)
fun1()

Output:

Value of external variable a 6
The sum of the internal function 8

Functions as first class objects

In Python, functions are first class objects. They have the same properties and methods as regular objects. For example, a function can be assigned to a variable, passed as an argument, stored in a data structure and returned as the result of another function. All data in Python are represented as objects or relations.

Features of functions as objects of the first class:

  1. Functions can be assigned to variables.
  2. A function can be an example of an object.
  3. A function can be returned from a function.
  4. Functions have the same properties and methods as objects.
  5. A function can be passed as an argument when another function is called.

Let’s take an example:


def my_object(text):
return text.upper()
print(my_object("Call my_object"))
upper = my_object
print(upper("Call upper"))

Conclusion:

CALLING MY_OBJECT
CALL UPPER

Let’s write a program to call a function in a class (more precisely, it will be a method of the class).


class Student:
no = 101
name = "Vladimir"
def show(self):
print("no {}nname {}".format(self.no, self.name))
stud = Student()
stud.show()

Conclusion:

№ 101
Name Vladimir

Related Posts

LEAVE A COMMENT