4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.

Slides:



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

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 6: User-Defined Functions I
1 Module 12 Computation and Configurations –Formal Definition –Important Terms –Examples.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Module 12 Computation and Configurations Formal Definition Examples.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
1 CSC103: Introduction to Computer and Programming Lecture No 14.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions g prototypes g arguments g overloading g return values part I Re-read Section 1.2.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Python Functions : chapter 3
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Functions Functions, locals, parameters, and separate compilation.
Learners Support Publications Introduction to C++
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Methods.
User-Defined Functions (cont’d) - Reference Parameters.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Test 2 Review Outline.
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Functions Inputs Output
FIGURE 4-10 Function Return Statements
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.
Value returning Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
CS150 Introduction to Computer Science 1
FIGURE 4-10 Function Return Statements
LMC Little Man Computer What do you know about LMC?
Chapter 6: User-Defined Functions I
CS150 Introduction to Computer Science 1
Fundamental Programming
FIGURE 4-10 Function Return Statements
Python Functions.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

4.3 Functions

Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them

Function Prototype FUNCTIONTYPE FUCNTIONNAME(TYPE FOR VAR1,TYPE FOR VAR 2……); Example int FindSum(int, int );

Function Prototype Used to let the computer know function return type and parameters before the function runs. Best to place at the very top of a program immediately after header files

Function Definition FUCNTIONTYPE FUNCTIONNAME(TYPE PARAMETER1,TYPE PARAMETER2….) { instruction1; instruction2; ….. return (whatever you want); }

The actual instructions for the function to execute. Needs to have variable names in definition – int FindSum(int X, int Y)

Function Call The function must be told to run Runs within body of main function FUNCTIONNAME(PARAM1,PARAM2);

3 Ways to Communicate Data 1.Return values 2.Use of parameters within functions 3.Global Variables

Use of Return Values Float FindDiff(float,float); Float FindDiff(float first, float second) { float differ; differ= first-second; return differ; }

Use of Return Values

Restrictions Using the return value method, we can only return one piece of data back from the functions.

Assign Make a program that inputs 2 numbers, and outputs the sum, difference, and product of the 2 numbers. Use at least 3 different functions.