Programming fundamentals 2 Chapter 2:Function

Slides:



Advertisements
Similar presentations
Revision.
Advertisements

1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
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.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
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.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Introduction to Computers and C++ Programming
Fucntions in C++ Malik Jahan Khan
REPETITION CONTROL STRUCTURE
Function Topic 4.
Chapter 5 Function Basics
Functions and an Introduction to Recursion
Variables A piece of memory set aside to store data
Engineering Problem Solving with C++, Etter/Ingber
Chapter 5 Functions.
Introduction to C++ October 2, 2017.
לולאות קרן כליף.
Introduction to Functions
Lab 1 Introduction to C++.
Function User defined function is a code segment (block) that perform an specific action and may return a value. Function Definition: Return_DT F_name.
CSC1201: Programming Language 2
Compound Assignment Operators in C++
Chapter 5 Function Basics
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Programming Funamental slides
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
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.
CS150 Introduction to Computer Science 1
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 6: User-Defined Functions I
The Function Prototype
Functions and an Introduction to Recursion
Statements and flow control
Fundamental Programming
CSC1201: Programming Language 2
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
CS1201: Programming Language 2
TOPIC: FUNCTION OVERLOADING
Introduction to Functions
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Functions Chapter No. 5.
Presentation transcript:

Programming fundamentals 2 Chapter 2:Function Miss:Hanan Hardam

Top-Down Algorithm Design Topics to cover here: Top-Down Design approach Structured Programming Sub-algorithms (a breadth look) Functions in C++

Top-Down Algorithm Design 1) Top-Down Design The problem is divided into its major sub-problems Solve the sub-problems to derive the solution to the original problem. 2) Structured Design: Dividing the problem into smaller sub-problems is called “structured design”, “top-down design”, “step-wise refinement”, and “modular Programming” 3) Structured Programming: The process of implementing a structured design is called “structured programming”

Structured Programming & Structure Chart The main goal of structured programming is to write error-free code to reuse any possible code that has already been written and tested (this is called reusability) to have many sub-problems that are related.

Sub-algorithms A sub-algorithm is a block of instructions that is executed when it is called from some other point of the algorithm. The top-down algorithm design needs - to write the sub-algorithm definitions - to write an algorithm that calls the sub-algorithms (i.e. includes a CALL statement for each one) . Sub-algorithms are of two type: - Sub-algorithms that do not return a value - Sub-algorithms that return a value * The sub-algorithm is called function in C++.

The Sub-algorithm Definition a) Definition of a sub-algorithm that does not return a value 1- Sub-algorithm without arguments: SUBALGORITHM subalgorithm-name ( ) Statements END subalgorithm-name where, ( ) is the empty list.

The Sub-algorithm Definition 2- Sub-algorithm with arguments: SUBALGORITHM subalgorithm-name (parameter-list) Statements END subalgorithm-name where, Parameter-list is a list that contains one or more parameters that are passed to the sub-algorithm.

The Call to a Sub-algorithm The call to a sub-algorithm that does not return a value is given in the CALL statement which has the following syntax: subalgorithm-name (Actual parameters) Examples: draw_circle ( ) sum(4,7) sum(x,y)

The Call to a Sub-algorithm Interpretation of the sub-algorithm call Suppose that algorithm Figure calls the sub-algorithm draw_circle using the call statement CALL draw_circle ( ) Then the flow of control between sub-algorithms will be as follows (see the next diagram): The flow of control in algorithm Figure stops on this statement to initiate the execution of the sub-algorithm draw_circle after this sub-algorithm has finished executing, the next statement in the calling algorithm will be executed.

The Call to a Sub-algorithm Algorithm Figure Sub-algorithm draw_circle ALGORITHM Figure ------- CALL draw_circle ( ) ------ END Figure SUBALGORITHM draw_circle ( ) ------ END draw_circle call return

Example 1: Syntax ( in Algorithm ): Syntax ( in C++ ): SubAlgorithm print_box () output “* * * * * * *\n” output “* “ , 50 , “ * \n” End print_box Algorithm Test begin print_box () output “\n” End Test void print_box () { cout << “* * * * * * *\n” ; cout << “* “ << 50 << “ * \n” ; } void main() print_box (); cout<<endl;

Example 2: Syntax ( in Algorithm ): Syntax ( in C++ ): SubAlgorithm print_box ( n) output “* * * * * * *\n” output “* “ , n , “ * \n” End print_box Algorithm Test Begin Output “ Enter integer number" Input a print_box (a) print_box (5) End Test void print_box (int n) { cout << “* * * * * * *\n” ; cout << “* “ << n << “ * \n” ; } void main() int a; cout<<"\n Enter integer number"; cin>>a; print_box (a); cout<<endl; print_box (5);

