FUNCTIONS - What Is A Function? - Advantages Function Declaration

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Functions Modules in C++ are called functions and classes
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
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.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
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 CS161 Introduction to Computer Science Topic #9.
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.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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)
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default 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.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 5 Function Basics
Functions and an Introduction to Recursion
School of EECS, Peking University
Chapter 5 Classes.
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Value returning Functions
Chapter 6: User-Defined Functions I
Functions and an Introduction to Recursion
CS149D Elements of Computer Science
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Introduction to Functions
Functions I Creating a programming with small logical units of code.
Presentation transcript:

FUNCTIONS - What Is A Function? - Advantages Function Declaration Function Definition Variable scope Function Arguments Passing Arguments by value Passing Arguments by Reference Calling other Functions / Recursive

What is a Function? It is a section of code that is enclosed and that provides specific functionality to a program. i.e. - it is enclosed outside the main program - therefore it is called from outside the main program or other functions to provide a service / functionality

What is a Function? 3 Main Advantages Code Easier :- They make the program code easier to understand and to maintain Code Reuse :Tried and tested function can be reused by other programs Division of labour : several programmers can divide the workload in a large project by working on different functions for the program

What is a Function? Function Declaration functions must be declared early in the program code, just as it is done for primitive variables It is declared as a prototype

What is a Function? Declaration syntax return-data-type function-name (arguments-list) Where return-data-type can be int string char double void among others

What is a Function? where arguments-list - are values to be passed as arguments from the caller - and these can be of any quantity and data type - they must agree with those specified in the prototype function declaration

What is a Function? Function Prototype declaration Eg void calcsum(); int calcmean(); double calcstdev();

What is a Function? void calcsum(); declares a function named calcsum(); that accepts no argument -- () is empty and returns no value : because type is void

What is a Function? Definition - Appears later in the program code Comprises a repeat of the prototype together with the function body where the function body is a pair of braces { } surrounding the statements that are to be executed when the function is called.

Examples #Include<iostream> using namespace std; //declare the function prototype early void calcsum(); void writetitle(); int sum; int main() { writetitle(); // function calls from main rsum = calcsum(); cout << “The Sum is = “ << rsum << endl ; system(“pause”); return 0; }

// function definition //define the function writetitle() void writetitle() { string dept = “Computer Science”; cout << dept << endl; }

// function definition //define the function calcsum() void calcsum() { int val1 = 20; int val2 = 35; int tot = val1 + val2; cout << “The sum is = “ << tot endln; }

Passing values to functions Functions can be passed values The caller function passes value in the form of arguments to a function that it calls These can be of any quantity and data type But they must agree with those specified in the prototype function declaration

Functions can return a value - the function can return a value of any data-type, as long as it is of the type specified in the function prototype

// Function arguments Note the FUNCTION the Caller FUNCTION Int main() { ................ writeline(); calcsum(); .................... }

// Function arguments #Include<iostream> using namespace std; //declare the function prototype early void calcsum(); void writetitle(); int sum; int main() { writetitle(); // function calls from main calcsum(); system(“pause”); return 0; } //define the function writetitle() void writetitle() string dept = “Computer Science”; cout << dept << endl; //define the function calcsum() void calcsum() int val1 = 20; int val2 = 35; int tot = val1 + val2; cout << “The sum is = “ << tot endln; Pfunction.txt

// Function passing arguments by value Note the Function Prototype arguments and the Function definition arguments // Function Prototype declaration int getmax(int n1, int n2); int getnum();

// Function passing arguments by value // function definition int getnum() { int num cout << “Enter a number”; cin >> num; return num; }

// Function passing arguments by value // function definition int getmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }

// Function passing arguments by value #include<iostream> using namespace std; // function declaration prototype int getnum(); int getmax(int n1 , int n2); //main function int main() { int num1 , num2; num1 = getnum(); num2 = getnum(); // determine the maximum of num1 and num2 cout << “Max number : “ << getmax(num1, num2) << endl; }

// Function passing arguments by value int getnum() { int num cout << “Enter a number”; cin >> num; return num; }

// Function passing arguments by value // function definition int getmax(int n1, int n2) { return (n1 > n2) ? n1 : n2; }

// Function passing arguments by value when arguments are passed to a function it is the value that is passed, not the variable itself