1 / 88

Functions

Functions. Function Definition: section of program with special functionalityClassification: built-in functions or pre-packed functions in C/C standard libraryprogrammer-defined functions and user-defined libraryfunctions built-in class libraries. Functions. Each function should be called or i

thu
Download Presentation

Functions

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. Functions Make your code more organized or manageable. Make your code more concise. Make your code more scalable. Make big code to be small pieces with special task. Function can be built in a class as a member.

    2. Functions Function Definition: section of program with special functionality Classification: built-in functions or pre-packed functions in C/C++ standard library programmer-defined functions and user-defined library functions built-in class libraries

    3. Functions Each function should be called or invoked (call to perform) Calling function and called function, depending on their situation or status There must be a communication between the calling function and the called function. Passing information to called function Return information from called function

    4. Functions Return value A called function which may not return a value to the calling function is named a void function

    5. Functions Return value A called function which must return a value to the calling function is named a function with a return value

    6. Functions Passing information to a called function through arguments Passing by value Passing by reference Passing by address

    7. Functions Function Format: In calling function FunctionName (argument1, argument2, ) In called function FunctionName (parameter1, parameter, ) Example:

    8. Functions Common math library functions: ceil(x) cos(x) exp(x) fabs(x) floor(x) fmod(x,y) log(x) log10(x) pow(x,y) sin(x) sqrt(x) tan(x) The variable declared inside function are local variables.

    9. Function definition and usage

    11. Functions Format of a function name(parameter-list)

    15. Functions Prototype Function prototypes Pre-registering function name, return data type, name of parameters and the order in which these parameters are expected. Pre-checking the matches of data communication between calling functions and called functions Forcing conversion between arguments and parameters during information passing

    16. Functions Promotion order (precedence) for built-in data type long double double float unsigned long int long int unsigned int int unsigned short int short int unsigned char short char

    17. Functions Standard library header files (Table, Page 155-157) Programmers can design their custom header file #include "myheader.h"

    18. Standard library Header Files

    19. Functions rand(): A special function in <cstdlib> to generate random number

    30. Function shifting and scaling

    31. Game of chance and enum enum is user-defined type

    38. Identifiers for variable or objects of classe Storage class, scope and linkage C++ provides five storage class specifiers: auto, register, extern, mutable, static It helps determine its storage class, scope and linkage Identifier's Storage Class determines the period during which the identifier exits in memory. Some identifier exit shortly, some repeatedly created and destroyed, and others exit for entire execution.

    39. Functions Storage Class, scope and linkage Scope determines the reference of variables. Some identifier can be referenced in limited portions of program, while others can be references through out whole program. Linkage determines the accessibility of identifier among multiple C++ files

    40. Storage class specifiers: automatic storage static storage Automatic storage class auto and register keywords are used to declare variables of the automatic storage class. Local variables or function parameters are normally of automatic storage class. They are available only within active C++ block. auto is defaulted for local variables in C++. register is used to reverse local variable in register memory, but not common.

    41. Functions extern and static keywords are used to declare variables or function in storage class. extern is used to declare global variables and functions extern is defaulted for global variables and function names in C++ static is used to expand scope for local variables

    42. Functions Example: static variables remain their values, even the function is exited.

    43. Functions Scope: portion pf the program where an identifier has meaning Four scope rules: file scope: function scope, using : (like switch structure) block scope, using { } function-prototype scope

    44. File scope: Declare out side any functions, such as global variables, function definitions, function prototypes Function scope with labels Declare within function block or after labels, such as in switch-case statement, like case (1): Block scope Declare within local block Function prototype scope Ignore the variable name in parameter-list

    51. Functions Function Recursion -- a function can be called by itself. Recursion function is usually a function which can be called by itself repeatedly. Factorial of a non-negative integer, n! is a very typical and simple example n!=n*(n-1)*(n-2)*2*1 = n*(n-1)!. 0!=1

    52. Functions Design an iterative for loop (non-recursively)

    53. Functions Result:

    54. Functions Instead, we can design recursive function for perform the same task. Question: which we should use: either iterative or repetitive structure using for-loop or recursive selection structure using repeatedly calling? Fig 3.17 lists recursive function examples through out the textbook for your reference.

    58. Functions

    62. Recursion and Iteration Recursion uses selection structure Iteration use repetition structure Recursion is easy to implement But cost more memory allocation

    63. Functions

    66. Inline Functions Copy the content of function and insert Applied to a program which call the function more frequently Fast but need more memory

    67. Inline Functions

    69. Functions References and Reference parameters The first method is to pass by value: copy the value and pass over The second method is to pass by reference, i.e., is to make alias variables Use & in front of parameter, like int &count Weak security Example: call-by-reference

    75. Functions Defaulted arguments The defaulted value of the argument can be automatically inserted by the compiler can passed in the called.

    79. Functions

    82. Functions

    85. Functions

More Related