1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing.
1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
1 Chapter 4 (II) Functions and Structured Programming.
Chapter 6: User-Defined Functions I
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Function. Outline Intro. Functions Examples of Functions Prototypes of a Functions Local and Global Variables.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions Dilshad M. Shahid New York
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
Introduction to C++ Systems Programming.
Suppose we want to print out the word MISSISSIPPI in big letters.
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions.
User-Defined Functions
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
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Problem Solving and Programming
Department of Statistics St. Joseph’s College (Autonomous)
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.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter 4 Section 1 to 12

2 Program Components  A C program is made up of one or more functions, one of them being main() which is the starting point of program execution  Typically, some functions are defined by the programmer and the others are predefined functions provided in the C standard libraries or other libraries.

3 The C standard libraries  provide functions for:  Input/output, math calculation, character/string processing, error-checking …  Related library functions are grouped and can be referenced by including the corresponding header file into the source program during preprocessing  Header files for ANSI C’s standard library

4 Functions  Why functions?  Natural for top-down design approach  Enhance software reusability  Avoid repeating code main worker3worker2worker1 worker3worker4

5 Function Invocation  When a function is called (invoked), program control is passed to that function.  When that function ends, program control is returned to the statement immediately after the function call.  Syntax: function_name ( argument_list )

6 Calling Predefined Functions #include void main() { float area; printf(“Enter the area of a square: “); scanf(“%f”, &area); printf(“The square has perimeter: %f”, 4*sqrt(area)); }

7 Another example #include int main() { int i=0, n; printf(“No. of random integers you want to see? ”); scanf(“%d”, &n); while (i < n) { if (i++ % 6 == 0) /* 6 numbers (max.) per line */ printf("\n"); printf(“%9d”, rand()); /* call rand() in stdlib */ } printf(“\n”); return 0; }

8 Programmer-defined functions  You can define your own functions and then call them as needed.  Either the function definition or function prototype must appear before the function calls.  Function prototype describes how the function is invoked (interface)  Function definition describes how the function computes the return value (implementation)  A function definition cannot be nested within another function definition.

9 Function Definition  Syntax: type function_name( parameter_list ) { sequence_of_statements }  Everything before the ‘ { ’ comprises the function header; the rest constitutes the function body

10 Example #include int powerOfTwo(int n) { int power=1; for (; n>0; n--) power *= 2; return power; } void main() { int i, result; printf(“Enter a non-negative integer: “); scanf(“%d”, &i); result = powerofTwo(i); printf(“The %d-th power of 2 is %d\n”, i, result); return; }

11 Invocation & Call-by-Value  When a C function is invoked, the arguments within the parentheses are passed using a call-by- value mechanism, which means that each argument is evaluated, and its value is used locally in place of the corresponding formal parameter  Example (previous page):  Suppose user input 7 for the value of i. The argument received by function powerOfTwo is 7.  At the end, power and n are destroyed. They are called local variables (covered next time.)  Variable result will get the value 128.  Variable i is unchanged

12 Call-by-value (cont ’ ) power n result i 1   Destroyed after function invocation

13 The return Statement  When a return is encountered,  program control goes back to the calling function  the value of the expression after the keyword return is sent back to the calling function  The return value of a function will be converted, if necessary, to the type of function as specified in the header to the function definition  Syntax: return expression; return ;

14 Example of return #include int min(int a, int b) { if (a<b) return a; return b; } int main(void) { int j, k, m; printf("Input two integers: "); scanf("%d%d", &j, &k); m = min(j, k); printf("\nOf the two values %d and %d, " "the minumum is %d.\n\n", j, k, m); return 0; }

15 The void data type  When a function does not return any value, the type of function is void, e.g, : void err_msg(int i) { switch (i) { case 1: printf(“invalid user”); break; case 2: printf(“input too large”); break; default: printf(“system error”); } return; /* optional */ }  When a function does not take any argument, the parameter list is void, e.g. : e.g.: int fabc(void) or int fabc()

16 Function Prototype  A function prototype tells the compiler the number and type of arguments that are to be passed to the function and the type of the value that is to be returned by the function  Syntax: type function_name(parameter_type_list); Or type function_name(parameter_list);

17 Example of function prototype #include int powerOfTwo(int n); void main() { int i; printf(“Enter a non-negative integer: “); scanf(“%d”, &i); printf(“The %d-th power of 2 is %d\n”, i, powerOfTwo(i)); return; } int powerOfTwo(int n) { int power=1; for (; n>0; n--) power *= 2; return power; }

18 A multi-file C program pgm.h #include #define N 3 void fct1(int); void fct2(void); void prn_info(void);

19 A multi-file C program ( cont’d ) main.c #include "pgm.h" int main(void) { char ans; printf("\nDo you want more information? "); scanf("%c", &ans); if (ans == 'y' || ans == 'Y') prn_info(); fct1(N); printf("\nBye!\n\n"); return 0; }

20 A multi-file C program ( cont’d ) prn.c #include "pgm.h" void prn_info(void) { printf(”\nUsage: pgm\n\n" "This program illustrates how one can write\n" "a program in more than one file. In this\n" "example, we have a single.h file that gets\n" "included at the top of our three.c files.\n" "Thus the.h file acts as the \"glue\"\n" "that binds the program together.\n" "\n”); }

21 A multi-file C program ( cont’d ) fct.c #include “pgm.h” void fct1(int n) { int i; printf(“Hello from fct1()\n”); for (i = 0; i < n; ++i) fct2(); } void fct2(void) { printf(“ Hello from fct2()\n”); }

22 How are the files related?

23 Macros  Macros are defined with #define  Problem: 11 is printed instead of 25 Solution: #define SQUARE(x) (x)*(x) #define SQUARE(x) x*x #include int main(void) { printf("Result is %d\n", SQUARE(2+3)); return 0; }