B. RAMAMURTHY CH.4 IN KERNIGHAN AND RITCHIE C TEXTBOOK C Language: Functions 5/11/2013 Amrita-UB-MSES-2013-4 1.

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

C Language.
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Spring Semester 2013 Lecture 5
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
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.
Review for midterm exam Dilshad M. Shahid Spring NYU.
Topic R3 – Review for the Final Exam. CISC 105 – Review for the Final Exam Exam Date & Time and Exam Format The final exam is 120-minutes, closed- book,
Guide To UNIX Using Linux Third Edition
Chapter 6: Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
CPS120: Introduction to Computer Science Functions.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
User defined functions
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
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.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
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, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
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.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
TK1924 Program Design & Problem Solving Session 2011/2012
Dr. Shady Yehia Elmashad
C Language By Sra Sontisirikit
Topic: Functions – Part 2
Function There are two types of Function User Defined Function
Functions CIS 40 – Introduction to Programming in Python
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
Functions in C Mrs. Chitra M. Gaikwad.
B. Ramamurthy University at Buffalo
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Dr. Shady Yehia Elmashad
B. Ramamurthy University at Buffalo
Functions Declarations CSCI 230
B. Ramamurthy University at Buffalo
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
B. Ramamurthy University at Buffalo
Functions, Part 1 of 3 Topics Using Predefined Functions
Modular Programming with Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Array and Method.
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Introduction to Classes and Objects
Presentation transcript:

B. RAMAMURTHY CH.4 IN KERNIGHAN AND RITCHIE C TEXTBOOK C Language: Functions 5/11/2013 Amrita-UB-MSES

Topics 5/11/2013 Amrita-UB-MSES Purpose of functions Function design Function definition Function call

Functions 5/11/2013 Amrita-UB-MSES Functions are modular units that perform a specific operation It provides the ability to divide the program into coherent modules Functions can be parameterized providing a general solution that can be customized with specific parameters Functions offers a method for spreading the code around many files, thus providing a method for organization of code (into libraries, say) Once defined, a function can be called from anywhere accessible and any number of times resulting in reusability, standardization and uniform application of operations

Function Design 5/11/2013 Amrita-UB-MSES Lets say you want to turn Fahrenheit to Celsius converter into a function rather than dumping all the code in the main function. fcel Value in F Converted value in C celf Value in C Converted value in F

Function Definition 5/11/2013 Amrita-UB-MSES Define the prototype Type function name (parameter type, parameter type…); 2. Define the header Return type function name (param type name, param type name…) 3. Define the body of the function { local variable statements one or more return statements} Example: float fcel (float); float fcel (float fah) { float celsius = (5.0/9.0) * (fah ); return celsius; }

Function Call 5/11/2013 Amrita-UB-MSES It is not enough defining the function: it needs to be activated. This is done by calling the function. Calling a function involves specifying its name and actual parameter values. If there is a return value the call needs to be assigned to a variable. Example float fahren = 35.0; float celsius = fcel(fahren);

Complete Example 5/11/2013 Amrita-UB-MSES Lets write another function for Celsius to Farenheit. float celf (float celsi) { float fahr = celsi * 9.0/ ; return fahr; } Call: float fa = celf (35.0);

Demos 5/11/2013 Amrita-UB-MSES Lets add an input statement to the functions and complete the example. Scanf is the input statement. You provide the format(type) as well as the location (address) with name the input will be loaded into. Example: scanf(“%f”, &celsi); scanf(“%f”, &fahr); celsi fahr

Summary 5/11/2013 Amrita-UB-MSES We studied basics of a function design. We learned function definition and function call. Standard IO using printf and scanf was also illustrated. Parameter passing by value and reference was introduced. We will discuss this in detail later. We demoed the implementation of the functions.