Top Banner
Lecture – 36 to 38 Computer Programming 14 Computer Systems Engineering – Second Semester By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET
47

Computer programming lecture – 36 to 38

Jul 08, 2015

Download

Education

Computer programming lecture – 36 to 38
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer programming lecture – 36 to 38

Lecture – 36 to 38Computer Programming

14 Computer Systems Engineering – Second Semester

By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

Page 2: Computer programming lecture – 36 to 38

Contents

• Function (LL 02)

• Types of Functions (LL 02)

• Pre-Defined Functions (LL 04)

• User-Defined Functions (LL 04)

• Creating User-Defined Functions in C++ (LL 04)

• Function Declaration (LL 04)

• Function Definition (LL 04)

• Function Calling (LL 04)

• Passing Arguments to a Function (LL 04)

• Passing Arguments to a Function By Value (LL 04)LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis

Ali Asghar Manjotho, Lecturer CSE-MUET 2

Page 3: Computer programming lecture – 36 to 38

Contents

• Passing Arguments to a Function By Reference (LL 04)

• Program Examples (LL 04)

LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis

Ali Asghar Manjotho, Lecturer CSE-MUET 3

Page 4: Computer programming lecture – 36 to 38

Function

• Most computer programs that solve real-world problems are much larger than the programs we have done in the class.

• Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or components.

• This technique is called divide and conquer.

• A larger program is created by developing smaller components individually and them assembling them together as whole.

Ali Asghar Manjotho, Lecturer CSE-MUET 4

Page 5: Computer programming lecture – 36 to 38

Function

• A function groups a number of program statements into a unit and gives it a name.

• A function is a group of statements that together perform a task.

• Every C++ program has at least one function, which is main( ).

Ali Asghar Manjotho, Lecturer CSE-MUET 5

Page 6: Computer programming lecture – 36 to 38

Types of Functions

• There are two types of functions:

Ali Asghar Manjotho, Lecturer CSE-MUET 6

Page 7: Computer programming lecture – 36 to 38

Pre-Defined Functions

• Also called as built-in functions.

• The functions which are already built inside the C++ standard library, are called as pre-defined functions.

• You do not need to create them just call them whenever you want to use them.

• Few of the examples of pre-defined functions are:

Ali Asghar Manjotho, Lecturer CSE-MUET 7

Page 8: Computer programming lecture – 36 to 38

Pre-Defined Functions

Ali Asghar Manjotho, Lecturer CSE-MUET 8

Page 9: Computer programming lecture – 36 to 38

Pre-Defined Functions

Ali Asghar Manjotho, Lecturer CSE-MUET 9

Page 10: Computer programming lecture – 36 to 38

User-Defined Functions

• Also called as Programmer-Defined functions.

• The functions which are not the part of C++ standard library.

• The programmer defines the functionality of the functions by themselves.

• Because C++ does not provide every function that you will ever need, you must learn to write your own functions.

• For example there is no any function in C++ that finds the maximum number out of an array or swap two variables with each other.

• For these tasks you need to create your own (User-Defined) functions.

Ali Asghar Manjotho, Lecturer CSE-MUET 10

Page 11: Computer programming lecture – 36 to 38

User-Defined Functions

• A function is like a box (with group of statements inside) which receives some of the inputs, apply processing on them and gives you the output.

• A function can have zero or more inputs.

• A function can have zero or one output.

• The group of statements will define what operation, the function will perform on the inputs.

Ali Asghar Manjotho, Lecturer CSE-MUET 11

FunctionInputs Output

Page 12: Computer programming lecture – 36 to 38

User-Defined Functions

• User-defined functions in C++ are classified into two categories:

1. Value-Returning Functions

Functions that have a return type. These functions return a value of a specific data type using the return statement.

2. Void Functions

Functions that do not have a return type. These functions do not use a return statement to return a value.

Ali Asghar Manjotho, Lecturer CSE-MUET 12

Page 13: Computer programming lecture – 36 to 38

User-Defined Functions

• In first case the function takes some inputs, performs calculations and returns one value as a result. Lets say you want to create a function that receives the radius of the circle and returns you the area of the circle, in this case the function returns one value i.e. the area, hence it is a value returning function.

• In second case the function just does its job but do not return a value. Say, we need to create a function that receives two strings and displays both the strings after concatenating. In this case the function does not perform any calculation and will not return any value.

Ali Asghar Manjotho, Lecturer CSE-MUET 13

Page 14: Computer programming lecture – 36 to 38

Creating User-Defined Functions in C++

• In order to create a user-defined function in C++ you need to provide:

• Function Declaration

• Function Definition

• Function Calling

Ali Asghar Manjotho, Lecturer CSE-MUET 14

Page 15: Computer programming lecture – 36 to 38

Creating User-Defined Functions in C++

• Lets create two functions:

Function 1:

Create a function that receives the radius of the circle and returns you the area of the circle.

Functions 2:

Create a function that receives two strings and displays both the strings after concatenating.

Ali Asghar Manjotho, Lecturer CSE-MUET 15

Page 16: Computer programming lecture – 36 to 38

Function Declaration

• The function declaration just tells the compiler how the function looks like. It includes the function name, the parameter list and the return type.

• The function declaration just tells the compiler that, we are going to create one of the function in this program and it looks like this.

• Function declaration is also called as Function Prototype.

Ali Asghar Manjotho, Lecturer CSE-MUET 16

Page 17: Computer programming lecture – 36 to 38

Function Declaration

Function-1 Declaration:

Ali Asghar Manjotho, Lecturer CSE-MUET 17

Return Type

Function Name Parameter List

Page 18: Computer programming lecture – 36 to 38

Function Declaration

Function-2 Declaration:

Ali Asghar Manjotho, Lecturer CSE-MUET 18

Return Type

