Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals Enumerations and Functions.

Similar presentations


Presentation on theme: "Programming Fundamentals Enumerations and Functions."— Presentation transcript:

1 Programming Fundamentals Enumerations and Functions

2 Topics to be covered today Enumerations Functions Passing arguments to Function

3 Enumerations An enum declaration defines the set of all names that will be permissible values of the type. These permissible values are called enumerators. The enum type days_of_week has seven enumerators: Sun, Mon, Tue, and so on, up to Sat.

4 Syntax of enum specifier.

5 Enumerations An enumeration is a list of all possible values. This is unlike the specification of an int, for example, which is given in terms of a range of values. In an enum you must give a specific name to every possible value.

6 Enumerations Enumerations are treated internally as integers. This explains why you can perform arithmetic and relational operations on them. Ordinarily the first name in the list is given the value 0, the next name is given the value 1, and so on. In previous example, the values Sun through Sat are stored as the integer values 0–6.

7 Enumerations Enumerations can hold int value or not Enum days={1,2} Check whether user can input value in enumeration variable

8 Output ???

9 Simple Functions

10 Functions in C++ Experience has shown that the best way to develop and maintain large programs is to construct it from smaller pieces (modules) This technique Called “Divide and Conquer” main() { -----. ---- ----- return 0; } Bad Development Approach Easier To >> Design Build Debug Extend Modify Understand Reuse Wise Development Approach main() { ----- ---- } function f1() { --- } function f2() { --- }

11 Functions in C++(Cont.) In C++ modules Known as Functions & Classes Programs may use new and “prepackaged” or built-in modules New: programmer-defined functions and classes Prepackaged: from the standard library

12 Calling Function Function calls: - Provide function name and arguments (data): Function performs operations and Function returns results

13 Calling Functions Functions calling (Syntax): ( ); E.g., FunctionName( ); or FunctionName(argument1); or FunctionName(argument1, argument2, …);

14 Calling Functions Examples (built-in, and user-defined functions) int n = getPIValue( ) ; //Takes no argument cout << sqrt(9); //Takes one argument, returns square- root cout<<pow(2,3); //Calculates 2 power 3 cout<<SumValues(myArray); //Returns sum of the array A user-defined function

15 Functions A function groups a number of program statements into a unit and gives it a name. This unit can then be invoked from other parts of the program. The most important reason to use functions is to aid in the conceptual organization of a program. Another reason to use functions is to reduce program size.

16 Functions Any sequence of instructions that appears in a program more than once is a candidate for being made into a function. The function’s code is stored in only one place in memory, even though the function is executed many times in the course of the program.

17

18 Output Guess its instructions

19

20

21 Explanation The program consists of two functions: main() and starline(). What other components are necessary to add a function to the program? There are three: the function declaration, the calls to the function, and the function definition.

22 Function Prototype  Before a function is called, it must be declared first.  Functions cannot be defined inside other functions  A function prototype is a function declaration without implementation (the implementation can be given later in the program). int multiplyTwoNums(int,int); A Function prototype (declaration without implementation)

23 Function Prototype (cont.)  Before actual implementation of a function, a function prototype can be used.  Why it is needed?  It is required to declare a function prototype before the function is called.

24 The Function Declaration The declaration tells the compiler that at some later point we plan to present a function called starline. The keyword void specifies that the function has no return value, and the empty parentheses indicate that it takes no arguments. Function declaration is terminated with a semicolon.

25 The Function Declaration Function declarations are also called prototypes. They tell the compiler, “a function that looks like this is coming up later in the program, so it’s all right if you see references to it before you see the function itself.” The information in the declaration (the return type and the number and types of any arguments) is also sometimes referred to as the function signature.

26 Calling the Function The syntax of the call is very similar to that of the declaration, except that the return type is not used. The call is terminated by a semicolon. Executing the call statement causes the function to execute; that is, control is transferred to the function, the statements in the function definition are executed, and then control returns to the statement following the function call.

27

28 Calling the Function In our example function is called (or invoked, or executed) three times from main(). Each of the three calls looks like this: This is all we need to call the function: the function name, followed by parentheses

29 The Function Definition The definition contains the actual code for the function.

30 Function Definition Syntax format for function definition returned-value-type function-name (parameter-list) { Declarations of local variables and Statements; … } Parameter list Comma separated list of arguments  Data type needed for each argument If no arguments  leave blank Return-value-type Data type of result returned (use void if nothing will be returned)

31 The Function Definition The definition consists of a line called the declarator, followed by the function body. The function body is composed of the statements that make up the function, delimited by braces. The declarator must agree with the declaration: It must use the same function name, have the same argument types in the same order (if there are arguments), and have the same return type.

32 The Function Definition When the function is called, control is transferred to the first statement in the function body. The other statements in the function body are then executed and when the closing brace is encountered control returns to the calling program.

33 Comparison with Library Functions Where are the declaration and definition for the library function? E.g., Where are the declaration and definition for this library function? The declaration is in the header file specified at the beginning of the program (CONIO.H, for getche()). The definition (compiled into executable code) is in a library file that’s linked automatically to your program when you build it.

34 Eliminating the Declaration

35 Passing Arguments to Functions

36 An argument is a piece of data (an int value, for example) passed from a program to the function. Arguments allow a function to operate with different values, or even to do different things, depending on the requirements of the program calling it.

37 Passing Constants

38 Passing Variables

39 Passing by Value In previous example, the particular values possessed by chin and nin when the function call is executed will be passed to the function. As it did when constants were passed to it, the function creates new variables to hold the values of these variable arguments. The function gives these new variables the names and data types of the parameters specified in the declarator: ch of type char and n of type int. It initializes these parameters to the values passed. They are then accessed like other variables by statements in the function body.

40

41 Passing by Value Passing arguments in this way, where the function creates copies of the arguments passed to it, is called passing by value.

42 Questions????


Download ppt "Programming Fundamentals Enumerations and Functions."

Similar presentations


Ads by Google