Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-10-functions2

Avatar for William Albritton William Albritton
September 23, 2015
230

 ics212-10-functions2

Avatar for William Albritton

William Albritton

September 23, 2015
Tweet

Transcript

  1. Function-like Macros  A macro is an identifier defined in

    a #define preprocessor directive  Function-like macros look similar to functions, but they are actually text that is replacement before the program is compiled
  2. Macro Example  This macro calculates the area of a

    circle #define PI 3.14159265358979323846 #define CIRCLE_AREA(R) PI*R*R int main(){ double radius = 10.0; double area = CIRCLE_AREA(radius);
  3. Another Macro Example  This macro finds the max of

    two numbers #define MAX(A,B) A>B?A:B int main(){ int x = 10; int y = 20; printf(“%d\n”, MAX(x,y)); //20
  4. Benefits of Modularity  By separating a solution to a

    problem in smaller shorter modules makes each module easier to understand  The overall program will be easier to understand as well
  5. Benefits of Modularity  Modularity may reduce the size of

    a program by eliminating duplicate code  Several programmers may work in one program at once
  6. Benefits of Modularity  Each module may be written and

    tested independently  Modules are smaller so they are easier to test  Modules may be reused in other programs
  7. Divide and Conquer  Modules can be used to break

    down an algorithm written in C code into smaller pieces Problems are getting bigger and bigger
  8. Abstraction  Modules can be referenced and used without the

    need of knowing how these modules work internally
  9. The main Function  Execution of a C program always

    begins in the main function  Additional functions are invoked from the main function
  10. return-type function-name(parameters){ //body of the function //do not forget to

    return a value if not void return expression; } Function Definition
  11. Return Type void  A void function with return type

    void does not return a value  void as a parameter means that there are no parameters void fun(void){ statements; . . . return; }
  12. Return Statement for void  The return statement in a

    void function does not contain an expression and has the form: return; void fun(void){ statements; . . . return; }
  13. Function Prototype  The function prototype for each function used

    in a program must be declared at the top of the program above the main() function return-type function-name(parameters); int add(int, int);
  14. result = add(num1, num2); … int add(int x, int y){

    int w = 0; w = x + y; return w; } Actual Parameters (Arguments)  Parameters that are sent to the function in the function call
  15. result = add(num1, num2); … int add(int x, int y){

    int w = 0; w = x + y; return w; } Formal Parameters  Parameters that are received by the function in the function definition
  16. Pass by Value  Copy of argument’s value is passed

    to function  Changes in the copy do not affect the original variable (no “side effects”)  All calls are “call by value” in C
  17. Pass by Reference  Can modify the argument’s value 

    Simulated in C by using pointers  Make a copy of address & modify the contents  Used in C arrays & pointers  We will cover pass by reference later in the semester… Debit Card
  18. Coercion of Arguments  When there is a mismatch between

    the actual parameters and the formal parameters, the actual parameters will be converted to match the formal parameters  See example code at coercion.c, where doubles are converted into integers
  19. Watch Out!  If a return type or parameter is

    not listed, then the compiler will assume it is of type "int"  The return type and the argument types of the function prototype & the function definition must be the same  For example, see bugs.c
  20. Scope  Portion of a program where a variable or

    function can be referenced 1. Function 2. File 3. Block 4. Function-prototype
  21. Function Scope  Only labels have function scope  Identifier

    followed by a colon  May be used anywhere within a function
  22. Function Scope  For example, labels are used in switch

    statements switch(ch){ case ‘A’: case ‘a’: a++; break; . . .}
  23. File Scope  Identifiers declared outside any function  Known

    from declaration to end of file  Examples:  Global variables  Function prototypes
  24. Block Scope  Identifiers declared within a block  Only

    visible inside the block  Variables declared inside a block may hide any identically named variables in outer blocks  Will remain in existence until the matching right brace
  25. #include <stdio.h> int main(void){ int x = 10; { int

    x = 20; { int x = 30; printf(“x=%d\n”,x); } printf(“x=%d\n”,x); } printf(“x=%d\n”,x); return 0; }
  26. Macro Scope  Directive #undef “undefines” a symbolic constant or

    macro name  The scope of a symbolic constant or macro is from its definition until it’s undefined with #undef, or until the end of the file  Once undefined, a name can be redefined with #define
  27. Storage Classes  A variable’s storage class determines its storage

    duration, scope, and linkage  C has four storage classes  auto  register  extern  static
  28. Storage Classes  Storage duration (auto & static)  Period

    it exists in memory  Scope (file, function & block)  Where it can be referenced in a program  Linkage (internal & external)  Whether a variable is accessed only in current source file or in other source files
  29. auto  Variable created when block is entered  Variable

    exists while block is active  Variable destroyed when block is exited  Default for all variables inside functions int main(void){ auto int x = 100; }
  30. register  Put variable in high-speed register  Compiler is

    free to ignore it  It has automatic storage int main(void){ register int count = 0; }
  31. extern  Exists from start of program  Referenced by

    any function that follows its declaration  Has static storage duration  Default for global variables and functions  Don’t have to explicitly type “extern,” unless shared by multiple files
  32. extern  Variable global1 is extern by default #include <stdio.h>

    int global1 = 1; void fun(void); int main(void){. . .
  33. static  Declared in a function & retains its value

    after the function has finished executing  Next time the function is called, the variable still has the same value  Also has static storage duration
  34. Storage Classes Summary  extern (global variable)  Exists for

    the life of the program  Use anywhere in the program  static  Exists for the life of the program  Only use inside the function where it is declared  auto, register (local variable)  Exists for the life of the function  Only use inside the function where it is declared