Procedure calls, Python Programming

Procedure calls

When you compute an expression of the form

(,  ..., )

 

the Python interpreter treats  this as a procedure call. It will be simpler to talk about  a speci?c case of invoking a function, so we will explain with the example

 

>>> square(a + 3)

 

computed in environment E1. Here are the steps:

 

1.  The expression that determines the procedure () is computed. In this case, we compute

square in E1 and get Procedure1.

 

2.  The expressions that  determine the arguments (, ..., )  are computed. In this case, we compute a + 3 in E1 and get 5.

 

3.  A new environment (in this case E2) is prepared, which:

-  binds  the formal  parameters of the function (in this case x) to the values  of its arguments(in this case, 5); and

-  has as its base the environment in which  the procedure was de?ned we ?nd  a pointer .

 

1843_Procedure calls.png

 

The dotted line between E1 and  E2 is used to show that  E1 is the parent environmentof E2.

 

4.  The statements of the  procedure body  are computed in the  new  environment until  either  a return statement or the end of the list of statements is got. If a result expression is calculated, the expression after the return is computed and its value is returned as the value of the function call expression. Otherwise, the function has no return value,  and  the expression has the unique Python value  None.

 

In our  example, the  only  statement in the  body  is a return statement.  So, we  compute the expression x * x in E2, obtaining a value  of 25.  That  value  is returned as the value  of the entire  procedure-call expression square(a + 3).

 

This basic mechanism can generate behavior of arbitrary hardness, when grouped with  recursion or other control  structures.

 

 

 

 

Posted Date: 8/11/2012 2:04:23 AM | Location : United States







Related Discussions:- Procedure calls, Assignment Help, Ask Question on Procedure calls, Get Answer, Expert's Help, Procedure calls Discussions

Write discussion on Procedure calls
Your posts are moderated
Related Questions
inputWithinRange (prompt, lowValue, highValue) This function will ask for a number using the prompt parameter. The function will only return a value that is within the inclusive ra

Coding examples Following are some  attempts at defining a function isSubset,  which  takes  two  arguments, a and  b, and  returns True if a is a subset  of b, assuming that

Write a program that asks the user to enter a number of seconds. The responses of the program will vary depending on the length of seconds: • If the number of seconds is under 60

Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

The goal of this exercise is to write a \business phrase" generator, which each time it is called produces lines of business speak, such as: It's time that we became uber-effici

Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl

Variables We cannot  go very far without variables. A variable is a value related to a name that we can bind  to have a particular value  and  then  later use in an expression.

simple program using list

For this assignment, you are to: 1.    Modify the output format of your Python script from Assignment 1 to match the input format of your C program from Assignment 2. 2.    W

What if you had a list of integer values, and you need to add  them  up and give the sum?  Here are a number of different types of doing  it. First,  here  is a type in a style