Parameter passing in Python

  • Susan Thomson
    Participant

    I am looking for some advice with parameter passing in Python. In the following example, exactly which parameters are the formal parameters and which are the actual parameters?

    Do I include the OUT parameters? or is it only applied to the parameters in brackets?

    # area of rectangle in modules

    def get_inputs():
    length = int(input(“Enter the length : “))
    breadth = int(input(“Enter the breadth : “))
    return length, breadth

    def calculate_area(length, breadth):
    area = length * breadth
    return area

    def display_area(area):
    print(“Area :”,area)

    # main program
    length, breadth = get_inputs()
    area = calculate_area(length, breadth)
    display_area(area)

    Alan
    Participant

    Hi Susan

    I would say that in your example you have all actual parameters, as the same variable names are used throughout.

    If you change the ‘calculate_area’ procedure to something like…

    def calculate_area(L, B):
    result = L * B
    return result

    And leave the rest the same then you will have some more obvious formal parameters e.g. ‘L’, ‘B’ and ‘result’.

    Mr Stagg
    Participant

    Actual – the parameters being passed
    Formal – the ones in the brackets in the function – being formally defined in the function

    thats how i teach it anyhow – its in the formality of the theory where python falls down – vb easy as byref and data types defined in the function line.

    Harry

    Susan Thomson
    Participant

    Many thanks for the reply Alan.

    I am just getting used to Python. Does this also mean that in the calling statements, the length, breadth and area (used in the assignments) are also actual parameters?

    I used Visual Basic before and the actual parameters were more obvious.

    Many thanks in advance

    Peter Thoresen
    Participant

    Algorithms will use in/out/in-out to show the flow of data – but this is for design only.

    Formal and Actual parameters only apply in the implementation.

    Your “getinput” function does have data coming out, but it has NO parameters.

    Your “calculate_area” function takes in two items of data, and sends out one item – but it has 2 formal parameters – length and width.

    “display_area” has one formal parameter.

    The actual parameters are in the main program.

    length, breadth = get_inputs() has no parameters.

    area = calculate_area(length, breadth) has two actual parameters (length and breadth) corresponding to the formal parameters in the function (also called length and breadth).

    David Muir
    Participant

    I can’t add to Peter’s reply as he explains what’s what very clearly.

    In terms of teaching parameter passing though, in any programming examples I show pupils, I always use different variable names for the actual parameters and the formal parameters to make it easier to refer to them when talking to the pupils. So, I might define the sub-program as –
    def calculate_area(f_length, f_breadth):
    and/or call the sub-program in the main program with –
    area = calculate_area(l, b)

    I don’t know if it helps, but the other thing I do is tell pupils:
    Formal parameters in the sub-program deFinition
    Actual parameters in the sub-program cAll

    Susan Thomson
    Participant

    This has been a good help.

    Many thanks to everyone who contributed.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.