EMAIL SUPPORT

dclessons@dclessons.com

LOCATION

AF

Function() Advance Concepts

Function() Advance Concepts

Pass by Object Reference

In python, the values are passed to function by object references. In python everything such as variable, string, numbers, tuples, lists, and dictionaries are objects. An objects can be treated as a memory block where any values are stored. Object are created on heap memory which depends upon the RAM of machine. This memory is available during run time of any program.

If want to know the location of an object in heap, id () function is used to get the identity number of an object.

Example:  X = 100

Id(X)

Print(X, id(X))

Then it will provide the location of a memory (15487908765) where the value of X is stored.  

Example 1: Write a python program to pass any integer to a function and modify it.

Here you can the output

  • 10 11066176
  • 20 11066496

In this inside function, when value of a is 10 then it has assigned the heap memory address = 11066176 Now when the value of a becomes 20, which is outside function, the memory address of a got changed to 11066496.

Now let’s understand by program to create a new object inside a function and that does not modify outside object.

Example 2: Write a python program to create a new object inside a function and that does not modify outside object.

Formal & Actual Arguments

Formal arguments are those arguments which receives the value from outside the function.

Example: def multiply (a, b)

Actual Arguments are those arguments which provides values to a function

Example multiply (2, 4)

There are four types of actual Arguments stated below:

  • Positional Arguments
  • Keyword Arguments
  • Default Arguments
  • Variable length Arguments

Positional Arguments

These are the arguments which are passed to function in same and correct positional order. This mean that number of arguments and their position in the function definition must match exactly.

Example below shows that a function named attach, which is used to add to string.

def attach (s1, s2)

Now, if we this function will pass two values attach (dclessons, portal). Now output of the function is “dclessons portal”.

Now if we pass the three or four string variable to function attach (), then it will show an error.

Example 3: Write a python program , to learn about positional arguments of a function. 

this by example. Here we are using a function fruits that provide a fruit item and its price and this can be shown as:

def fruit (graps per kilo, price)

And its value is represented as fruit (2, 20)


Comment

    You are will be the first.

LEAVE A COMMENT

Please login here to comment.