28.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture 9 28.1.2001.

Slides:



Advertisements
Similar presentations
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Advertisements

User Defined Functions
C Language.
Spring Semester 2013 Lecture 5
Chapter Five Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
CS 201 Functions Debzani Deb.
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 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
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.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Functions A Problem Suppose we are writing a program that displays messages on the screen. We want to display rows of ======================== =======
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
User defined functions
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
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.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Chapter 6: User-Defined Functions I
CSCI 161: Introduction to Programming Function
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
User Defined Functions
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
Predefined Functions Revisited
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.
Presentation transcript:

Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture

Sudeshna Sarkar, IIT Kharagpur 2 Class Test on 7 th February Thursday 5:30 to 6:30 pm

Sudeshna Sarkar, IIT Kharagpur 3 A Problem Suppose we are writing a program that displays messages on the screen. We want to display rows of =============================== to separate sections of output.

Sudeshna Sarkar, IIT Kharagpur 4 Solution #include int main (void) { /* produce some output */ /* print banner line */ printf(“==============\n”) ; /* produce more output */ printf(“==============\n”); /* produce even more output */ printf(“==============\n”); /* produce final output */ }

Sudeshna Sarkar, IIT Kharagpur 5 Critique Redundant code What if we want to change the display e.g. to print a blank line before and after each line with =’s ? What if we want to print banner lines in some other program?

Sudeshna Sarkar, IIT Kharagpur 6 The Solution: Functions Definition: A function is a named code sequence A function can be executed by using its name as a statement or expression. The function may have parameters - information that can be different each time the function is executed. The function may compute and return a value.

Sudeshna Sarkar, IIT Kharagpur 7 Why use functions ? Functions provide an abstraction when writing a program - allow low-level details to be packaged. Able to package a computation we need to perform at multiple spots within a program. Write once, use many times. If changes are needed, they only have to be done once, in one place.

Sudeshna Sarkar, IIT Kharagpur 8 Why use functions ? Allows the programmer to create a program from smaller, simpler sub-components. Many programs are far too large to understand all at once. Functions give us a way to break a large program into smaller pieces A function should perform a well-defined task.

Sudeshna Sarkar, IIT Kharagpur 9 Common functions int main (void) {... } Function definition for main() printf (“%d + %d is %d\n”,x,y,z); scanf (“%d%d”, &x, &y) ; limit = sqrt ((double) num); Function calls

Sudeshna Sarkar, IIT Kharagpur 10 More common functions Every standard C compiler comes with a set of standard libraries. #include Tells the compiler you will use the standard IO library #include C’s standard math library functions: sqrt, pow, sin, cos, exp,... isspace,...

Sudeshna Sarkar, IIT Kharagpur 11 Defining your own functions You may define your own functions in your programs You define a function by giving its name and writing the code that is executed when the function is called.

Sudeshna Sarkar, IIT Kharagpur 12 High level programming structure main () {... SetUpBoard (); do { redsTurn (); blacksTurn (); } while (gamenotover ()); }

Sudeshna Sarkar, IIT Kharagpur 13 Function definition /* print banner line */ void print_banner (void) { printf (“=========”); printf (“=========\n”); } heading comment function name function body (statements to be executed) A function can have ANY number of ANY kind of statements

Sudeshna Sarkar, IIT Kharagpur 14 /* print banner line */ void print_banner (void) { printf (“=========”); printf (“=========\n”); } void void has two different roles in this function definition. indicates that the function does not return (have) an output value. indicates that the function has no parameters.

Sudeshna Sarkar, IIT Kharagpur 15 Calling a function int main (void) { /* produce some output */... print_banner (); /* produce more output */... print_banner (); /* produce final output */... print_banner (); } To execute the function, it is called or invoked from within a program or another function. Note: A function that does not return a value can be called wherever a statement is allowed.

Sudeshna Sarkar, IIT Kharagpur 16 Terminology main () is the caller print_banner() is the callee. main() invokes / calls print_banner() 3 times. print_banner() is called from main()

Sudeshna Sarkar, IIT Kharagpur 17 Function Control Flow /* print banner line */ void print_banner (void) { printf(“**************\n”); } int main (void) {... print_banner ();... print_banner (); return (0); } main (void) { print_banner (); print_banner (); } print_banner { } print_banner { }

Sudeshna Sarkar, IIT Kharagpur 18 Control Flow All C programs Start at main () /*no matter where main()is */ Continue in top-to-bottom order, statement by statement, unless the order is changed by: function call function return if loops

Sudeshna Sarkar, IIT Kharagpur 19 Function Type and Value A function can return a value. Like all values in C, a function return value has a type. The function has the type of its returned value. /* Get number from user */ int get_input (void) { int num; printf (“Enter a number:”); scanf (“%d”, &num); return (num); } function type return statement returned value

Sudeshna Sarkar, IIT Kharagpur 20 Calling a function A value-returning function is called by including it in an expression. int main (void) { int x, y; x = get_input() ; y = get_input() ; printf (“ %d + %d = %d\n”, x, y, x+y); return (0); } Note : A value returning function can be used anywhere an expression of the same type can be used.

Sudeshna Sarkar, IIT Kharagpur 21 return In a value-returning function (result type is not void) return does two distinct things : 1. specify the value returned by the execution of the function 2. terminate that execution of the function. In a void function: return is optional at the end of the function body. return may also be used to terminate execution of the function explicitly. No return value should appear following return.

Sudeshna Sarkar, IIT Kharagpur 22 void compute_and_print_itax () { double income; scanf (“%f”, &income); if (income < 50000){ printf (“Income tax = Nil\n”); return; } if (income < 60000){ printf (“Income tax = %f\n”, 0.1*(income-50000); return; } if (income < ) { printf (“Income tax = %f\n”, 0.2*(income-60000)+1000); return ; } printf (“Income tax = %f\n”, 0.3*(income )+19000); } Terminate function execution before reaching the end

Sudeshna Sarkar, IIT Kharagpur 23 Function parameters It is very often useful if a function can operate on different data values each time it is called. Such values are function (input) parameters. The function specifies its inputs as parameters in the function declaration. /* Find area of a circle with radius r */ double area (double r) { return ( *r*r) ; } parameter

Sudeshna Sarkar, IIT Kharagpur 24 Arguments The function call must include a matching argument for each parameter. When the function is executed, the value of the argument is substituted for the parameter. int main (void) {... double circum;... area1 = area(circum/2.0);... } double area (double r) { return (3.14*r*r); } parameter passing