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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

C Language.
Spring Semester 2013 Lecture 5
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CPS120: Introduction to Computer Science Lecture 14 Functions.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
C Language By Sra Sontisirikit
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Scope, Parameter Passing, Storage Specifiers
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
A function with one argument
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
The Function Prototype
Functions, Part 1 of 3 Topics Using Predefined Functions
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Introduction to Computing Lecture 09: Functions (Part II)
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Scope Rules.
Presentation transcript:

CSI-121 Structured Programming Language Lecture 14 Functions (Part 2) CSE1301 Sem 2, 2003 CSI-121 Structured Programming Language Lecture 14 Functions (Part 2) Lecture 14: Functions Part 2

Topics Prototypes Scope CSE1301 Sem 2, 2003 Lecture 14: Functions Part 2

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

Example: isNegative.c #include <stdio.h> int 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; int isNegative ( int n ) { int result; if ( n<0 ) result=1; } else result = 0; return result;

Example: isNegative.c Function Prototype #include <stdio.h> int 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; int isNegative ( int n ) { int result; if ( n<0 ) result=1; } else result = 0; return result;

Example: isNegative.c Function Definition #include <stdio.h> int 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; int isNegative ( int n ) { int result; if ( n<0 ) result=1; } else result = 0; return result; Function Definition

(Must be after prototype, but can be before definition) Example: isNegative.c #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; 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)

Example: isNegative.c #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; 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

Example: isNegative.c #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; 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

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"

Example: isNegative.c Note: #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; 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!

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

Where can you use a variable which is declared in a function? 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

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

Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if (number<0) result=1; } else result = 0; return result; 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 Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if (number<0) result=1; } else result = 0; return result; 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

Use the parameter n which is local to the function isNegative() Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) result=1; } else result = 0; return result; 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()

Example: isNegative.c n is the formal parameter #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) result=1; } else result = 0; return result; 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

Example: isNegative.c result & n: local to isNegative() #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) result=1; } else result = 0; return result; 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()

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

Example: isNegativeGlobal.c #include <stdio.h> int number; int isNegative ( void ) { int result; if ( number <0 ) result=1; } else result = 0; return result; 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)

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

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

Summary Include function prototype before its use Be careful about the scope of variables

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