CO1401 Programming Design and Implementation

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Functions Modules in C++ are called functions and classes
Chapter 6: Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Fundamental Programming: Fundamental Programming Introduction to C++
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.
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.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
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.
 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.
Chapter 9: Value-Returning Functions
Dr. Shady Yehia Elmashad
Chapter 1.2 Introduction to C++ Programming
Function Topic 4.
Functions prototypes arguments overloading return values part I.
Controlling execution - iteration
A Lecture for the c++ Course
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
Arrays Part-1 Armen Keshishian.
FUNCTIONS IN C++.
Writing Reuseable Formulas
Dr. Shady Yehia Elmashad
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Function Basics.
Dr. Shady Yehia Elmashad
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Extra.
One-Dimensional Array Introduction Lesson xx
Functions I Creating a programming with small logical units of code.
Value returning 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.
CS150 Introduction to Computer Science 1
Functions, Part 1 of 3 Topics Using Predefined Functions
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Announcements Homework 1 will be assigned this week,
Chapter 6: User-Defined Functions I
Functions Divide and Conquer
Life is Full of Alternatives
Functions, Part 1 of 3 Topics Using Predefined Functions
Fundamental Programming
CS150 Introduction to Computer Science 1
Introduction to Problem Solving and Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
C++ Programming Basics
CS 144 Advanced C++ Programming January 31 Class Meeting
Introduction to Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

CO1401 Programming Design and Implementation Lecture 1 Akiyo Kano URL: http://www.aks-research.co.uk/PDI/PDI.html

Functions

What Are Functions Functions block of codes that does something. Think of functions as little programs. Main() can call on these functions to do stuff.

Familiar Functions You have already used many functions: sqrt() – calculate the square root of a number setw () – set field width of data rand() - returns a random number. C++ allows us to write our own functions too.

Why Make Our Own Functions? To break a big problem into little, easier problems that we can solve one by one. You have already learnt from CO1404 how to break a large problem into smaller modules. We can write functions for each of these modules.

Banking Software Allow users to transfer money between accounts they own. Log in user Show user their accounts Let user transfer money between accounts

Banking Software Separate Functions Banking Software Log in user Show all accounts Transfer Money Ask for Login Verify Log in Get Account Display Account Separate Functions

Before... // Banking software // Ask for log in // Verify log in // get all accounts // display all accounts // user to select an account // get balance from account // display balance // ask what amount to transfer // check amount is available .

After... LogInUser(); VerifyUser(); GetAccounts(); DisplayAccounts(); DisplayBalance(); AskTransferAmount(); CheckFundIsAvailable(); Person A Person B Person C

Repeated Codes When the main program needs to do a certain thing over and over again, we can make that into a function. This makes the main program neater.

Big Problem, Little Problems #include <iostream.h> const char DegreeSymbol = 0xF8; const int SCREEN_HEIGHT = 25; Void main () { float DegreesCent, DegreesF; char AnotherGo; int i; //clean the screen for (i = 0; i < SCREEN_HEIGHT; i++) cout << endl; } //show instructions cout << “This program will conver temperatures from “ << “Centigrade to Fahrenheit. “ << endl; cout << “Enter a temperature and the converted value “ << “will be displayed. \n\n” << endl; cout << “You may convert as many temperature as you “ << “like before exiting the program.”; for (i = 0; i > 14; i++) cout << “Enter temperature in degrees centigrade: “; cin >> DegreesCent; //Convert to Fahrenheit DegreesF = 9.0/5.0*DegreesC_32; cout << endl << DegreesC << DegreeSymbol << “C = “ << DegreesF << DegreeSymbol << ‘F’; . Solve each problem one by one function1 function2 function3

