1 / 25

CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)

Learn about function prototypes, scope, and function definition in structured programming language, with examples and header files.

higuera
Download Presentation

CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)

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. CSI-121Structured Programming LanguageLecture 14Functions (Part 2)

  2. Topics • Prototypes • Scope

  3. Prototyping of Functions • Must declare functions before use (like variables) • Declaration is called a “prototype” • Specifies the name, parameters and return type of the function, but not the code

  4. #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Example: isNegative.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; }

  5. Function Prototype #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Example: isNegative.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; }

  6. #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Example: isNegative.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Function Definition

  7. #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Example: isNegative.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Function Call (Must be after prototype, but can be before definition)

  8. #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Example: isNegative.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Header files (filename.h) contain function prototypes and global variable declarations

  9. #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Example: isNegative.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } stdio.h contains function prototypes for printf(), scanf(), and other I/O functions

  10. Header files • You can make your own header files with prototypes of frequently used functions: #include "myFunctions.h" • Put the functions in a corresponding C file, and include those too: #include "myFunctions.c"

  11. #include <stdio.h> #include "myFunctions.h" #include "myFunctions.c" int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Example: isNegative.c • Note: • " "around file name for user-defined files • < > for standard system files isNegative() is declared in myFunctions.h and defined in myFunctions.c,not in this file!

  12. Example: myFunctions.h int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Example: myFunctions.c int isNegative ( int );

  13. Scope Where can you use a variable which is declared in a function? • In that function only • Not in a calling function • Not in a called function

  14. Scope: Local Variables • Formal parameters: only accessible whilst function executing • Variables declared in a function body: only accessible whilst function executing • In fact, this is true of every block in a program

  15. #include <stdio.h> int isNegative ( int n ) { int result; if (number<0) { result=1; } else { result = 0; } return result; } Example: isNegative.c int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; }

  16. #include <stdio.h> int isNegative ( int n ) { int result; if (number<0) { result=1; } else { result = 0; } return result; } Example: isNegative.c int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } ERROR! Number is local to the main function, not accessible here

  17. #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Example: isNegative.c int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } Use the parameter n which is local to the function isNegative()

  18. #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Example: isNegative.c int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } n is the formal parameter number is the actual parameter

  19. #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Example: isNegative.c int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } result & n: local to isNegative() number:local to main()

  20. #include <stdio.h> int number; int isNegative ( void ) { int result; if ( number <0 ) { result=1; } else { result = 0; } return result; } Example: isNegativeGlobal.c int main (void) { printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative()) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; }

  21. #include <stdio.h> int number; int isNegative ( void ) { int result; if ( number <0 ) { result=1; } else { result = 0; } return result; } Example: isNegativeGlobal.c int main (void) { printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative()) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } number is now GLOBAL - declared outside any function, accessible in all functions (after the declaration)

  22. Scope: Global Variables • Global variables are accessible in any function after their declaration to the end of that source file • They're useful, but risky • if any and every function can modify them, it can be difficult to keep track of their value • Better to use local variables and parameter passing if possible

  23. Scope: Functions • Functions are also accessible in any function after their declaration to the end of that source file

  24. Summary • Include function prototypebefore its use • Be careful about the scope of variables

  25. Readings • King: 9.1 – 9.4 • D&D: 5.6 – 8.5, 5.12 • Kernighan & Ritchie: 4.1, 4.2, 4.4, 4.11

More Related