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

Post on 19-Dec-2015

218 views 0 download

Tags:

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

Computer ProgrammingLecture 13

Functions with Multiple Output Parameters

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

Department of Electrical & Electronics Engineeringnukhet.ozbek@ege.edu.tr

Topics

• Functions with Simple Output Parameters

• Formal Output Parameters as Actual Arguments

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

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

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

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

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

#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

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

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

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

Function separate

Diagram of Function separate with Multiple Results

Figure 6.3 Program That Calls a Function with Output Arguments

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

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

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

Comparison of Direct and Indirect Reference

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”

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

Figure 6.6 Program to Sort Three Numbers

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

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

&num3);

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