1 CS161 Introduction to Computer Science Topic #9.

Slides:



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

Spring Semester 2013 Lecture 5
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
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
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Introduction to C++ Functions Topic #2 1CS162 Topic #2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CPS120: Introduction to Computer Science Decision Making in Programs.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 CS161 Introduction to Computer Science Topic #10.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
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.
Learners Support Publications Functions in C++
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
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.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Program #2 Algorithm for Parking at PSU. Understanding the Assignment You will be writing a program to find out how much someone at PSU might be spending.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
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.
CS161 Topic #10 1 Today in CS161 Lecture #10 Introduction to Functions What is a function? Why would you want to use a function? How do you define functions?
User-Written Functions
Chapter 6: User-Defined Functions I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
User Defined Functions
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
Today in CS161 Week #6 Introduction to Functions
CPS125.
Presentation transcript:

1 CS161 Introduction to Computer Science Topic #9

CS161 Topic #92 Today in CS161 Introduction to Functions –What is a function? –Why would you want to use a function? –How do you define functions? –How do you call functions?

CS161 Topic #93 Functions: What are they? We can write our own functions in C++ These functions can be called from your main program or from other functions A C++ function consists of a grouping of statements to perform a certain task This means that all of the code necessary to get a task done doesn't have to be in your main program You can begin execution of a function by calling the function

CS161 Topic #94 Functions: What are they? When we write algorithms, we should divide our programs into a series of major tasks... –where each major task is a function, called by the main program We can group together statements that perform a distinct task and give the overall action a name. This is accomplished by writing a C++ function.

CS161 Topic #95 Functions: What are they? For example, tasks such as driving a car, or cooking breakfast are every day functions that we use. The exact details of driving a car or cooking are hidden in the actual process, but even though you don't know the details -- just from the statement "driving a car" you know what that involves and what I am talking about. I don't need to have to tell you that first I get out my keys, then unlock the car door, then get inside, then.....

CS161 Topic #96 Functions: What are they? The same thing applies to functions in C++. A function has a name assigned to it and contains a sequence of statements that you want executed every time you invoke the function from your main program! Data is passed from one function to another by using arguments (in parens after the function name). When no arguments are used, the function names are followed by: "()".

CS161 Topic #97 Functions: Defining Them... The syntax of a function is very much like that of a main program. We start with a function header: data_type function_name() { }

CS161 Topic #98 Functions: Defining Them... A function must always be declared before it can be used This means that we must put a one-line function declaration at the beginning of our programs which allow all other functions and the main program to access it. This is called a function prototype (or function declaration) The function itself can be defined anywhere within the program.

CS161 Topic #99 Functions: Using Them... When you want to use a function, it needs to be CALLED or INVOKED from your main program or from another function. If you never call a function, it will never be used. We have all had experience calling functions from the math library (e.g., the pow function) To call a function we must use the function call operator () some_variable = pow (x, 3);

CS161 Topic #910 Functions: Calling pow... When we called the power function, we are temporarily suspending execution of our main program (or calling routine) and executing a function called pow that someone else has written. It takes two values as arguments ( x and 3 ), called actual arguments and returns to the calling routine the result (a floating point value)

CS161 Topic #911 Let’s try writing a function! What might the major steps be for a program that calculates the day of the year based on a given date: –Welcome the user –Get the date from the user –Calculate the day of the year –Display the result –Signoff message Each one of these steps could be written as a function...

CS161 Topic #912 Let’s try writing a function! void welcome(); //function prototype int main() { welcome();//function call... return 0; } void welcome() {//function definition cout << “Welcome to.... “; }

CS161 Topic #913 Let’s try writing a function! Notice that in this example we use a function prototype for our function declarations. They are very similar to the function header except that they must be terminated by a semicolon....just like any other declaration in C++.

CS161 Topic #914 Why write functions? You might ask, why go through the trouble to write a program that does no more than the original, shorter version? One reason is that functions can be used as prefabricated parts for the easy construction of more complicated programs. Another reason is that a function - once created - can be called any number of times without writing its code again.

CS161 Topic #915 Why write functions? As our programs get more complicated, it is really important that you clearly understand the order in which statements are executed. The main program runs first, executing its statements, one after another. Even though the functions are declared before the main program (and may also be defined before the main program), they are not executed until they are called. They can be called as many times as you wish

CS161 Topic #916 Why write functions? By giving the task a name, we make it easier to refer to. Code that calls clearly named functions is easier to understand than code in which all tasks are described in the main program. Programs that use functions are easier to design because of the way they "divide and conquer" the whole problem.

CS161 Topic #917 Why write functions? By having a function perform the task, we can perform the task many times in the same program by simply invoking the function repeatedly. The code for the task need not be reproduced every time we need it. A function can be saved in a library of useful routines and plugged into any program that needs it. (like we have seen with the pow function)

CS161 Topic #918 Why write functions? Once a function is written and properly tested, we can use the function without any further concern for its validity. We can therefore stop thinking about how the function does something and start thinking of what it does. It becomes an abstract object in itself - to be used and referred to.

CS161 Topic #919 Why write functions? Functions enable us to implement our program in logically independent sections n the same way that we develop the solution algorithm. Our main for could be: welcome(); get_date(month, day, year); day = calculate (month, day, year); display(day); signoff();

CS161 Topic #920 Some details about functions: Each function declaration can contain declarations for its own...this includes constants, variables. These are considered to be LOCAL to the function and can be referenced only within the function in which they are defined data_type some_function() { data_type variable;//local variable }

CS161 Topic #921 Some details about functions: #include using namespace std; int print_asterisk(void); int main(){ int number;//local variable number = print_asterisk();... return 0; } // put comments here describing the purpose of this function! int print_asterisk () { int num_asterisk;//local variable cout << "How many asterisks would you like?\n"; cin >> num_asterisk; return(num_asterisk); }

CS161 Topic #922 Some details about functions: To have a function return a value - you simply say " return expression ". The expression may or may not be in parens. Or, if you just want to return without actually returning a value, just say return; (note: return(); is illegal). If you normally reach the end of a function (the function's closing "}"), its just like saying return; and no value is returned.

CS161 Topic #923 Some details about functions: For functions that don't return anything, you should preface the declaration with the word "void". When using void, it is illegal to have your return statement(s) try to return a value Also notice, that the type of a function must be specified in both the function declaration and in the function definition.