×

How to Call a Function in Python

How To Call a Function in Python

Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best way to reduce the size of code. Whenever we talk about using 'functions' in a code, we have to mention the following three things-

  • Function declaration
  • Function definition
  • Function calling

The reason behind why python is the most commonly used language among developers is its readability and how it simplifies complex programs.

So, when we are using functions in our python program, we just need to define and call it.

Let’s look at an example where we will see how functions are called in python.

 def add(a,b):    #function to add two numbers
     return a+b
 x=10
 y=20
 print(add(x,y)) #function call 

The above example shows how we can add two numbers in Python using functions.

The way to define functions in Python is-

def function_name(parameters):

                  return _______

  1. The keyword ‘def’ is used to define a function in Python
  2. After that, we must define the name of our function.
  3. And then specify the number of parameters involved in our function.
  4. Finally, we will write the expression required for desired result or output preceded with the keyword return.

In the example given above, we have made a function that adds two numbers.

We have defined the function using the 'def' keyword, named our function like add, and specified the parameters a and b that will hold the values for further computation.

Since the function 'add' will give us the addition of two numbers as the output, we will write this statement as return a+b.

Now the next step is to take the values from the user to check how our function works-

The values from the user can be taken in two ways-

  1. Assign the variables a value like-
 x=10
 y=20 
  • Ask the user to input the values of these variables using the ‘input’ function like-
 x=int(input(“Enter value of x: ”))
 y=int(input(“Enter value of y: ”)) 

Once we receive the values for our variables, we can call the function 'add' and display the result using the print function.

When the function is invoked, the control of the program passes to the called function, executes the statements/computes the values (in case of a mathematical expression) and then the control again comes back to the calling function.

Let’s look at some points we must keep in mind while declaring a function-

  1. The function name is given so that it can be identified uniquely in a function.
  2. Make sure you place a colon outside the parentheses in which you have specified the parameters.
  3. If your function is meant to simply print a statement or a set of statements, then you need not write anything in the parentheses.

For example-

 def message():
     print("Hello All")
 message()
 message() 
  • The indentation should be proper in the code block.
  • The number of arguments at the time of calling the function must be the same as specified in the function definition.
  • The return statement should be present within the function.
  • Once a value is returned from a function, it instantly marks the exit of that function.

LAMBDA FUNCTIONS

Lambda or anonymous functions are the functions that are not declared with the keyword ‘def’ and are a one-line version of our function.

They are created with the help of keyword lambda and contain a single line.

Let’s understand this with the help of an example-

 add=lambda a,b: a+b
 print(“Addition =”,add(2,4)) 

In the above example, the lambda function gives the sum of two parameters a and b, the output is evaluated with the help of the expression a+b and then the result is displayed.

Some Properties of Lambda Functions-

  1. There is no need to give a function name in this case.
  2. It returns a single value which is obtained by evaluating an expression.
  3. There is no need to explicitly mention the return statement.
  4. Since they are a one-line version, they cannot have multiple statements or expressions.
  5. They can’t access global variables.

Let's have a glance at some examples of functions-

  1. Program to add two numbers using functions
 def sum(x,y):
     res=x+y
     print("Sum of {} and {} is {}".format(x,y,res))
 x=int(input("Enter the value of x: "))
 y=int(input("Enter the value of y: "))
 sum(x,y)   
Call a Function in Python
  • Program to find cube of a number
 cube=lambda x:x*x*x
 print("Cube is {}".format(cube(9))) 
Call a Function in Python

Related Topics

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Allocate a minimum number of pages in python

You have given a sorted array of size n which represents the number of pages in n different books and an integer value which denotes the number of students. We...

4 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.