Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.

Similar presentations


Presentation on theme: "Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor."— Presentation transcript:

1 Programming Fundamentals

2 Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor

3 Recursion Recursion involves a function calling itself.

4 Inline Function By using functions we may save memory space but it takes some extra time. Like there must be an instruction for the jump to the function (actually the assembly-language instruction CALL or something similar), instructions for saving registers, instructions for pushing arguments onto the stack in the calling program and removing them from the stack in the function (if there are arguments), instructions for restoring registers, and an instruction to return to the calling program. The return value (if any) must also be dealt with. All these instructions slow down the program.

5 Inline Function To save execution time in short functions, you may elect to put the code in the function body directly inline with the code in the calling program. That is, 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.

6 Inline Function

7

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

9

10 Scope and Storage Class The scope of a variable determines which parts of the program can access it, and its storage class determines how long it stays in existence. Two different kinds of scope are important here: local and file. Variables with local scope are visible only within a block. Variables with file scope are visible throughout a file. A block is basically the code between an opening brace and a closing brace. Thus a function body is a block.

11 Scope and Storage Class There are two 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.

12 Local Variable- Storage Class A local variable is not created until the function in which it is defined is called. More accurately, we can say that variables defined within any block of code are not created until the block is executed. Thus variables defined within a loop body only exist while the loop is executing. Variables are automatically created when a function is called and automatically destroyed when it returns.

13 Local Variable- Scope Variables defined within a function are only visible, meaning they can only be accessed, from within the function in which they are defined.

14 Global Variables While local variables are defined within functions, global variables are defined outside of any function. A global variable is visible to all the functions that follow the variable’s definition in the listing. Usually you want global variables to be visible to all functions, so you put their declarations at the beginning of the listing.

15 Global Variables Global variables are also sometimes called external variables, since they are defined external to any function. A global variable is used when it must be accessible to more than one function in a program. Global variables have storage class static, which means they exist for the life of the program.

16 Global Variables Memory space is set aside for them when the program begins, and continues to exist until the program ends. You don’t need to use the keyword static when declaring global variables; they are given this storage class automatically. Global variables are visible in the file in which they are defined, starting at the point where they are defined.

17 Static Local Variables A static local variable has the visibility of an automatic local variable (that is, inside the function containing it). However, its lifetime is the same as that of a global variable, except that it doesn’t come into existence until the first call to the function containing it. Thereafter it remains in existence for the life of the program.

18 Static Local Variables Static local variables are used when it’s necessary for a function to remember a value when it is not being executed; that is, between calls to the function.

19

20 Check Output for this code

21 Returning by Reference

22 Important points to remember You can’t return a constant from a function that returns by reference. If you try this the compiler will complain that you need an lvalue, that is, something that can go on the left side of the equal sign: a variable and not a constant.

23 Important points to remember You can’t return a reference to a local variable. The problem is that a function’s local variables are probably destroyed when the function returns, and it doesn’t make sense to return a reference to something that no longer exists.

24 const Function Arguments Suppose you want to pass an argument by reference for efficiency, but not only do you want the function not to modify it, you want a guarantee that the function cannot modify it. To obtain such a guarantee, you can apply the const modifier to the variable in the function declaration.

25 Compilation Error

26 Is this assignment ok??

27 A Simple Class Structures provide a way to group data elements. Functions organize program actions into named entities. We will put these ideas together to create classes.

28

29 Classes and Objects Placing data and functions together into a single entity is a central idea in object oriented programming. An object has the same relationship to a class that a variable has to a data type. An object is said to be an instance of a class, in the same way my 1954 Chevrolet is an instance of a vehicle.

30 Syntax of a class definition

31

32 Constructors The previous example shows two ways that member functions can be used to give values to the data items in an object. Sometimes, however, it’s convenient if an object can initialize itself when it’s first created, without requiring a separate call to a member function. Automatic initialization is carried out using a special member function called a constructor.

33 Constructors A constructor is a member function that is executed automatically whenever an object is created. A constructor has no return type but can take arguments. Constructors can be overloaded, so an object can be initialized in different ways.

34

35 Initializer List

36 OUTPUT???

37 Destructors Constructor is called automatically when an object is first created. You might guess that another function is called automatically when an object is destroyed. Such a function is called a destructor. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde:

38 Destructors Like constructors, destructors do not have a return value. They also take no arguments.

39

40 Questions????


Download ppt "Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor."

Similar presentations


Ads by Google