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.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
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.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined 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.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 6: User-Defined Functions
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose.
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.
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: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined 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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
More on Functions. Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
CSCI 161: Introduction to Programming Function
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 4 Functions Objectives
Chapter 4 void Functions
6 Chapter Functions.
Chapter 6: User-Defined Functions I
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to C++ Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
CPS125.
Presentation transcript:

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 into manageable parts is called the top-down design. In top-down design, a program is divided into a main module and its related modules (functions ). Each module is in turn divided into submodules until the resulting modules are intrinsic; that is until they are implicitly understood without further division. The top-down design is usually done using a visual representation of the modules know as a structure chart. Main Module Module 1Module 2 Module 1a Module 1b Module 1c Module 2a Module 3a Module 3 Module 3b Calling modules Called modules No communication can take place directly between modules that do not have a calling-called relationship

Functions The technique used to pass data to a function is known as parameter passing. Data are passed to a function using one of two techniques: pass by value - copy of the data is made and the copy is sent to the function. pass by reference - sends the memory address of the data rather than a copy. Functions in C A C program is made of one or more functions, one and only one of which must be called main. The execution of the program always starts and ends with main but this function can call other functions to do special tasks. The function main is called by the operating system; main in turn calls other functions. When main is completed, control returns to the operating system. A function receives zero or more pieces of data, operates on them, and returns at most one piece of data. At the same time, a function can have a side effect. Zero or more pieces of data go in Side effects Function At most one piece of data comes out

Functions The side effect can involve accepting data from outside the program, sending data out of the program to the monitor or a file, or changing the value of a variable in the calling function. There are several reasons for using functions in C: divide the problem into understandable and manageable steps provide a way to reuse code standard and personal libraries protect data (data local to a function are available only to that function) User-Defined Functions These are functions that are defined by the user. Like any other objects in C, functions must be both declared and defined. The function declaration is done with a prototype statement. The function definition contains the local variables and code needed to complete the task. You use a function by calling it. The name of the function is used in three ways: for declaration, in a call, and for definition.

Functions #include int main (void) { /* Prototype Declarations to be seen only by main*/ int multiply ( int num1, int num2); /* Local declarations */ /* Statements */ … product = multiply ( multiplier, multiplicand); … return 0; } int multiply (int num1, int num2) { /* Local declarations */ /* Statements */ … return ( num1 * num2); } Definition is done after the calling function Declaration is coded first Calling is done in the statement section semicolon no semicolon

Functions Function Definition The function definition contains the code for the function. It is made up of two parts: return_type function_name ( formal parameter list ) { /* Local Declarations */ /* Statements */ } Function header Function body The function header consists of three parts: the return type - this will be the data type of the one returned value from the function. This is the value returned by the return statement. The function name(must follow the rules of identifiers the function body - contains the local declarations and statements for the function. The function body is terminated with a return statement.

Formal parameters are variables that are declared in the header of the function definition. The function’s formal parameter list defines and declares the variables that will contain the data received by the function from the calling function. If the parameter list is empty then use the keyword void. All parameters must indicate the type and name and must be separated by commas. Functions Prototype declaration consists only of the function header; they contain no code. Unlike the header for the function definition, prototype declarations are terminated with a semicolon. In the parameter list the names do not need to be the same in the prototype declaration and the function definition but the types must be the same or you will get a compile time error. The prototype must be placed before the physical call. int average (int x, int y ) { double sum; sum = x + y; return (sum / 2); } Parameter variables x y local variable sum Two values are received from the calling function

Functions int main (void) { int multiply ( int multiplier, int multiplicand ); int product; … product = multiply (6, 7 ); … return 0; } int multiply (int x, int y ) { return x + y ; } Prototype declaration 6767 Function definition 42 values copied x y Function call The Function call The actual parameters of the function call identify the values that are to be sent to the called function. They match the function’s formal parameters in type and order in the parameter list. Actual parameters of the function call can be primary expression, binary expression or any expression that reduces to a single value. multiply ( multiply ( 2, 3),1 + 5 );

Functions Void functions with no parameters. int main (void ) { void greeting (void); /* statements */ greeting( ); return 0; } void greeting (void ) { printf ( “Hello World”); return ; } Hello world declaration Call Side effect

Functions Void functions with parameters. int main (void ) { void printTwo (int, int ); int a = 5; int b = 10; printTwo( a, b); return 0; } void printTwo (int x, int y ) { printf ( “%d %d”, x, y); return ; } 5 10 declaration Call Side effect Nothing is returned to the calling function a b xy Values copied

Functions Functions that return a value. int main (void ) { int sqr (int x ); int a; int b; scanf(“%d”, &a ); b = sqr (a); printf(“%d squared: %d\n, a, b); return 0; } int sqr (int x ) { return ( x * x); } Call a b x copied Returned value here declaration