CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Chapter 6: User-Defined Functions I
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Basic Elements of C++ Chapter 2.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
CSC 107 – Programming For Science. Announcements  Tutors available MTWR in WTC206/WTC208  Special lab (with Macs) & not in the Tutoring Center  Can.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSC 107 – Programming For Science. Spacing in a Program  C++ ignores spaces in a program  This also means where newlines placed ignored  #define &
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
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.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
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. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
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.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
CSC Programming for Science Lecture 37 : Course Review.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CSC Programming for Science Lecture 23: More on Function Parameters.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
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
Variables Mr. Crone.
Variables A piece of memory set aside to store data
CSC113: Computer Programming (Theory = 03, Lab = 01)
Multiple Files Revisited
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Understanding Conditions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Multiple Files Revisited
CS150 Introduction to Computer Science 1
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and why we use the return statement  Why skipping return statements are bad ideas

Why We Use Functions  Simplify code  Replace copies of code by placing in single location  Locate commonly-used computations & actions  Good to find code, but can only return a single value  Input & output performed in these functions  Will discuss ways to change parameters’ values

Functions  Already been programming with functions  Built-in functions like pow, exp, & log  Writing & using programmer-defined function: main  All functions similar  Will be using same process to call function  Handling return result same for all functions  Process is same for variables, scoping, passing data

Functions  All functions similar, differ only in who wrote it Built-in Function WriterUser-Defined Function Writer

Function Names  Rules over legal names identical to variables  Letter or underscore start name for it to be legal  Can use any letters, numbers, or underscore after  Names should be unique (including variables)  Function names rely upon relaxed style rules  Start with a letter & make name meaningful  Names should only use words you would say to:

Function Declaration

Return Type  Functions must specify their return type  Type of value returned by the function float abs(float x); double pow(double x, double t); int main();  Use special return type, void, if no value is returned void goodSoldier(); void doSomethingWithX(int x);

Function Definitions

return Statement

return Examples

return Statement

return Examples

Multiple return Statements  Multiple return s possible in a single function  Each time function is called, only one is executed  Gives greater flexibility by not tying code down bool userTypedOddNumber() { int num; cout > num; if ( (num % 2) == 1) { return true; } else { return false; } }

Your Turn  Get into your groups and try this assignment

For Next Lecture  Read about parameters in Section 9.4 –  How do we pass values to a function?  How does a function take in those values?  What can a function do to those values?  Weekly Assignment #6 out & due next Tues.  Avoid the rush by start working on it now  Programming Assignment #2 now on Angel  This is a larger assignment and due in 3 weeks