Passing arguments to called program in Karel

  • I'm trying to display a yes/no menu from Karel. From TP, it would look like this:


    CALL MHPRMPTY(2,37)


    I can call MHPRMPTY from Karel using CALL_PROG but I can't figure out how to pass the arguments and I can't find anything about it in the karel reference manual. Any help would be appreciated. Thanks.

  • Place your Ad here!
  • This will work. I have used this in the past.

    +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+

    MI Robot Guy

  • This will work. I have used this in the past.

    that' great!:smiling_face:

  • Below I show you how to pass arguments from Karel to Karel, not sure if you can pass arguments from Karel to TP.


    In Karel you cannot pass arguments from one Karel program to another Karel program exactly like you do in a TP program eg CALL GET_AREA(2,5);


    The answer is to use the ROUTINE function. This function is surprisingly well explained in the Karel reference manual.


    The examples below show the basic concept:


    Program MATH_LIB

    ROUTINE GET_AREA(length: REAL; width: REAL; area: REAL)

    BEGIN

    area = length * width

    END GET_AREA


    BEGIN

    --Main body of program.

    -- Can be empty

    END MATH_LIB



    Program TEST

    VAR

    area: REAL

    --declare the routine like it is a variable

    ROUTINE GET_AREA(length: REAL; width: REAL; area: REAL) FROM MATH_LIB


    BEGIN

    GET_AREA(2,2,area) -- pass 'area' by reference

    WRITE(’Area = ’, area, CR) -- Print: Area = 4

    END TEST


    Note you can also make the ROUTINE return a value, instead of passing a variable by reference. eg.


    ROUTINE GET_AREA(length: REAL; width: REAL) : REAL

    VAR area: REAL

    BEGIN

    area = length * width

    RETURN (area)

    RETURN (length * width) -- Maybe this works too

    END GET_AREA


    Program TEST2

    VAR

    area: REAL

    ROUTINE GET_AREA(length: REAL; width: REAL) : REAL FROM MATH_LIB


    BEGIN

    area = GET_AREA(2,2) -- Returns a number

    WRITE(’Area = ’, area, CR) -- Print: Area = 4

    END TEST2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account
Sign up for a new account in our community. It's easy!
Register a new account
Sign in
Already have an account? Sign in here.
Sign in Now

Advertising from our partners