Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet...

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

Transcript of Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet...

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

Computer ProgrammingLecture 13

Functions with Multiple Output Parameters

Assist.Prof.Dr. Nükhet ÖZBEKEge University

Department of Electrical & Electronics [email protected]

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

Topics

• Functions with Simple Output Parameters

• Formal Output Parameters as Actual Arguments

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

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

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

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

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

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

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

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

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

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

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

#include <stdio.h>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

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

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=3Within funct1: u=0 v=0After calling funct1: u=1 v=3

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

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=3Within funct2: *pu=0 *pv=0After calling funct2: u=0 v=0

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

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

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

Function separate

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

Diagram of Function separate with Multiple Results

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

Figure 6.3 Program That Calls a Function with Output Arguments

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

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

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

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

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

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

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

Comparison of Direct and Indirect Reference

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

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”

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

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

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

Figure 6.6 Program to Sort Three Numbers

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

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

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

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

&num3);

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

Different Kinds of FunctionsPurpose Function Type Parameters To 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

void Input parameters hold copies of data provided

No result is returned

To compute multiple results

void Input 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

void Input/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