Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.

Similar presentations


Presentation on theme: "Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics."— Presentation transcript:

1 Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr

2 Topics Functions with Simple Output Parameters Formal Output Parameters as Actual Arguments

3 Pointers The correct understanding and use of pointers is crucial to successful C programming. There are several reasons for this: –Pointers provide the means by which functions can modify their calling arguments –Pointers support dynamic allocation –Pointers can improve the efficiency of certain routines –Pointers provide support for dynamic data structures, such as binary trees and linked lists

4 Pointers Pointers are one of the strongest but also one of the most dangerous features in C. For example, a pointer containing an invalid value can cause your program to crash Perhaps worse, it is easy to use pointers incorrectly, causing bugs that are very difficult to find

5 Pointers Pointers are used frequently in C, as they have a number of useful applications. –For example, pointers can be used to pass information back and forth between a function and its reference point In particular, pointers provide a way to return multiple data items from a function via function arguments Pointers also permit references to other functions to be specified as arguments to a given function. This has the effect of passing functions as arguments to the given function

6 Passing Pointers to a Function Pointers are often passed to a function as arguments This allows data items within the calling portion of the program to be accessed by the function, altered within the function, and then returned to the calling portion of the program in altered form We refer to this use of pointers as passing arguments by reference (or by address or by location), in contrast to passing arguments by value

7 Passing Pointers to a Function When an argument is passed by value, the data item is copied to the function. Thus, any alteration made to the data item within the function is not carried over into the calling routine When an argument is passed by reference, however (i.e., when a pointer is passed to a function), the address of a data item is passed to the function. Any change that is made to the data item (i.e., to the contents of the address) will be recognized in both the function and the calling routine

8 #include void funct1( int u, int v); / * function prototype */ void funct2( int *pu, int *pv); /* function prototype */ main () { int u = 1, v = 3; printf("\nBefore calling funct1: u=%d v=%d", u, v); funct1(u, v); printf( "\nAfter calling funct1: u=%d v=%d", u, v); printf(“\n\nBefore calling funct2: u=%d v=%d“, u,v); funct2(&u, &v); printf( "\nAfter calling funct2: u=%d v=%d", u, v); } void funct1(int u, int v) { u = 0; v = 0; printf( "\nWithin funct1: u=%d v=%d", u, v); } void funct2(int *pu, int *pv) { *pu = 0; *pv = 0; printf( "\nWithin funct2: *pu=%d *pv=%d",*pu,*pv); } Example

9 main () { int u = 1, v = 3; printf("\nBefore calling funct1: u=%d v=%d", u, v); funct1(u, v); printf( "\nAfter calling funct1: u=%d v=%d", u, v); printf(“\n\nBefore calling funct2: u=%d v=%d“, u,v); funct2(&u, &v); printf( "\nAfter calling funct2: u=%d v=%d", u, v); } void funct1(int u, int v) { u = 0; v = 0; printf( "\nWithin funct1: u=%d v=%d", u, v); } Example Before calling funct1: u=1 v=3 Within funct1: u=0 v=0 After calling funct1: u=1 v=3

10 main () { int u = 1, v = 3; printf("\nBefore calling funct1: u=%d v=%d", u, v); funct1(u, v); printf( "\nAfter calling funct1: u=%d v=%d", u, v); printf(“\n\nBefore calling funct2: u=%d v=%d“, u,v); funct2(&u, &v); printf( "\nAfter calling funct2: u=%d v=%d", u, v); } void funct2(int *pu, int *pv) { *pu = 0; *pv = 0; printf( "\nWithin funct2: *pu=%d *pv=%d",*pu,*pv); } Example Before calling funct2: u=1 v=3 Within funct2: *pu=0 *pv=0 After calling funct2: u=0 v=0

11 Returning Multiple Results from a Function So far, we have used return statement to return one result from a function Pointers can be used to return multiple results

12 Function separate

13 Diagram of Function separate with Multiple Results

14 Figure 6.3 Program That Calls a Function with Output Arguments

15 Program That Calls a Function with Output Arguments (cont’d)

16

17 Parameter Correspondence for separate(value, &sn, &whl, &fr);

18 Comparison of Direct and Indirect Reference

19 Meanings of * Symbol 1.Multiplication operation (a * b) 2.Pointer declaration char *signp; Read * as “pointer to” 3.Unary * *signp = ‘+’; Means “follow the pointer” Careful! Unary * does not mean “pointer to”

20 Multiple Calls to a Function with Input/Output Parameters So far, we have passed information into a function through its input parameters and returned results from a function through its output parameters We can also use a single parameter both to bring data value into a function and carry the result out of the function

21 Figure 6.6 Program to Sort Three Numbers

22 Figure 6.6 Program to Sort Three Numbers (cont’d)

23 Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);

24 Different Kinds of Functions PurposeFunction TypeParametersTo Return Result To compute a single value Same as type of value to be computed Input parameters hold copies of data provided Function includes a return statement To produce printed output voidInput parameters hold copies of data provided No result is returned To compute multiple results voidInput parameters hold copies of data provided Output parameters are pointers to actual arguments Results are stored in the calling function’s data area by indirect assignment. No return statement is required To modify argument values voidInput/Output parameters are pointers to actual arguments Results are stored in the calling function’s data area by indirect assignment. No return statement is required


Download ppt "Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics."

Similar presentations


Ads by Google