3/23/2022
“Talk is cheap. Show me the code.”
- Linus Torvalds
Functions
• Functions: Components of a program
CSE102 • Connect functions to generate a program
• Each function has
Computer Programming with C • Inputs
• Parameters
• Computes manipulate different data each time it is called
• Outputs
2021-2022 Spring Semester • Returns a result with return statement
Modular Programming • Output parameters to return multiple results
© 2015-2022 Yakup Genç
March 2022 CSE102 Computer Programming 2
1 2
Functions Diagram of Function separate
• Function call • Ex: Gets a double value, find and return
• Allocate memory space for each formal parameter • Sign
• Heap vs Stack • Whole number magnitude
• Store actual parameter value in the allocated space • Fractional part
• Execute function code
• Three output parameters
• Manipulates the values of formal parameters
March 2022 CSE102 Computer Programming 3 March 2022 CSE102 Computer Programming 4
3 4
3/23/2022
Function separate Function Output Parameters
• Use * in front of the output parameters
• declaration
char *signp,
• assignment
*signp = ‘-’;
• signp : pointer
• contains address of a char variable
• “p” is used because it is pointer
March 2022 CSE102 Computer Programming 5 March 2022 CSE102 Computer Programming 6
5 6
Program That Calls separate Program That Calls separate
• Three variables defined in main function
• values will be defined by function separate
• address of sn is stored in output parameter signp
• Use & operator on the actual parameter
separate(value, &sn, &whl, &fr);
• separate knows where sn is in the memory.
• Like scanf
• &sn is of type char-pointer
March 2022 CSE102 Computer Programming 7 March 2022 CSE102 Computer Programming 8
7 8
3/23/2022
Parameter Correspondence Use of output parameters
• Indirection operator: *
separate(value, &sn, &whl, &fr); • Applied to formal parameter (pointer)
• Follow the pointer referenced by the formal parameter
• Indirect reference
March 2022 CSE102 Computer Programming 9 March 2022 CSE102 Computer Programming 10
9 10
Function separate Meanings of * Symbol
• Three distinct meanings
• Multiplication
• Declaration
• char *sn : means sn is pointer to char
• Indirection operator
• Follow pointer
• *sn is of type char
March 2022 CSE102 Computer Programming 11 March 2022 CSE102 Computer Programming 12
11 12
3/23/2022
Input/Output Parameters Program to Sort Three Numbers
• Single parameter for
• Bring data to the function
• Carry result out of the function
• Ex: Arrange three values in increasing order
• Input:
• num1, num2, num3
• Output:
• num1 is the smallest of input values,
• num2 is the second smallest of input values,
• num3 is the largest of input values,
• Function order orders two arguments
March 2022 CSE102 Computer Programming 13 March 2022 CSE102 Computer Programming 14
13 14
Data Areas of order(&num1, &num3); Program to Sort Three Numbers
March 2022 CSE102 Computer Programming 15 March 2022 CSE102 Computer Programming 16
15 16
3/23/2022
Program Style Subprograms
• Use functions take input parameters and return a value
• Easier to understand and maintain
• No indirect reference
• No address operator
• Return value is assigned to a variable at caller
• Math function are of this type
March 2022 CSE102 Computer Programming 17 March 2022 CSE102 Computer Programming 18
17 18
Subprograms Scope of Names
• Region of program that the name is visible
• Scope of
• constant macros
• From definition to the end of source file
• function names
• From function prototype to the end of source file
• variables
• From declaration to closing brace
• What if an identifier is defined before?
March 2022 CSE102 Computer Programming 19 March 2022 CSE102 Computer Programming 20
19 20
3/23/2022
Program for Studying Scope of Names Scope of Names
March 2022 CSE102 Computer Programming 21 March 2022 CSE102 Computer Programming 22
21 22
Formal Output Parameters as Actual Arguments Function scan_fraction
• Passing output parameters to other functions
• Ex: Reading values into output parameters
• Ex: Write a function to read a common fraction
numerator / denominator
• Function scan_fraction
• Two output parameters
• Reads a fraction until a valid fraction is entered
March 2022 CSE102 Computer Programming 23 March 2022 CSE102 Computer Programming 24
23 24
3/23/2022
Function scan_fraction Data Areas for scan_fraction and Its Caller
March 2022 CSE102 Computer Programming 25 March 2022 CSE102 Computer Programming 26
25 26
Case Study: Common Fraction Problem Case Study: Common Fraction Problem
• Problem: Write a program to add, subtract, multiply and divide pairs Algorithm
of common fractions 1. Repeat as long as user wants to continue
• Inputs: 2. Get a fraction problem
• First fraction: numerator and denominator 3. Compute the result
• Second fraction: numerator and denominator 4. Display the problem and result
• Operator
5. Check if user wants to continue
• Output:
• Resulting fraction
March 2022 CSE102 Computer Programming 27 March 2022 CSE102 Computer Programming 28
27 28
3/23/2022
Case Study: Common Fraction Problem Case Study: Common Fraction Problem
Algorithm Algorithm
1. Repeat as long as user wants to continue 1. Repeat as long as user wants to continue
2. Get a fraction problem
2. Get a fraction problem
1. Get first fraction (scan_fraction)
3. Compute the result
1. Select and perform task based on operator
2. Get operator (get_operator)
Add the fractions (add_fractions)
3. Get second fraction (scan_fraction) Add the first fraction and the negation of the second fraction
3. Compute the result Multiply the fractions (multiply_fractions)
Multiply the first fraction with reciprocal of the second fraction
4. Display the problem and result 2. Put the result fraction in reduced form
5. Check if user wants to continue 4. Display the problem and result
5. Check if user wants to continue
March 2022 CSE102 Computer Programming 29 March 2022 CSE102 Computer Programming 30
29 30
Case Study: Common Fraction Problem
Algorithm
1. Repeat as long as user wants to continue
2. Get a fraction problem
3. Compute the result
1. Select and perform task based on operator
2. Put the result fraction in reduced form
Find the GCD of the numerator and denominator (find_gcd)
Divide numerator and denominator by the GCD (reduce_fraction)
4. Display the problem and result (print_fraction)
5. Check if user wants to continue
March 2022 CSE102 Computer Programming 31 March 2022 CSE102 Computer Programming 32
31 32
3/23/2022
March 2022 CSE102 Computer Programming 33 March 2022 CSE102 Computer Programming 34
33 34
March 2022 CSE102 Computer Programming 35 March 2022 CSE102 Computer Programming 36
35 36
3/23/2022
March 2022 CSE102 Computer Programming 37 March 2022 CSE102 Computer Programming 38
37 38
March 2022 CSE102 Computer Programming 39 March 2022 CSE102 Computer Programming 40
39 40
3/23/2022
Sample Run Program Style
• Keep the functions to a manageable size
• Less error
• Easier to read and test
March 2022 CSE102 Computer Programming 41 March 2022 CSE102 Computer Programming 42
41 42
Testing Stub for Function multiply_fractions
• Top-down testing
• Test general flow of control
• stubs
• Stubs
• Used instead of functions not yet written
• Team work!..
• Enables testing and debugging
• Displays an identification message
• Assign values to output parameters
March 2022 CSE102 Computer Programming 43 March 2022 CSE102 Computer Programming 44
43 44
3/23/2022
Testing Driver for Function scan_fraction
• Bottom-up Testing
• First test individual functions
• Unit test
• Test entire system later
• System integration test
• Unit Test
• Preliminary test of a function separate from the whole program
• Using driver program
• Driver gives values to input parameters
• Calls the function
• Display and check function results
March 2022 CSE102 Computer Programming 45 March 2022 CSE102 Computer Programming 46
45 46
Debugging
• Good documentation is essential
• Function’s purpose, parameters, local variables
• Debug each function as you write them
• Create a trace
•
•
Display the function name as you enter it
Display and verify the input parameters as you enter a function
Thanks for listening!
• Display and verify return values after function returns
• After it works fine do not erase display statements, comment them out. You may need them
later
• Use debugger
• First execute a function as a single statement
• If the result is incorrect step in its statements
March 2022 CSE102 Computer Programming 47
47 48