The Sub-algorithm Definition b) Definition of a sub-algorithm that returns a value: 1- Sub-algorithm without arguments: ftype SUBALGORITHM subalgorithm-name ( ) Statements END subalgorithm-name Notes: - ftype: is any data type that the result of a subalgortihm can have. If the subalgorithm does not return any result, then no type is specified. -( ) empty list. - If the subalgorithm has a type, then the Statements in the body of the subalgorithm should have return statement, usually it is the last statement.

The Sub-algorithm Definition b) Definition of a sub-algorithm that returns a value: 2- Sub-algorithm without arguments: ftype SUBALGORITHM subalgorithm-name (parameter-list) Statements END subalgorithm-name Notes: - ftype: is any data type that the result of a subalgortihm can have. If the subalgorithm does not return any result, then no type is specified. - parameter-list: includes one or more arguments. - If the subalgorithm has a type, then the Statements in the body of the subalgorithm should have return statement, usually it is the last statement.

The Call to a Sub-algorithm The call to a sub-algorithm that returns a value is given as follows: - The name of the sub-algorithm is given within the OUTPUT statement assignment statement Examples: result  Sum (x, y ) output Sum (x, y )

Example: Syntax ( in Algorithm ): Syntax ( in C++ ): INT SubAlgorithm sum () x5 y7 return x+y End sum Algorithm Test Begin Output “sum=“ ,sum() a  sum() Output “sum=“ , a End Test int sum () { int x=5, y=7 ; return x+y ; } void main() int a; cout<<“sum=“<<sum()<<endl; a=sum(); cout<<“sum=“<<a<<endl;

Example: Syntax ( in Algorithm ): Syntax ( in C++ ): INT SubAlgorithm Rect_area (L, W) a  L * W return a End Rect_area Algorithm Test Begin Output “ Enter 2 integer number" Input a ,b Output “area==“, Rect_area(a,b) z  Rect_area(3,2) Output “area=“ , z End Test int Rect_area (int L, int W) { int a ; a = L * W ; return a ; } void main() int a,b,z; cout<<"\n Enter 2 integer number"; cin>>a>>b; cout<<“area==“<<Rect_area(a,b)<<endl; z=Rect_area(3,2); cout<<“area=“<<z<<endl;

Example #include <iostream> using namespace std; int Max (int Value1, int Value2) { if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl; Function Definition Calling the function in an expression like cout<<, condition, assignment statements

Example #include <iostream> using namespace std; int Max (int Value1, int Value2) { if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl;

Example #include <iostream> using namespace std; int Sum (int A, int B) { return (A+B); } void main() { int N1, N2, S; cout<<"\n Please Enter N1 and N2: "; cin>>N1>>N2; S = Sum(N1,N2); cout<<"\nSum= "<<S<<endl;

Example #include <iostream> using namespace std; bool Positive (int Num) { if (Num > 0) return true; else return false; } void main() { int Number; cout<<"\nEnter Number: "; cin>> Number; if (Positive(Number)) cout<<"\n the number is positive"; cout<<"\n the number is negative"; cout<<endl;

Example #include <iostream> using namespace std; float Area (int R) { return (3.14 * R * R ); } void main() { int Radius; cout<<"Enter the Redius: "; cin>>Radius; cout<<"\nCircle Area is: "<<Area(Radius); cout<<endl;

Example #include <iostream> using namespace std; long Power(int Base, int Exp) { int M=1; for(int i=1; i<=Exp; i++) M*=Base; return M; } void main() { int B, E; cout<<"\nEnter Base: "; cin>>B; cout<<"\nEnter Exponent: "; cin>>E; cout<<"\n Result= "<<Power(B,E); cout<<endl;

Example #include <iostream> using namespace std; long Fact (int Num) { int F = 1, i = Num; while (i>=1){ F *= i; i--; } return F; } void main() { int Number; cout<<"Enter an integer number: "; cin>>Number; cout<<endl<<Number<<"!= "<<Fact(Number); cout<<endl;

Example No Return Statement #include <iostream> using namespace std; void Print(char Ch, int n) { for (int i=1; i<=n; i++) cout<<Ch; cout<<endl; } void main() { char Sym; int Number; cout<<"\nEnter the Symbol: "; cin>>Sym; cout<<"\nHow many times: "; cin>>Number; Print(Sym,Number); No Return Statement

Example #include <iostream> using namespace std; int Mul(int V1, int V2) { return V1 * V2; } void Result() { cout<<"\n5 x 9 = "<<Mul(5,9); cout<<"\n4 x 7 = "<<Mul(4,7); cout<<"\n6 x 4 = "<<Mul(6,4)<<endl; } void main() { Result() ; }