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.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 6: User-Defined Functions I
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.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
CPS120: Introduction to Computer Science Functions.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
User defined functions
CSCI 171 Presentation 6 Functions and Variable Scope.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
CSCI 161: Introduction to Programming Function
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Functions Inputs Output
User Defined Functions
Functions Chapter 3 of the text Motivation:
6 Chapter Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Functions, Part 1 of 3 Topics Using Predefined Functions
Standard Version of Starting Out with C++, 4th Edition
Introduction to Methods and Interfaces
Presentation transcript:

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 var) { return (var*var*var); } main() is the caller cube() is the callee main() calls cube() 2 times invokes return type parameter cube() is called by main() 2 times from

142 F -2 Picturing call and return main() cube() call return call return Can simplify and complete the picture: Operating System main() cube()printf() Static Call Graph: gives the call hierarchy, who calls who

void (1) 142 F-3 as a return type The function returns NO result void display_salary(double salary) { printf(“You earn $%.2f”,salary); return /*Optional*/ } Write display_salary(mySalary); to call the function

as a parameter The function takes NO parameter int number(void) { int user_number; printf("Enter a number: "); scanf("%i",&user_number); return user_number; } Write number(); to call the function as a parameter and return type void welcome(void){ printf("Welcome"); } Write welcome(); to call the function void (2) 142 F-4

142 F -5 Marching order: Control Flow (order of execution of a program) Begin at main() (no matter where main is) Execute each statement in descending order (top to bottom) UNLESS: function call function return if : Some statements can be skipped loops: some statements can be repeated (see later)

main {... prompt( );... prompt( ); } prompt { printf... } prompt { printf... } Control Flow: Example void prompt (void) { printf("Next integer:"); } int main (void) {... prompt();... prompt();... return 0; } CODE EXECUTION 142 F -6

142 F-7 Functions Prototypes To declare a function Put the prototypes after the # commands e.g. double tax(double income, double rate); The names are arbitrary and can even be omitted. But use them to clarify your program. A prototype specifies how the function is called. The compiler checks any call to a function against the prototype and generates an error if there is a mismatch need another argument of type double amount = tax( ); /*error*/

142 F-8 when writing code, you can just copy and paste the first line of your function definitions for the function prototypes. Note: #include /* prototypes */ double tax(double income, double rate); /* function definitions */ int main(void) {... amount = tax( , 0.12);... return 0; } double tax(double income, double rate) { /*compute and return the tax */ return income*rate; }

Parameters 142F-9 The caller may need to pass the function some values on which to operate input parameters of the function Inputs are specified as formal parameters in function definition double cube(double var) { return (var*var*var); } formal input parameter When calling the function, the caller provides actual parameters the value of the actual parameter is substituted for the formal parameter int main(void) {... x=cube(z/2);... } double cube(double var) { return (var*var*var); } parameter passing

Control and Data Flow When a function is called: (1) control (=execution) transfers to the function body (2) the function executes (3) control returns to the point of call int main(void) { double x,y,z; y=6.0; x=cube(y);... return 0; } double cube(double var) { return(var*var*var); } functions are called by value. var and y are in different locations of the memory. When calling cube, the value of y is copied into var (more on this later). Note: F-10

142F-11 Multiple parameters A function can have multiple parameters. The actual parameters (in the call statement) must match the formal parameters (in the header of the function definition) in number, order and type /* in main */ double length_room, width_room; double surface; … surface = area(length_room,width_room);... /* definition of area */ double area(double length, double width) { return (length*width); } match in type, number, and order but can have different names

142F-12 Rules for using functions actual parameters MUST match formal parameters in number in order in type a function can ONLY return ONE value A function with return type T can be used anywhere an expression of type T can be used. expression MUST be of type T In a function that returns type T, the return

Why have functions? 142F-13 Reuse of program text code it once and use it many times saves space and improve correctness Centralize changes changes and bug fixes are made in one place Better program organization easier to test, understand, and debug Modularization for team projects each person can work independently BETTER: “see the forest, and not just the trees” The program is shaped in meaningful units.

Why have functions? 142F-14 This is how modern programming is done: API Application Programming Interface A library of functions for a particular purpose e.g. Windows API.