1 / 42

Programming Fundamentals

Programming Fundamentals. Enumerations and Functions. Topics to be covered today. Enumerations Functions Passing arguments to Function. Enumerations. An enum declaration defines the set of all names that will be permissible values of the type.

grantk
Download Presentation

Programming Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ProgrammingFundamentals 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 enumerationis 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. Wise Development Approach main() { ----- ---- } function f1() { --- --- } function f2() { --- --- } Bad Development Approach main() { ----- ----- ----- ----- . . . ---- ----- ----- return 0; } 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” • Easier To >> • Design • Build • Debug • Extend • Modify • Understand • Reuse

  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): • <function name> (<argument list>); • 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 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. Output Guess its instructions

  18. 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 callsto the function, and the function definition.

  19. 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)

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. 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

  25. The Function Definition • The definition contains the actual code for the function.

  26. Function Definition • Syntax format for function definition returned-value-typefunction-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)

  27. 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.

  28. 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.

  29. 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.

  30. Eliminating the Declaration

  31. Passing Arguments to Functions

  32. Passing Arguments to Functions • 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.

  33. Passing Constants

  34. Passing Variables

  35. 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.

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

  37. Questions????

More Related