0% found this document useful (0 votes)
10 views

Lecture 02 - Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 02 - Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

3/9/2022

“while (noSuccess) { tryAgain(); if (dead) break; }”


- Unknown
Function: Modules of Program
Programmers use segments of earlier programs to construct new
CSE102 programs
• Documentation is very important

Computer Programming with C • Use of predefined functions


• Top-down stepwise refinement
• Major steps = modules of program

2021-2022 Spring Semester


Top-Down Design with Functions
© 2015-2022 Yakup Genç

March 2022 CSE102 Computer Programming 2

Case Study: Circle Case Study: Circle


Problem: Compute and display the area and the circumference of a circle Implementation: The following slides contains the initial program
• Analysis:
• Input: radius (double)
• Outputs: area and circumference (double)
• Relationship: ???
• Design:
1. Get the radius
2. Calculate the area
3. Calculate the circumference
4. Display the area and the circumference
• Some steps requires refinement

March 2022 CSE102 Computer Programming 3 March 2022 CSE102 Computer Programming 4

1
3/9/2022

Outline of Program Circle Program Circle

March 2022 CSE102 Computer Programming 5 March 2022 CSE102 Computer Programming 6

Outline of Program Circle Case Study: Weight of Washers


• Here, we will use the solution of the previous case study
• Problem: Manufacturer of flat washers needs to estimate shipping
cost. They need to compute the weight of a specifies quantity of flat
washers
• Analysis:
• Weight is volume times density of the material
• Volume is the rim area times thickness
• Rim area is calculated as in the next slide
• Inputs: diameters, thickness, density, quantity
• Outputs: weight
• Relationships: ??

March 2022 CSE102 Computer Programming 7 March 2022 CSE102 Computer Programming 8

2
3/9/2022

Computing Area of a Flat Washer Case Study: Weight of Washers


• Design:
• Initial Algorithm: ??
• Implementation:
• next

March 2022 CSE102 Computer Programming 9 March 2022 CSE102 Computer Programming 10

Program Washer Program Washer (cont’d)

March 2022 CSE102 Computer Programming 11 March 2022 CSE102 Computer Programming 12

3
3/9/2022

Program Washer (cont’d) Library Functions


• Software engineering:
• Goal: writing error-free codes
• Use well tested existing codes: code reuse
• Use predefined functions
• EX: sqrt function in math library
• Use it as a black box
y = sqrt(x);
• EX: printf and scanf in stdio library

March 2022 CSE102 Computer Programming 13 March 2022 CSE102 Computer Programming 14

Function sqrt as a “Black Box” Square Root Program

March 2022 CSE102 Computer Programming 15 March 2022 CSE102 Computer Programming 16

4
3/9/2022

Square Root Program (cont’d) Math Library

March 2022 CSE102 Computer Programming 17 March 2022 CSE102 Computer Programming 18

Math Library Library Functions


• Example: Compute the roots of a quadratic equation
−𝑏 ± 𝑏2 − 4𝑎𝑐
𝑥, =
2𝑎
• Example: Compute the length of the third side of a triangle

a2 = b2 + c2 – 2bc cos α

March 2022 CSE102 Computer Programming 19 March 2022 CSE102 Computer Programming 20

5
3/9/2022

User-defined Functions Case Study: Simple Diagrams


• Example: area of a circle • Problem: Draw simple diagrams on your screen
area = find_area(radius); • Ex: house, person
• Analysis: Basic components
• Circle
• Example: circumference of a circle • Parallel lines
circum = find_circum(radius); • Base line
• Intersecting lines
• Design: Divide the problem into three subproblems
• Example: rim area calculation • Draw a circle
rim_area = find_area(edge_radius) - find_area(hole_radius); • Draw a triangle
• Draw intersecting lines
• Further refinement in triangle – see following structure chart

March 2022 CSE102 Computer Programming 21 March 2022 CSE102 Computer Programming 22

Structure Chart for Drawing a Stick Figure Function Prototypes and Main Function

March 2022 CSE102 Computer Programming 23 March 2022 CSE102 Computer Programming 24

6
3/9/2022

User Defined Functions User Defined Functions


• Function prototype • Function call
• Functions should be defined before they are used • Calling a function
• Insert the whole function definition
• Insert the function prototype
• Defines function_name (arguments);
• Data types of the function
• Function name
• Arguments and their types
• Ex:
draw_circle();
function_type function_name (argument types); printf(“%d”, year);

• Ex:
void draw_circle(void);

March 2022 CSE102 Computer Programming 25 March 2022 CSE102 Computer Programming 26

User Defined Functions Function draw_circle


• Function definition
• Defines the operation of a function
• Similar to main function

function_type function_name (argument list)


{
local declerations
executable statements
}

• Function heading: similar to function prototype


• Function body: enclosed in braces

March 2022 CSE102 Computer Programming 27 March 2022 CSE102 Computer Programming 28

7
3/9/2022

Function draw_triangle Program to Draw a Stick Figure

March 2022 CSE102 Computer Programming 29 March 2022 CSE102 Computer Programming 30

Program to Draw a Stick Figure Flow of Control

• Compiling the program:


• Function prototypes: compiler knows the functions
• enables compiler to translate function calls
• Function definition: translates the code of the function
• Allocates memory needed
• Function call: Transfers of the control to the function
• End of the function: Transfer of the control back to the calling statement
• Releases the local memory

March 2022 CSE102 Computer Programming 31 March 2022 CSE102 Computer Programming 32

8
3/9/2022

Flow of Control Advantages of Functions


• For team of programmers:
• Dividing programming tasks to the programmers
• Procedural abstraction
• Move the details of the operation to the functions
• Focus on the main operations
• Code reuse
• In a program
• In other programs
• Well tested functions

March 2022 CSE102 Computer Programming 33 March 2022 CSE102 Computer Programming 34

Function instruct Functions with Input Arguments


• Functions are building blocks to construct large programs
• Like Lego blocks

• Arguments:
• to carry information to functions : input arguments
• to return multiple results : output arguments
• Arguments makes functions more versatile
• Manipulate different data at each call

rim_area = find_area(edge_radius) - find_area(hole_radius);

March 2022 CSE102 Computer Programming 35 March 2022 CSE102 Computer Programming 36

9
3/9/2022

Function print_rboxed Executing print_rboxed (135.68);

• Actual parameter: 135.68


• Formal parameter: rnum

March 2022 CSE102 Computer Programming 37 March 2022 CSE102 Computer Programming 38

Function with Input Arguments and Result Functions find_circum and find_area

March 2022 CSE102 Computer Programming 39 March 2022 CSE102 Computer Programming 40

10
3/9/2022

Executing circum = find_circum (radius); Function scale

March 2022 CSE102 Computer Programming 41 March 2022 CSE102 Computer Programming 42

Testing functions Testing Functions


• Functions can be tested by a program that uses it
• Driver program
• Defines function arguments
• Call the functions
• Display the return value

March 2022 CSE102 Computer Programming 43 March 2022 CSE102 Computer Programming 44

11
3/9/2022

scale(num_1, num_2); Argument Correspondence


• Be careful to provide correct
• number of arguments
• order of arguments
• type of arguments
• Actual parameter int to formal parameter double
• Actual parameter double to formal parameter int
• Loss of fractional part

March 2022 CSE102 Computer Programming 45 March 2022 CSE102 Computer Programming 46

Thanks for listening!

12

You might also like