Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Conclusion CIS 61.

Similar presentations


Presentation on theme: "Chapter 5 Conclusion CIS 61."— Presentation transcript:

1 Chapter 5 Conclusion CIS 61

2 Recursion The existence of functions makes possible a programming technique called recursion. Recursion involves a function calling itself. This might sound like a bug to you and something not useful but when used correctly, this technique is extremely powerful.

3 Recursion Recursion is much easier to understand with an example than with long explanations so let’s look at an example. We are going to look at a program that calculates factorials. That is 3! = 3*2*1. Let’s look at “factor2.cpp”.

4 Recursion Is it true that many versions of a recursive function are stored in memory while it’s calling itself? Not really. Each version’s variables are stored, but there’s only one copy of the function’s code. Even so, a deeply nested recursion can create a great many stored values, which can pose a problem to the system if it doesn’t have enough space for them.

5 Inline Functions We mentioned that functions save memory space because all the calls to the function cause the same code to be executed; the function body need not be duplicated in memory. When the compiler sees a function call, it normally jumps to the function. At the end of the function it jumps back to the instruction following the call.

6 Inline Functions While this sequence of events may save memory space, it takes some extra time. There must be an instruction for the jump to the function, instructions for saving registers, instructions for pushing arguments onto the stack in the calling program and removing them from the stack in the function, instructions for restoring registers, and an instruction to return to the calling program.

7 Inline Functions The return value must also be dealt with.
In other words, in the background there are many things that the compiler is doing that you don’t see. All these extra instructions slow down the program.

8 Inline Functions To save execution time in short functions, you may try and put this code in the function body directly inline with the code in the calling program. In other words, each time there’s a function call in the source file, the actual code from the function is inserted, instead of a jump to the function.

9 Inline Functions Long sections of code are generally better off as normal functions: the savings in memory space is worth the small sacrifice in execution speed. But making a short section of code into an ordinary function may result in little savings in memory space, while having just as much of a time penalty as a larger function.

10 Inline Functions If a function is very short, the instructions necessary to call it may take up as much space as the instructions within the function body, so that there is not only a time penalty but a space penalty as well. In this case you can just repeat the necessary code in your program, inserting the same group of statements wherever it was needed.

11 Inline Functions The trouble with repeatedly inserting the same code is that you lose the benefits of program organization and clarity that come with using functions. The program may run faster and take less space, but the listing is longer and more complex.

12 Inline Functions The solution to this problem is the inline function.
This kind of function is written like a normal function in the source file but compiles into inline code instead of into a function. The source file remains well organized and easy to read, since the function is shown as a separate entity. However when the program is compiled, the function body is actually inserted into the program wherever a function call occurs.

13 Inline Functions Functions that are very short, maybe one or two statements are good candidates to be inlined. Let’s look at “inliner.cpp” that is a variation of the program that converts weight in kilograms to pounds.

14 Inline Functions You should know that the inline keyword is actually just a request to the compiler. Sometimes the compiler will ignore the request and compile the function as a normal function. It might decide that the function is too long to be inline, as an example.

15 Default Arguments A function can be called without specifying all its arguments. This doesn’t work on just any function. The function declaration must provide default values for those arguments that are not specified.

16 Default Arguments Let’s look at an example of this. missarg.cpp

17 Default Arguments Remember that the missing arguments must be the trailing arguments. The compiler will flag an error if you leave out arguments for which the function does not provide default values. Default arguments are useful in cases where, after a program is written, the programmer decides to increase the capability of a function by adding another argument.

18 Scope and Storage Class
The scope of a variable determines which parts of the program can access it. The storage class determines how long a variable stays in existence. There are 2 different kinds of scope, local and file.

19 Scope and Storage Class
Variables with local scope are visible only within a block. Variables with file scope are visible throughout a file. A block is the code between an opening brace and closing brace { }.

20 Scope and Storage Class
There are 2 storage classes, automatic and static. Variables with storage class automatic exist during the lifetime of the function in which they’re defined. Variables with storage class static exist for the lifetime of the program.

21 Local Variables Almost all variables that we have used are local variables. void somefunc() { int somevar; // other statements }

22 Storage Class A local variable is not created until the function in which it is defined is called. The period of time between a variable creation and destruction is called its lifetime or duration. The idea behind limiting the lifetime of variables is to save memory space.

23 Scope A variables scope, also called visibility, is the locations within a program from which the variable can be accessed. Variables defined in a function can only be accessed in that function. Limiting visibility helps to keep one function from accidentally altering variables it shouldn’t change.

24 Global Variables Global variables are defined outside of any function.
A global variable is visible to all the functions in a file. A global variable is initialized when they are created by the compiler. Lets look at an example “extern.cpp”.

25 Lifetime and Visibility
Global variables have a storage class static, which means they exist for the life of the program. Global variables are automatically declared as static. Global variables are visible in the file they are defined, starting at the point where they are defined.


Download ppt "Chapter 5 Conclusion CIS 61."

Similar presentations


Ads by Google