Lecture 9 Oct 2, 02. Functions ► Function is a self contained block of program that performs a coherent task of some kind. ► Eg: Consider an automobile,

Slides:



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

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)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Functions Prototypes, parameter passing, return values, activation frams.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
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.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
1 Lecture 10 Chapter 7 Functions Dale/Weems/Headington.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 4 Summation.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Functions g g Data Flow g Scope local global part 4 part 4.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
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.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
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.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 13 Thanks for lecture slides: Prentice Hall, Inc., 2. C++
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Lecture 10 Oct 7, 02. Pass by value and reference ► Calling a function and passing the value of a variable as an argument is called pass by value. The.
Introduction to Programming
Basic concepts of C++ Presented by Prof. Satyajit De
Functions and an Introduction to Recursion
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSCI 161: Introduction to Programming Function
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Value returning Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Lecture 12 Oct 16, 02.
CS150 Introduction to Computer Science 1
Functions and an Introduction to Recursion
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
TOPIC: FUNCTION OVERLOADING
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Presentation transcript:

Lecture 9 Oct 2, 02

Functions ► Function is a self contained block of program that performs a coherent task of some kind. ► Eg: Consider an automobile, it consists of various modules like the engine, transmission, braking, the body so on…. ► Each of these modules are linked and placed under the control of the driver, which can be compared to the main program. ► Each module can be considered a function. Each module can be individually constructed, tested, and found to be free of defects before it is installed in the final product. ► A C++ program must contain a main() function. In addition to this required function, C++ programs may also contain any number of additional functions. These additional functions are under the main() function.

Simple Program with a function Eg 1 ► #include ► #include void message(); // function prototype declaration void message(); // function prototype declaration int main() int main() { message(); // calling the function message message(); // calling the function message cout<<“I am in main() function”<<endl; cout<<“I am in main() function”<<endl; return 0; return 0; } // the additional function message // the additional function message void message() // the function header line void message() // the function header line { cout<<“I am in function message”<<endl; // body of function cout<<“I am in function message”<<endl; // body of function } output: output: I am in function message I am in function message I am in main() function I am in main() function

Example 2 ► #include ► #include int calsum(int, int, int) // function prototype declaration int calsum(int, int, int) // function prototype declaration int main() int main() { int a,b,c,sum; int a,b,c,sum; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here sum=calsum(a,b,c); // function call done here return 0; return 0; } // The function calsum // The function calsum int calsum(int x, int y, int z) // function header int calsum(int x, int y, int z) // function header { int d; int d; d = x+y+z; d = x+y+z; return(d); return(d); }

Example 3 ► #include ► #include float calsum(int, float, int) // function prototype declaration float calsum(int, float, int) // function prototype declaration int main() int main() { int a,c; int a,c; float b,sum; float b,sum; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here sum=calsum(a,b,c); // function call done here return 0; return 0; } // The function calsum // The function calsum float calsum(int x, float y, int z) // function header float calsum(int x, float y, int z) // function header { float d; float d; d = x+y+z; d = x+y+z; return(d); return(d); }

Example 4 ► #include ► #include float calsum(int, float, int) // function prototype declaration float calsum(int, float, int) // function prototype declaration int main() int main() { int a,c; int a,c; float b; float b; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; cout<<“Sum of three numbers is “<<calsum(a,b,c)<<endl; // function call done diff cout<<“Sum of three numbers is “<<calsum(a,b,c)<<endl; // function call done diff return 0; return 0; } // The function calsum // The function calsum float calsum(int x, float y, int z) // function header float calsum(int x, float y, int z) // function header { float d; float d; d = x+y+z; d = x+y+z; return(d); return(d); }

Example 5 ► #include ► #include void calsum(int, float, int) // function prototype declaration void calsum(int, float, int) // function prototype declaration int main() int main() { int a,c; int a,c; float b; float b; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; calsum(); calsum(); return 0; return 0; } // The function calsum // The function calsum void calsum(int x, float y, int z) // function header void calsum(int x, float y, int z) // function header { float d; float d; d = x+y+z; d = x+y+z; cout<<“ The sum is “<<d; // no return required cout<<“ The sum is “<<d; // no return required }

Function concepts studied ► Function definition return data type function name( parameters) return data type function name( parameters) ► Function prototype declaration return data type function name ( data type list) return data type function name ( data type list) ► Calling a function ► message(); sum=calsum(a,b,c); sum=calsum(a,b,c); cout<<“ sum of three numbers is “<<calsum(a,b,c); cout<<“ sum of three numbers is “<<calsum(a,b,c); ► Returning a value to the function Not applicable for void data type. Applicable for all other datatypes Not applicable for void data type. Applicable for all other datatypes