• Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials
      • InfyTQ Archive
      • Infosys Archive
      • TCS Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Tutorials
      • Node.js Tutorial
      • Express.js Tutorial
      • Python Tutorial
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
      • Java Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Tutorials
    • Node.js Tutorial
    • Express.js Tutorial
    • Python Tutorial
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements

Formal and Actual Parameters in Python Function

  • December 11, 2021
  • CODE OF GEEKS
  • 1

Pre-requisite : Functions in Python

In Python, Parameter refers to the information passed to the function. Parameters are also known as arguments.

Typically, Parameters are of two types – Formal Parameters, Actual Parameters

Formal Parameters are the parameters which are specified during the definition of the function.

Consider the following code :

def sum(a, b):
   return a + b

In the above code, ‘a’ & ‘b’ are acting as formal parameters.



Actual Parameters are the parameters which are specified during the function call. Actual Parameters are actually of four types :

  1. Positional Parameters
  2. Keyword Parameters
  3. Default Parameters
  4. Variable length Parameters

Let us explore them one by one

Positional Parameters/ Arguments

Positional Parameters are the parameters that are passed to the function in the correct positional order. For example, consider following code :

def result(name, marks):
    # some cool code
result("Geek", 95)

Above function expects you to provide two parameters during the invocation(function call). During, first parameter should be a string and second parameter should be an integer.

Below code will work and produce output

def result(name, marks):
    print("Name of student : ", name)
    cgpa = marks/10
    print("CGPA is : ", cgpa)

# calling above function with name as "Geek" and marks as 95. 
result("Geek",95)

Output :

Name of student : Geek
CGPA is : 9.5

In the above code, parameter name has the value “Geek” and marks has 95 as value.

Now, let’s do some experiment, let’s swap both parameters

def result(name, marks):
    print("Name of student : ", name)
    cgpa = marks/10
    print("CGPA is : ", cgpa)

# calling above function with name as 95 and marks as "Geek". 
result(95,"Geek")

Output :

TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’

So, position matters.



Keyword Parameters/ Arguments

Positional Parameters are the parameters that are are capable of identifying the parameters with their specified name. For example, consider following code :

def result(name, marks):
    # some cool code
result(name = "Geek", marks = 95)

Above function expects you to provide two parameters during the invocation(function call). In Keyword parameters, maintaining order is not mandatory as we are already specifying the correct parameter with its name.

Both code will work and produce output

def result(name, marks):
    print("Name of student : ", name)
    cgpa = marks/10
    print("CGPA is : ", cgpa)

# calling above function with name as "Geek" and marks as 95. 
result(name = "Geek", marks = 95)

Output :

Name of student : Geek
CGPA is : 9.5

In the above code, parameter name has the value “Geek” and marks has 95 as value.

Now, let’s do some experiment, let’s swap both parameters

def result(name, marks):
    print("Name of student : ", name)
    cgpa = marks/10
    print("CGPA is : ", cgpa)

# calling above function 
result(marks = 95,name = "Geek")

Output :

Name of student : Geek
CGPA is : 9.5



Default Parameters/ Arguments

Default Parameters are the parameters in which we can specify the default value for the parameters in the function definition. For example, consider following code :

def result(name, marks = 95):
    # some cool code
result("Geek")

Default Parameter is a parameter that assumes a default value if a value is not specified during function invocation. Consider the following code :

def result(name, marks = 95):
    print("Name of student : ", name)
    cgpa = marks/10
    print("CGPA is : ", cgpa)

# calling above function 
result("Geek") # Invocation 1 
result("Geek", 90) # Invocation 2

In the above code example, we have called same function twice.

Invocation 1 : result() is invoked with “Geek” as the first parameter. In this case, value for second parameter(marks) is not passed. But, in the function definition, value for variable ‘marks’ is defined as 95.

So, for Invocation 1, output would be :

Name of student : Geek
CGPA is : 9.5

Invocation 2 : result() is invoked with “Geek” as the first parameter and 90 as the second parameter. But, in the function definition, value for variable ‘marks’ is defined as 95.

So, in the case where no value is specified for a parameter, default value is considered or else actual value is given more priority over default value.

So, for Invocation 2, output would be :

Name of student : Geek
CGPA is : 9.0



Variable Length Parameters/ Arguments – *args & **kwargs

There might be the cases where even developer is not clear of the number of values a function may receive. In all such cases, we can not specify the number of parameters in the function definition. To encounter this issue, variable length parameters comes into picture.

def result(farg, *args):
    # some cool code
result(1, "Geek")

farg represents the formal argument and *args represents the variable length parameters. We can pass 1 or more values to *args and it will store them in tuple (stores a group of elements).

Let’s understand this with the help of code example :

def square_of_numbers(farg, *args):
    print("Type of arg is ", type(args))
    print("farg is : ", farg)
    for each_arg in args: 
        print("args is : ", each_arg)
square_of_numbers(1, 5, 10, 15)

O/P :

Type of arg is <class ‘tuple’>
farg is : 1
args is : 5
args is : 10
args is : 15



In the above program, we have 1 formal argument and 3 variable length parameters.

Please note that while invoking a function, formal parameters are mandatory meanwhile it is optional to provide variable length parameters.

**kwargs : In Python, **kwargs represents the keyword variable arguments or parameters. This argument represents the dictionary object. Dictionary in Python stores the data in the form of key-value pairs.

Extra Gyan !

A Dictionary is more like a set of key-value pairs enclosed under ‘{}’.

Example

d = {
      "name": "Apurv", 
      "marks" : "95", 
      "grade" : "A+", 
      "result" : "PASS"
    }
# traversing a dictionary 
for key, val in d.items(): # items() will give pairs of items
        print("Key : {0}, Value : {1}".format(key, val))

O/P

