CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Slides:



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

Chapter 6: User-Defined Functions I
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 7: User-Defined Methods
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
User-Defined Functions II TK1914: C++ Programming.
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.
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.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Chapter 9: Value-Returning Functions
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
User-Defined Functions
User Defined Functions
User Defined Functions
FUNCTION CSC128.
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

C ONTENTS Introduction User-defined function Function without parameter Calling Functions Function with parameter Function with return value 2

INTRODUCTION Program without function This program has one long, complex function containing all of the statements necessary to solve a problem void main() { statement; } 3

INTRODUCTION Program with function In this program the problem has been divided into smaller problems, each of which is handled by a separate function int main() { statement1; function1(); function2(); function3(); } void function1() { statement; } void function2() { statement; } void function3() { statement; } 4

USER-DEFINED FUNCTION There are 3 elements related to functions: Function prototype/declaration Function definition Function call

Function Declaration/Prototype A form for called function must be declared first: function_type function_name(parameter_list); parameter_list – consists of data type for every parameter which is a function input. function_type function_name( ); USER-DEFINED FUNCTION 6

Function Definition Properties that form the function definition: Name of the function Number of parameters Data type of each parameter Type of the function Code required to accomplish the task (the body of the function) Syntax: function Type : type of the value returned by the function (data type) formal parameter list: a variable declared in the function heading; can be empty USER-DEFINED FUNCTION 7

Function Call The syntax for a function call is: A call to a value-returning function with an empty formal parameter list is: functionName() Example: USER-DEFINED FUNCTION 8

There are 2 ways to declare a function: i. through global variable ii. through local variable using parameter TYPES OF VARIABLE AND ITS SCOPE 9

T HROUGH GLOBAL VARIABLE Global variable is a variable that can be declared outside of the function (outside declaration). By declaring variable as global, variables can be used in many functions. 10

T HROUGH LOCAL VARIABLE Local variable is a variable that must be declared in any block. Its scope starts at its declaration point until the end of the block. It only can be used in that function. Local variable belongs to the function where it is declared and can’t be shared or used by any other function including main function. If the same variable name is declared as a local variable in few functions, the variable wil be assumed as a different one. 11

12 Example: #include void display(); float number = 50.6; // a global variable named number int main() { cout << “The value of number is: ”<< number << endl; display(); return 0; } void display() { float number= 34.7; // a local variable named number cout<<“The number is:”<<number<<endl; } TYPES OF VARIABLE AND ITS SCOPE

F UNCTION WITHOUT PARAMETER void function without parameters : Function definition syntax: void is a reserved word Function call syntax: 13

F UNCTION WITH PARAMETER void function with parameters : Function definition syntax: 14

Example: F UNCTION WITH PARAMETER 15

A formal parameter receives a copy of the content of corresponding actual parameter A value parameter - a formal parameter that receives a copy of the content of the corresponding actual parameter A reference parameter - a formal parameter that receives the location (memory address) of the corresponding actual parameter F UNCTION WITH PARAMETER 16

Value parameters : If a formal parameter is a value parameter The value of the corresponding actual parameter is copied into it The value parameter has its own copy of the data During program execution, the value parameter manipulates the data stored in its own memory space F UNCTION WITH PARAMETER 17

Reference parameters : If a formal parameter is a reference parameter It receives the address and stores the corresponding actual parameter During program execution to manipulate the data, the address stored in the reference parameter directs it to the memory space of the corresponding actual parameter Pass one or more values from a function Change the value of the actual parameter F UNCTION WITH PARAMETER 18

F UNCTION WITH PARAMETER 19

F UNCTION WITH PARAMETER 20

Cont’d F UNCTION WITH PARAMETER 21

F UNCTION WITH PARAMETER 22

23 FUNCTION WITH RETURN VALUE Void functions: do not have a return type Value-returning functions: have a data type A function needs to return a value when the function type is int, double, float or char. Once the function computes the value, the function returns the value via the return statement except void data type for function. The syntax of the return statement is: When a return statement executes Function immediately terminates Control goes back to the caller

24 Example : FUNCTION WITH RETURN VALUE

USER-DEFINED FUNCTION Example : Example

26 Flow of Execution Execution always begins at The first statement in the function main no matter where main is placed in the program Other functions are executed only when they are called Function prototypes appear before any function definition The compiler translates these first The compiler can then correctly translate a function call

27 EXERCISE Write a program to calculate grade. The program has 3 functions: 1.main a. get the course score b. print the course grade 2.getScore a. prompt the user for the input b. get the input c. print the course score 3.printGrade a. calculate the course grade b. print the course grade

28 EXERCISE Determine the output for the program below: #include void test(int first, int second); int main() { int num = 5; test(24, num); cout<<num<<endl; test(num, num); cout<<num<<endl; test(num*num, num); cout<<num<<endl; test(num+num, num); cout<<num<<endl; return 0; } void test(int first, int second) { int third; third = first + second * second + 2; first = second - first; second = 2 * second; cout<<first<<" " << second <<“ "<<third<<endl; }

29 EXERCISE Determine the output for the program below: You are required to write a function-based program that will calculate area of a circle. There will be only ONE input value respectively (radius). Area of a circle is calculated as: area = radius * radius * (use predefined function!) Suggested name for each function:

30 EXERCISE - Answers Write a program to calculate grade. The program has 3 functions: Example: #include void getScore(); void printGrade(); void main() { getScore(); printGrade(); } void getScore() { cout<<“Enter score:”; cin>>score; cout<<“The score is:”<<score<<endl; }

31 EXERCISE - Answers Write a program to calculate grade. The program has 3 functions: Example: void printGrade() { if (score>=80) cout<<“A”<<endl; if (score>=60) cout<<“B”<<endl; if (score>=50) cout<<“C”<<endl; if (score<50) cout<<“F”<<endl; }

32 Determine the output for the program below: EXERCISE - Answers OUTPUT:

33 EXERCISE Determine the output for the program below: You are required to write a function-based program that will calculate area of a circle. There will be only ONE input value respectively (radius). Area of a circle is calculated as: area = radius * radius * (use predefined function!) Suggested name for each function: