Formal and Actual Parameters in a Function in Python

Formal and actual parameters in a function in python

A formal parameter, i.e. a parameter, is in the function definition. An actual parameter, i.e. an argument, is in a function call.

So n here:

def factorial(n):

Is a formal parameter.

And n - 1 (or rather, the value it evaluates to) here:

   return n * factorial(n-1)

Is an "actual parameter", i.e. an argument.

Python: How to assign a value as an actual parameter when not using numbers?

You don't necessarily need a formal parameter in func1().

def func1():
infile = open("october.txt", "r")
process_contents = infile.read()
return process_contents

def func2():
message = func1()
print("asterisks" + message + "box thingy")

Although, you can use the formal parameter to generalize the function like this:

FILENAME = "october.txt" # or rather something like sys.argv[1]

def func1(FILENAME):
infile = open(FILENAME, "r")
process_contents = infile.read()
return process_contents

def func2():
message = func1(FILENAME)
print("asterisks" + message + "box thingy")

Actual parameters in Python lambda expressions

lambda pair: pair[1] is exactly equivalent to a function definition like the following:

def lambda_equivalent (pair):
return pair[1]

So pair is the parameter the anonymous lambda function accepts. It only has that one parameter.

The function is then passed as the argument to the key parameter of the list.sort function. So it again would be equivalent to the following:

pairs.sort(key=lambda_equivalent)

What list.sort then does is call that key function for every item to determine the actual value list items should be sorted by. So list.sort will eventually call key((1, 'one')) and key((2, 'two')), etc., so the list item—the tuple—is the parameter that is passed to the key function. In case of your lambda, this would be pair.

How do I differentiate parameters and arguments in python?

Yes, your understanding of both argument and parameter are correct.
So if you look at question 10, the data is passed into the function 'min'. that is why it is an argument.

As for question 8, data is a parameter of the function 'f'. When you want to call function 'f' in your program, you will need to provide an argument for the function.

Example code:

def hello(name):     # name is a parameter 
return name

myName = 'jack'
hello(myName) # myName is an argument for function 'hello'

instance variable and data attribute

class Coordinate (object):
def __init__ (self, x, y):
self.x = x
self.y = y

c = Coordinate (3,4)
print (c.x)

In the above code :

  1. self.x and self.y are attributes of the class

  2. c.x and c.y are the instance variables. All the instance variables have different values for different instances.

  3. Actual parameters are the parameters which you specify when you call the Functions. A formal parameter is a parameter which you specify when you define the function. The actual parameters are passed by the calling function. The formal parameters are in the called function. Thus (3,4) here are the actual parameters, whereas the variables used in the function definition which later will hold the values of the actual parameters are termed as formal parameters. Here self.x and self.y are formal parameters.

Why I use list as a parameter of a function and the function can't change the value of the actual parameter but only the formal parameter?

I think your issue is that you should be adding to the values in the list, but instead you're reassigning. Change = to +=:

def twomerger(lx, ly):
if lx[0] == ly[0]:
lx[0] += ly[0]
ly[0] = 0
if lx[1] == ly[1]:
lx[1] += ly[1]
ly[1] = 0
if lx[2] == ly[2]:
lx[2] += ly[2]
ly[2] = 0
if lx[3] == ly[3]:
lx[3] += ly[3]
ly[3] = 0

row1 = [0, 2, 4, 4]
row2 = [2, 2, 4, 4]
twomerger(row1, row2)
print (row1)
print (row2)

Output:

[0, 4, 8, 8]
[2, 0, 0, 0]

For what it's worth, your method can be written much more concisely:

def twomerger(lx, ly):
for i, num in enumerate(lx):
if lx[i] == ly[i]:
lx[i] += ly[i]
ly[i] = 0

Or if you're comfortable with zip:

def twomerger(lx, ly):
for i, (a, b) in enumerate(zip(lx, ly)):
if a == b:
lx[i] += b
ly[i] = 0

Formal parameter of the form **keyword

If you use a dictionary, calling a function would look like this:

myfunv({"key1": "value1", "key2": "value2"})

Using **kwargs this becomes

myfunc(key1="value1", key2="value2")

Many people prefer the latter.

This construct is also very useful when you are transparently passing arguments to another function or class. For example, I use this a lot when subclassing Tkinter widgets. I want to support all of the options of the base class, so I code it like this:

class MyFrame(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
<more code here>

This allows me to support all the same arguments without having to explicitly list them all.

Can giving same variable names to actual and formal parameters in C++ create any problems?

params

If you are talking about stuff in picture, simple answer is it does not matter at all. In "pass by value" case, value of caller function is copied to the address of parameter of called function.

When it comes to references, here is one quote:

A reference shares the same memory address with the original variable but also takes up some space on the stack whereas a pointer has its own memory address and size on the stack.

So to simplify the answer, parameter and local variable names do not matter much because compiler converts them into addresses. As seen from above, even reference has to store the address of referred variable.

My suggestion is read this. And think of variables or parameters in term of addresses, not in terms of their names.

Difference between parameter and argument

Argument is often used in the sense of actual argument vs. formal parameter.

The formal parameter is what is given in the function declaration/definition/prototype, while the actual argument is what is passed when calling the function — an instance of a formal parameter, if you will.

That being said, they are often used interchangeably, their exact use depending on different programming languages and their communities. For example, I have also heard actual parameter etc.

So here, x and y would be formal parameters:

int foo(int x, int y) {
...
}

Whereas here, in the function call, 5 and z are the actual arguments:

foo(5, z);


Related Topics



Leave a reply



Submit