Functions Dilshad M. Shahid New York

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
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.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
1 MATERI PENDUKUNG METHOD Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Functions Dilshad M. Shahid New York
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
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.
1 Lecture 3 Part 1 Functions with math and randomness.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Learners Support Publications Functions in C++
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© 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.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
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.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
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.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
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.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 5 Functions DDC 2133 Programming II.
Programming Fundamentals Lecture #7 Functions
CS1061 C Prgramming Lecture 11: Functions
CSCI 161: Introduction to Programming Function
Functions.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions Imran Rashid CTO at ManiWeber Technologies.
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:

Functions Dilshad M. Shahid New York

Today Function definition return statement Function prototypes Function invocation Examples

Function definition Heart of effective problem solving is problem decomposition Taking a problem and breaking it into small, manageable pieces is critical to writing large programs This is known as “top-down” method of programming

Function definition Another motivation is software reusability - using existing functions as building blocks to create new programs

Function definition Format return-value-type function-name(parameter_list) { declarations statements return value }

Function definition Function-name is any valid identifier return-type is the data type of the result returned to caller a return-type void indicates a function does not return a value if you don’t specify a return-type, the compiler assume int

Return statement When a return statement is encountered, execution of function is terminated and control is passed back to the calling environment If the return statement contains an expression, then value of that expression is passed back as well Morever, this value will be converted, if necessary, to the specified type

Return statement Some examples are: return; return x; return ++a; return (a*b);

Function prototypes Functions have to be declared before they can be used A function prototype tells the compiler: –the number and types that are to be passed to the function –the type of value that is to be returned by the function

Function prototypes General form return-value-type function-name(parameter_list); The compiler uses function prototypes to validate function calls Also values passed to functions are properly coerced, if possible

Function prototypes If the first line of your function is: void sum_it (int a, int b) { then an easy way to declare your function prototype is copy and paste this line, putting ; instead of { at the end, as in: void sum_it (int a, int b); But the compiler actually ignores the names of the variables in the parameter list so the following would be equally valid: void sum_it (int, int); It’s up to you - whatever you find easiest

Function invocation Program execution always begins with main() When program control encounters a function name, the function is called or invoked This means that control passes to that function After the function does its work, program control passes back to the calling environment, which then continues with its work

Function invocation Examples getchar(); max(x, y); sum=compute_sum(n);

Function Invocation General layout function1 prototype; function2 prototype; main { variable declarations function1 call function2 call statements } function1 { } function2 { }

Function invocation Example cleaned up and moved to web page under Programs Discussed in Class