CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Chapter 3 Function Basics Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
CS 201 Functions Debzani Deb.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
CSC 200 Lecture 04 2/8/06 Matt Kayala. Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
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 7 Functions.
Lecture 3 Using C++ Functions COSC1567 C++ Programming.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
Slide 1 Chapter 3 Function Basics. Slide 2 Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
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.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
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.
Programming Fundamentals Enumerations and Functions.
 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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 9: Value-Returning Functions
User-Written Functions
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Function Basics TBC=15 Dr. Xiao Qin.
CSCI 161: Introduction to Programming Function
Lesson #6 Modular Programming and Functions.
Functions Declarations CSCI 230
Introduction to Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Computing Fundamentals
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

CSIS 113A Lecture 5 Functions

Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms, methods  In C++: functions  I-P-O  Input – Process – Output  Basic subparts to any program  Use functions for these ‘pieces’

Predefined Functions  Libraries full of functions for our use!  Two types:  Those that return a value  Those that do not (void)  Must ‘#include’ appropriate library  e.g.: , (Original ‘C’ libraries)  (for cout, cin)

Using Predefined Functions  Math functions very plentiful  Found in library  Most return a value (the ‘answer’)  Example: theRoot = sqrt(9.0);  Components: sqrt = name of library function theRoot = variable used to assign ‘answer’ to 9.0 = argument or ‘starting input’ for function  In I-P-O:  I = 9.0  P = ‘compute the square root’  O = 3, which is returned & assigned to theRoot

The Function Call  Back to this assignment: theRoot = sqrt(9.0);  The expression ‘sqrt(9.0)’ is known as a function call, or function invocation  The argument in a function call (9.0) can be a literal, a variable, or an expression  The call itself can be part of an expression:  bonus = sqrt(sales)/10;  A function call is allowed wherever it’s legal to use an expression of the function’s return type

An Example #include #include using namespace std; int main() { int numLoop; cout > numLoop; for(int i = 0; i < numLoop; i++) { cout << "The square of " << i << "is " << pow(i, 2) << endl; cout << "The cube of " << i << " is " << pow(i, 3) << endl; cout << "The sqrt of " << i << " is " << sqrt(i) << endl; } return 0; }

More Predefined Functions  #include  Library contains functions like:  abs()// Returns absolute value of an int  labs()// Returns absolute value of a long int  *fabs()// Returns absolute value of a float  *fabs() is actually in library !  Can be confusing  Remember: libraries were added after C++ was ‘born’, in incremental phases  Refer to appendices/manuals for details

More Math Functions  pow(x, y)  Returns x to the power y double result, x = 3.0, y = 2.0; result = pow(x, y); cout << result;  Here 9.0 is displayed since = 9.0  Notice this function receives two arguments  A function can have any number of arguments, of varying data types

Even More Math Functions

Predefined Void Functions  No returned value  Performs an action, but sends no ‘answer’  When called, it’s a statement itself  exit(1);// No return value, so not assigned  This call terminates program  void functions can still have arguments  All aspects same as functions that ‘return a value’  They just don’t return a value!

Ok, So What Is This void Thing? Some functions don't return anything –An example from the previous table is the exit function. – Notice that the return type is void. void is a type in C++ and it is used to indicate nothing. –So, why do you need void it if it means nothing? Well that is a tough question. It all comes down to the fact that the compiler needs a definition of the function before you can use it. To make all functions similar (meaning they all have a return type) void is used to indicate the function returns nothing.

Programmer Defined Functions Like small sub programs that allow you to organize your code in a logical fashion. What are you really doing when you write a piece of software? –The answer is that you are solving some sort of problem. –The easiest way to tackle the problem is to break the large problem into several smaller problems and then tackle them one at a time. In computer science, this is called stepwise refinement

Function Syntax

User Defined Functions II Return Type – –All functions declarations must have a return type. If the function doesn't return anything then you need to use the keyword void. You can return any valid C++ type from a function. Later on you will learn that you can return you own custom made types. Function Name – –You can name a function anything that you like but you must also follow the same rules for naming variables. Formal Parameters – –The formal parameters (formal arguments) must be enclosed inside the parentheses following the function name. If you write a function that doesn't need any parameters then you just leave the parentheses empty. If you need multiple parameters sent to your function they must be separated by commas. Curyly Braces – –The body of the function is always surrounded by open and closed curly braces. If you omit one or all of the braces then you will get a big syntax error. Function Body – –All of the code you want executed when the function is called (invoked) goes between the open and closed curly braces.

Simple Example #include using namespace std; void helloWorld(); int main() { helloWorld(); return 0; } void helloWorld() { cout << "Hello World" << endl; }

The return Statement Two ways that program can branch back to where it came from. –1. when the closing brace of the function is reached. In this case the flow of the program will branch back to where it was called from. –2. The return statement. You can place the return statement anywhere you want within a function and the program will branch back to where it came from.

Another Example #include using namespace std; void helloWorld(); int main() { helloWorld(); return 0; } void helloWorld() { cout << "Hello World" << endl; return; // Force the flow of the program to brach back to where it came from }

Returning A Value Some functions produce values. –Data gets returned to calling function Place a value after the return statement –return 10; »Value must match the return type –Value returned gets assigned through equal sign int x = foo(); // Assigned just like value to a variable

Another Example #include using namespace std; int getValue(); int main() { int someValue; someValue = getValue(); cout << "The value entered was " << someValue << endl; return 0; } int getValue() { int val; cout > val; return val; }

Modified Example #include using namespace std; int getValue(); int main() { cout << "The value entered was " << getValue() << endl; return 0; } int getValue() { int val; cout > val; return val; }