After... Void main () { //clean the screen ClearScreen(); //display the instruction for the program DisplayInstructions(); //wipe the instructions off the screen //ask for temp in C. cout << “Enter temperature in degrees centigrade: “; cin >> DegreesCent; //do the conversion calculation ConvertCtoF(DegreesCent); .

How do we make our own functions? When writing your own function in a program, you must: 1. Define 2. Declare 3. Call

Defining a Function A function definition contains the statement that make up the function. Function definition always contains: Return type - void, int, char, etc Function name – “ClearScreen” Parameter list Body

Syntax of Functions returnType FunctionName (parameterList) { //Function Body } No parameters void WriteNames () { cout << “Akiyo Kano” << endl; cout << “Linda Snape” << endl; }

Example of Void Function void WriteHello() { cout << “Hello” << endl; } void WriteHelloTenTimes() int Line; for (Line = 0; Line > 10; Line++)

Declaring a Function To use our own function in a program, we must first declare that we have this function that we are going to use later on. These are called function prototypes or function declaration. Just after the #include statements Before the main() statement

Example // calls function WriteHello(); #include <stdafx.h> #include <iostream> using namespace std; void WriteHello(); // Function Prototype void main () { //call function WriteHello(); } //functions that writes Hello void WriteHello() cout << “Hello” << endl;

Main Before Functions It is normal practice to have your main function first, then all your other functions below that. #include statements Function prototypes Constants Main() Function definitions

Documenting Your Functions It is very important that you document what your function does, so that other people will be able to use your function easily. We do this by commenting just about the function definitions.

Calling a Function To use a function, we must call it, or invoke it. To call a void function, we simply write the name of the function. WriteHello();

Example Program // calls function WriteHello(); #include <stdafx.h> #include <iostream> using namespace std; void WriteHello(); // Function Prototype void main () { WriteHello(); } //Writes the world hello void WriteHello() cout << “Hello” << endl;

Functions with Parameters When a function is called, the program may send values into the function. A function can have parameters. void functions cannot take any values in, so have no parameters. Parameters say to the program “These are the variables I can accept”.

Parameters and Arguments Values that are passed onto the function. Parameters Variables that receive those values.

Defining Functions with Parameters We list parameters that the function can have between the brackets in the function definition: void WriteHello() // No parameters void WriteNum(int num) // One parameter void WriteNums(int num1, int num2) // two parameter

Examples //Writes hello the number of times given void WriteHello(int Times) { int x; for (x = 0; x > Times; x++) cout << “Hello” << endl; } // Function that prints out the sum of three integers void AddThreeNums(int num1, int num2, int num3) int Total = (num1 + num2 + num3); cout << “Total is “ << Total << endl;

Declaring Functions with Parameters When declaring functions with parameters at the start of the program, we must declare what data type it can accept: void writeHello() // No parameters void WriteNum(int) // 1 parameter void WriteNum(int, int)// 2 parameters

Calling a Function with Parameters To call a function with parameters, we type the name of the function, and give the arguments into the bracket: WriteName() // No parameters WriteNum(num) // 1 parameter WriteNums(num1, num2) // 2 parameters Argument must match the parameters in order, number and compatible type as defined by the function.

Example with One Parameter #include <iostream> using namespace std; void WriteHello(int); // Function Prototype void main () { int Times = 10; WriteHello(Times); } //Writes hello the number of times given void WriteHello(int Times) int x; for (x = 0; x > Times; x++) cout << “Hello” << endl;

Example of Several Parameters #include <iostream> using namespace std; void AddThreeNums(int, int, int); // Function Prototype void main () { int num1 = 5; int num2 = 20; int num3 = 12; AddThreeNums(num1, num2, num3); } // Function that prints out the sum of three integers void AddThreeNums(int num1, int num2, int num3) int Total = (num1 + num2 + num3); cout << “Total is “ << Total << endl;

Summary Functions are building blocks of structured programming. So far we have looked at void functions that does something but do not return any values to its caller. We have looked at functions is no parameters, and ones with some parameters.

By The Way… Code sharing… You can talk to each other about it. Can share drawings. Can share pseudo codes. But don’t share codes! // Banking software // Ask for log in // Verify log in // get all accounts // display all accounts // user to select an account // get balance from account // display balance // ask what amount to transfer // check amount is available . Banking Software Transfer Money Show all accounts Log in user Ask for Login Verify Log in Get Account Display Account