Presentation is loading. Please wait.

Presentation is loading. Please wait.

CO1401 Programming Design and Implementation

Similar presentations


Presentation on theme: "CO1401 Programming Design and Implementation"— Presentation transcript:

1 CO1401 Programming Design and Implementation
Lecture 1 Akiyo Kano URL:

2 Functions

3 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.

4 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.

5 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.

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

7 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

8 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 .

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

10 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.

11 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

12 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); .

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

14 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

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

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

17 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

18 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;

19 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

20 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.

21 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();

22 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;

23 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”.

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

25 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

26 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;

27 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

28 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.

29 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;

30 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;

31 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.

32 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


Download ppt "CO1401 Programming Design and Implementation"

Similar presentations


Ads by Google