Key : name, Value : Apurv
Key : marks, Value : 95
Key : grade, Value : A+
Key : result, Value : PASS

Note ! Key or value can be of any type.



A keyword variable argument can accept any number of values provided in the form of key-value pairs.

multiply(num1, num2 = 20)

In the above code, num1 is a formal argument and num2 is a keyword variable argument with ‘num2′ as a key and ’20’ as value. Its representation could be imagined as :

{ "num2" : 20 } 

One more example,

multiply(rollno, name = "Apurv", marks = 99, result = "PASS")

In the above code, rollno is a formal parameter and name, marks, & result are keyword variable parameters.

Program : To check the implementation of **kwargs.

def display(rollno, **kwargs):
    for key, val in kwargs.items(): 
        print("{0} : {1}".format(key, val))
display(1, name = 'Apurv', marks = 95, grade = 'A+', result = 'PASS')

O/P :

name : Apurv
marks : 95
grade : A+
result : PASS

Well, that’s all in this lesson.





Tags: **kwargs pythonactual arguments pythonfarg args in pythonfarg pythonformal and actual arguments pythonformal and actual parameters in pythonformal arguemnts [ython
  • Previous Local & Global Variables | Global Keyword in Python
  • Next Introduction to Functions in Python with Example

1 comment on “Formal and Actual Parameters in Python Function”

  1. PRATAP says:
    October 14, 2022 at 1:17 am

    thank’s

    Reply

Leave a Reply Cancel reply

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

Advertise with us

Table of Content – Python

  1. Introduction to Python ▼
    • Python and its features
    • Key Differences : C vs Python
    • Key Differences : Java vs Python
    • Python Flavors
    • PVM and Memory Management in Python

  2. Python Fundamentals ▼
    • 'Hello World' using Python
    • Comments in Python
    • Variables and Garbage Collection in Python
    • Datatypes in Python
    • Determining the Datatype of Literal | type() method
    • Tokens in Python
    • Keywords in Python
    • Identifiers and Naming Convention in Python
    • Literals in Python

  3. Python Operators ▼
    • Operators in Python
    • Python Mathematical Functions

  4. Python User Inputs ▼
    • Taking Inputs | input() function in Python

  5. Python Control Statements ▼
    • Control Statements in Python
    • if-elif-else Statements in Python
    • Looping in Python
    • while loop in Python
    • while-else loop in Python
    • for loop in Python
    • for-else loop in Python
    • Infinite loops in Python
    • Nested loops in Python
    • break Statement in Python
    • continue Statement in Python
    • pass Statement in Python
    • assert Statement in Python
    • return Statement in Python

  6. Python Functions ▼
    • Introduction to Python Functions
    • Python Functions vs Python Methods
    • Local and Global Variables in Python
    • Formal and Actual Arguments in Python
    • Recursion in Python Functions
    • Lambda Functions in Python
    • Function Decorators in Python
    • Function Generators in Python

  7. Python Strings ▼
    • Strings in Python and Basic Operations
    • String Immutability Explained
    • strip() method in Strings
    • find() method in Strings
    • count() method in Strings
    • replace() method in Strings
    • split() method in Strings
    • Switching Cases in Strings
    • startswith() method in Strings
    • endswith() method in Strings

  8. Python Lists ▼
    • Python Lists Basics
    • index() method in list
    • append() method in list
    • insert() method in list
    • copy() method in list
    • remove() method in list
    • pop() method in list
    • sort() method in list
    • reverse() method in list
    • clear() method in list
    • Nested Lists in Python
    • List Comprehensions in Python

  9. Python Sets ▼
    • Python Sets and Basic Operations
    • Adding Elements in Python Sets
    • Removing Elements in Python Sets
    • Union in Python Sets
    • Intersection in Python Sets

  10. Python Tuples ▼
    • Python Tuple Basics
    • Python Tuples Immutability Explained
    • Python Tuples Methods
    • Python Nested Tuples

  11. Python Dictionary ▼
    • Python Dictionary Basics
    • Python Dictionary Sorting
    • Python Dictionary List to Dictionary Conversion
    • Passing Dictionary to Function

  12. Python Classes and Objects ▼
    • Classes and Objects
    • self variable in Python
    • __init__() method in Python
    • Inner Classes
    • Instance variable and methods
    • Class variable and methods

  13. Python OOPs ▼
    • Inheritance and its types
    • super() method in Python
    • Abstract class| Abstract method in Python

  14. Python Exception Handling ▼
    • Errors in Python and its types
    • Exception Handling in Python
    • Types of Exceptions in Python
    • Custom Exceptions in Python

  15. Python File Handling ▼
    • File Handling in Python
    • with statement in Python

  16. Python DateTime Modules ▼
    • datetime module in Python

  17. Python MultiThreading ▼
    • Multithreading in Python

  18. Python Requests Module ▼
    • Python GET Request
    • Python POST Request
    • Python PUT Request

Python Programs

  1. Fibonacci Series till 'N' Numbers

  2. Sum of Digits of a Number

  3. Checking Prime Number

  4. Checking Armstrong Number

  5. Finding Reverse Number (Traditional)

  6. Finding Reverse Number (Pythonic)

  7. Finding Prime Numbers within Range

  8. Finding Prime Numbers within Range Using SOE

  9. Checking Leap year

  10. Calculating Simple Interest

  11. Generate Random Numbers

  12. Calculate Compound Interest

  13. Area of Circle

  14. ASCII Value of Character
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions
Refund Policy
Contact us

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Tutorials

Python
Node.js
Express.js
Golang

Recent Posts

Strings in Go and its basic operations
  • August 11, 2023
Introduction to Functions in Go programming
  • August 11, 2023

Copyright @ CODE OF GEEKS. All Rights Reserved.