How do you call a function in Python?

How do you call a function in Python?

 

Python Functions Examples: Call, Indentation, Arguments & Return Values

 

As you may already know, functions are the block of statements or code used to perform some specific and particular tasks in programming. They also help to break the large groups of code into smaller chunks or modules. You can call functions anywhere and any number of times in a program. It enables us to reuse the code by simply calling the particular function or a block in the program. Therefore, it prevents the repetition of the same code. We can define the functions inside the class, modules, or nested functions.

Features of Functions:-

 

Features of Python | Top 10 Features of Python programming | Edureka

 

The features of functions are under mentioned:

  1. Functions are used to avoid repetitions of the code.
  1. Using the functions, we can divide a group of code into smaller modules or chunks.
  2. Functions help to hide the code and create clarity to understand the modules.
  3. Functions allow code to be reused, thus saving memory.
  4. Statements that are written inside a function can only be executed with a function name.
  5. Python functions start with def and then a colon (:) followed by the function name.

Rules used for defining a function

  1. The def keyword can be used in the Python function to declare and define any function.
  2. The function name must commend with the following identifiers: A-Z, a- z, and underscore (_).
  3. Every function must have a colon (:) at the end of the function name and then indention to write the given program.
  4. In a Python function, the reserved words cannot be used as a function name or an identifier.
  5. In the Python, the function parameter can be empty or multiples.

How to Create a function in Python

To create a function, you as a user need to use the def keyword to declare or define or write a function in Python. Here is the syntax that is used for creating a function:

Syntax used

 

  1. def function_name(): # use def keyword to define the function
  2. Statement that is to be executed
  3. return statement # return a single value.

Now let’s create a function program in Python.

Myfun.py

  1. def myFun(): # define the function name
  2.     print(” Welcome to the JavaTpoint”)
  3. myFun() # call to print the statement

Output of the above code:

Welcome to the JavaTpoint

Function Calling in Python

Once you create a function in Python, you can call it by writing function_name() itself or another function or even nested function. Following is the syntax used for calling a function.

Syntax:

  1. def function_name():
  2.        Statement1
  3. function_name() # directly call the function
  4. # calling function using built-in function
  5. def function_name():
  6. str = function_name(‘johnny’) # assign the function to call the function
  7. print(str) # print the statement

Consider the given example to print the given Welcome Message using a function in Python programming language.

CallFun.py

  1. def MyFun():
  2.     print(“Hello World”)
  3.     print(” Welcome to the JavaTpoint”)
  4. MyFun() # Call Function to print the message.

Output:

Hello World

Welcome to the JavaTpoint

In the above example, we are calling the MyFun() function that prints the given statements.

Calling Nested Functions in Python

When we are constructing one function inside of another function, it is called a nested function. We can create a nested function using the def keyword. After creating the function, we have to call the outer as well as the inner function to execute the statement. Let us now create a program to comprehend the concept of nested functions and how we can call these functions.

Nest.py

  1. def OutFun(): # outer function
  2.     print(“Hello, this is the outer function”)
  3.     def InFun(): # inner function
  4.         print(“Hello, this is the inner function”)
  5.     InFun() # call inner
  6. OutFun() # call outer function

Output:

Hello, this is the outer function

Hello, this is the inner function

As we can already see in the above example, the InFun() function is being defined inside the OutFun() function. To call the above InFun() function, we are first calling the OutFun() function in the program. After we do that, the OutFun() function will start executing and then call InFun() as the mentioned output.

Note: To call the inner function, we have to first call the outer function. If the external function will not be invoked, the inner function cannot be executed.

Program used to print multiplication of two given numbers using nested function in Python programming language.

Nest_arg.py

  1. def fun1(): # outer function
  2.     a = 6 # define variable
  3.     def fun2(b): # inner function
  4.         a = 4 # inner variable
  5.         print (“Display the sum of the inner function”, a + b) # sum of inner function
  6.     print (“Display the value of the outer variable”, a) # it displays the value of outer function    fun2(4)  # Inner function

Output:

Display the value of the outer variable 6

Display the sum of the inner function 8

Functions as the First-Class Objects

In Python programming language, functions as First-Class Objects. This is due to the fact that it treats the same as the object, and it has the same properties and even the methods as an object. A function can be assigned to the variable, pass them as argument, store them in the data structures and then return a value from the other functions. It can even be manipulated, such as the other objects in Python. Furthermore, all the data that is stored in the Python program is represented in the objects or relations. Therefore it is also called as first-class citizens of Python function.

Properties of the First-Class functions

  1. Functions can be assigned to a given variable.
  2. A function is an example of the object type.
  3. We also have to return the function from a function.
  4. Functions have the exact same properties and methods as an object.
  5. The function is treated as an object to pass as an argument to some other function.

Now we will create a program to comprehend the Python functions as an object.

Obj.py

  1. def MyObject(text): # Pass an argument.
  2.     return text.upper()
  3. # Call the function inside the print() function.
  4. print (MyObject(“Welcome to the JavaTpoint”))
  5. str = MyObject # assign the function to a variable
  6. # call the function using the str variable.
  7. print (str(“Hello, Welcome to the JavaTpoint”))

Output:

WELCOME TO the JAVATPOINT

HELLO, WELCOME TO the JAVATPOINT

Write a program wherein you have to call a function inside the class.

Student.py

  1. class Student:
  2.     Roll_no = 101
  3.     name = “Johnson”
  4.     def show(self):
  5.         print(” Roll no. is %d\nName of student is %s” % (self.Roll_no, self.name))
  6. stud = Student() # Create the stud object of Student class
  7. stud.show()   # call the function using stud object.

Output:

Roll no. is 101

Name of student is Johnson

Folks that is it for today’s blog. I hope you were able to understand everything that I tried explaining. I hope you have a wonderful rest of your day!! Thank you for reading this blog. <3

September 5, 2021

0 responses on "How do you call a function in Python?"

Leave a Message