CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
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.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Chapter 5 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Computer Science 1620 Function Scope & Global Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
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 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 - What Is A Function? - Advantages Function Declaration
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
User-Written Functions
A Lecture for the c++ Course
Chapter 5 Function Basics
User-Defined Functions
6 Chapter Functions.
Chapter 9: Value-Returning Functions
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs using functions

Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs using functions  Already been doing this  Already been doing this, but should clarify process

Functions  Already written programs using functions  One function always present in all programs: main  cos, sin, floor also functions you've called before  main is a programmer-defined function  Pre-defined functions are similar, but part of C++  Programmers can define and use own functions cos radians cosine

Functions  Already written programs using functions  One function always present in all programs: main  cos, sin, floor also functions you've called before  main is a programmer-defined function  Pre-defined functions are similar, but part of C++  Programmers can define and use own functions Function Parameters Return Value

Why We Use Functions  Simplify code  Replace copies of code by placing in single location  Commonly-used math function computation  Each function can return a single value  Input & output performed in these functions  Will discuss ways to change parameters’ values

Function Basics

Return Type  Each function must declare a return type  Type of value returned by a function float abs(float x); double pow(double x, double t); int main();  May want nothing returned : use void as return type void printFormattedNumber(int x); void readAndPrintAverage();  Must return value from non- void function  If function is void, cannot return value

Function Declaration  When definition is after calling function…  Could be that function is later in file  Function in another file for use in many programs  Also important for built-in functions without bodies  Declare functions at start of the file  Often listed in header ( *.h ) files to enable reuse  #include "copy-and-paste" code from those files  Declaration lists vital information: function signature

Function Declarations

Function Definitions

Variable Scope  Variables create name for memory address  Name is not universal, but limited by the scope  Variable usable only in braces in which declared  For this copy of variable, scope defines its lifetime  Variable "dies" with end of scope in which declared  At start of scope, new copy created  Cannot use outside scope: error for bad variable  Must have unique names within a scope  If no overlap, can reuse names since old name gone

Variable Scope int num = 3; void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { readNumber(num); readNumber(5); return 0; }

int num = 3; void readNumber(int len) { int num = 0; // This is name is not unique here! for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { readNumber(num); readNumber(5); return 0; } Variable Scope

void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { int num = 3; readNumber(num); readNumber(5); return 0; }

Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { int num = 3; readNumber(num); readNumber(5); return 0; }

Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { int num = 3; readNumber(num); readNumber(5); return 0; }

Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { int num = 3; readNumber(num); readNumber(5); return 0; }

Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { int num = 3; readNumber(num); readNumber(5); return 0; }

Let's Trace void readNumber(int); int main() { int num = 3; readNumber(num); readNumber(5); return 0; } void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; }

For Next Lecture  Read more about functions in Sections  Describe how value returned from a function  What happens to code after the return statement?  How do we pass values to a function?  What can a function do to those values? Wed.  Weekly Assignment #6 out & due next Wed.  Avoid the rush by start working on it now  Note special due date, since no school on Tuesday