CSCI 161: Introduction to Programming Function

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
 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
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Introduction to Methods
Chapter 6: Functions.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
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.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
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.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th 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.
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.
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.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
Chapter 6: Functions Starting Out with C++ Early Objects
User Defined Functions
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Chapter 4 void Functions
Functions.
6 Chapter Functions.
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Chapter 6: User-Defined Functions I
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Imran Rashid CTO at ManiWeber Technologies.
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

CSCI 161: Introduction to Programming Function

Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection of statements to perform a task Motivation for modular programming: Improves maintainability of programs Simplifies the process of writing programs

Functions Function - set of statements collected together and given a name Allow programmer to signify entire set of operations with single name Make programs shorter and simpler May include the computation of a value

Defining and Calling Functions Function call: statement causes a function to execute Function definition: statements that make up a function

Function Call e.g. f(x, y); Call act of executing statements associated with function done by writing name of function, followed by list of expressions enclosed in parenthesis arguments - allow passage of information to function function call is simply an expression

Function Return e.g. z = f(x, y); Return once function completes, returns to program step where call was made and continues may also send back results to calling program returning a value

math library #include <cmath> functions that take value of type double as argument and return value of type double sqrt, exp, log, sin, cos e.g. root = sqrt(3.0); distance = sqrt(x*x + y*y); tangent = sin(x) / cos(x);

Function Prototype(Declarations) name of the function type of each argument and descriptive names type of value that function returns Has form result-type name(argument-specifiers); where result-type – data type of function result name – name of function itself argument-specifiers – argument data types (optionally followed by descriptive names)

More on Prototypes e.g. double sqrt(double); Function sqrt takes one argument of type double and returns a value of type double e.g. double sin(double radians); Name for argument provides further information for programmer using function

void data type Keyword void is used to indicate no arguments or no return value e.g. int getNumber(void); void menu(void);

Writing Your Own Functions Two distinct steps 1. Specify function prototype (after the includes, before main) - shows format 2. Provide implementation of function (actual steps involved) - shows details

Function Body Block consisting of statement in { } Similar to main May include variable declarations Return statement - required if function returns a result back to caller return (expression);

Celsius to Fahrenheit Prototype double CtoF(double c); Implementation { return (9.0/5.0 * c + 32); } Another implementation double f; f = (9.0/5.0 * c + 32); return (f);

Main Program for CtoF To call function, could use f_value = CtoF(30.0); cout << f_value; Or simply use cout << CtoF(30.0); or double c_value = 30.0; f_value = CtoF(c_value);

Function Prototypes Ways to notify the compiler about a function before a call to the function: Place function definition before calling function’s definition Use a function prototype (function declaration) – like the function definition without the body Header: void printHeading() Prototype: void printHeading();

Prototype Notes Place prototypes near top of program Program must include either prototype or full function definition before any call to the function – compiler error otherwise When using prototypes, can place function definitions in any order in source file

Function Definition Definition includes: return type: data type of the value that function returns to the part of the program that called it name: name of the function. Function names follow same rules as variables parameter list: variables containing values passed to the function body: statements that perform the function’s task, enclosed in {}

Other Parameter Terminology A parameter can also be called a formal parameter or a formal argument An argument can also be called an actual parameter or an actual argument

Parameters, Prototypes, and Function Headers For each function argument, the prototype must include the data type of each parameter inside its parentheses the header must include a declaration for each parameter in its () void evenOrOdd(int); //prototype void evenOrOdd(int num) //header evenOrOdd(val); //call

Example output if each entered value is even or not, terminate when the number entered is -1 Done in class

Function Call Notes Value of argument is copied into parameter when the function is called A parameter’s scope is the function which uses it Function can have multiple parameters There must be a data type listed in the prototype () and an argument declaration in the function header () for each parameter Arguments will be promoted/demoted as necessary to match parameters

Passing Multiple Arguments When calling a function and passing multiple arguments: the number of arguments in the call must match the prototype and definition the first argument will be used to initialize the first parameter, the second argument to initialize the second parameter, etc.

(Program Continues)