Function Name Parameter List

Page 19: Computer programming lecture – 36 to 38

Function Definition

• The function declaration provides the details of the functions. Here we write all the statements that make up a function.

• Function definition tells the compiler, what the function will do.

• It includes, the function name, return type, parameter list and the body of the function.

Ali Asghar Manjotho, Lecturer CSE-MUET 19

Page 20: Computer programming lecture – 36 to 38

Function Definition

Function-1 Definition:

Ali Asghar Manjotho, Lecturer CSE-MUET 20

Return Type

Function Name

Parameter List

Function Body

Page 21: Computer programming lecture – 36 to 38

Function Definition

Function-2 Definition:

Ali Asghar Manjotho, Lecturer CSE-MUET 21

Return Type

Function NameParameter List

Function Body

Page 22: Computer programming lecture – 36 to 38

Function Calling

• Once the function is created, it can be used inside the program by calling it.

• It includes, the function name and argument list.

• Arguments are different then parameters.

• Parameters are the variables that we use while function definition.

• Arguments are the values/variables that we use while calling the function.

Ali Asghar Manjotho, Lecturer CSE-MUET 22

Page 23: Computer programming lecture – 36 to 38

Function Calling

Function-1 Calling:

Ali Asghar Manjotho, Lecturer CSE-MUET 23

Function Name

Argument List

Page 24: Computer programming lecture – 36 to 38

Function Calling

Function-2 Calling:

Ali Asghar Manjotho, Lecturer CSE-MUET 24

Function Name

Argument List

Page 25: Computer programming lecture – 36 to 38

Passing Arguments to a Function

• While calling the function, we have to specify all the arguments of the function for each of the parameter.

• In a function the arguments can be passed “By Value” or “By Reference”.

Ali Asghar Manjotho, Lecturer CSE-MUET 25

Page 26: Computer programming lecture – 36 to 38

Passing Arguments to a Function By Value

• When we pass the arguments to a function by value, the actual variables are not affected.

• Here, the copy of the variable is passed.

• Consider the following program:

Ali Asghar Manjotho, Lecturer CSE-MUET 26

Page 27: Computer programming lecture – 36 to 38

Passing Arguments to a Function By Value

Ali Asghar Manjotho, Lecturer CSE-MUET 27

Page 28: Computer programming lecture – 36 to 38

Passing Arguments to a Function By Value

Ali Asghar Manjotho, Lecturer CSE-MUET 28

Page 29: Computer programming lecture – 36 to 38

Passing Arguments to a Function By Reference

• When we pass the arguments to a function by reference, the actual variables are affected.

• Here, the reference (address) of the variables is passed.

• Consider the following program:

Ali Asghar Manjotho, Lecturer CSE-MUET 29

Page 30: Computer programming lecture – 36 to 38

Passing Arguments to a Function By Reference

Ali Asghar Manjotho, Lecturer CSE-MUET 30

Page 31: Computer programming lecture – 36 to 38

Passing Arguments to a Function By Reference

Ali Asghar Manjotho, Lecturer CSE-MUET 31

Page 32: Computer programming lecture – 36 to 38

Program ExamplesFunctions in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 32

Page 33: Computer programming lecture – 36 to 38

Program Example 01

Problem Statement:

Create a function that receives two integer numbers and returns the maximum number out of them.

Ali Asghar Manjotho, Lecturer CSE-MUET 33

Page 34: Computer programming lecture – 36 to 38

Program Example 01

Ali Asghar Manjotho, Lecturer CSE-MUET 34

Page 35: Computer programming lecture – 36 to 38

Program Example 01

Ali Asghar Manjotho, Lecturer CSE-MUET 35

Page 36: Computer programming lecture – 36 to 38

Program Example 02

Problem Statement:

Create a function that receives two integer numbers and swaps them.

Ali Asghar Manjotho, Lecturer CSE-MUET 36

Page 37: Computer programming lecture – 36 to 38

Program Example 02

Ali Asghar Manjotho, Lecturer CSE-MUET 37

Page 38: Computer programming lecture – 36 to 38

Program Example 02

Ali Asghar Manjotho, Lecturer CSE-MUET 38

Page 39: Computer programming lecture – 36 to 38

Program Example 03

Problem Statement:

Create a function that receives two floating point number and an operator (+ , -, / , * ). The function returns the result of the operation.

Ali Asghar Manjotho, Lecturer CSE-MUET 39

Page 40: Computer programming lecture – 36 to 38

Program Example 03

Ali Asghar Manjotho, Lecturer CSE-MUET 40

Page 41: Computer programming lecture – 36 to 38

Program Example 03

Ali Asghar Manjotho, Lecturer CSE-MUET 41

Page 42: Computer programming lecture – 36 to 38

Program Example 04

Problem Statement:

Create two functions, first receives temperature in Fahrenheit and converts it in to Celsius. The second receives temperature in Celsius and converts it in to Fahrenheit

Ali Asghar Manjotho, Lecturer CSE-MUET 42

Page 43: Computer programming lecture – 36 to 38

Program Example 04

Ali Asghar Manjotho, Lecturer CSE-MUET 43

Page 44: Computer programming lecture – 36 to 38

Program Example 04

Ali Asghar Manjotho, Lecturer CSE-MUET 44

Page 45: Computer programming lecture – 36 to 38

Program Example 05

Problem Statement:

Create two functions, first receives integer array and returns maximum item. The second receives integer array and returns minimum item.

Ali Asghar Manjotho, Lecturer CSE-MUET 45

Page 46: Computer programming lecture – 36 to 38

Program Example 05

Ali Asghar Manjotho, Lecturer CSE-MUET 46

Page 47: Computer programming lecture – 36 to 38

Program Example 05

Ali Asghar Manjotho, Lecturer CSE-MUET 47