ICS 103: Computer Programming in C

Handout-10

Topic: Functions with Output Parameters

OR

Function Output Parameters Implemented with Pointers

 

Instructor: M. Waheed Aslam.

 

Objectives:

·        To know how a function can return more than one value using output parameters.

·        To know some basic concept about pointers.

·        To study some examples on the above concepts.

 

Functions with output parameters using pointers:

·        Till now, we used only the functions that returned zero or one value through the return statement.

·        To return more than one value, we need to use the output parameters, which are pointers.

·        Note that: when we use output parameters in functions, we declare those functions as void returning functions because they are not returning single value by return statement.

 

Basic Concept about Use of Pointers:

What is Pointer?

·         Pointer variable is a special variable which stores the address of other variable.

·        If a pointer variable stores the address of a char variable, we call it a character pointer and so on for other types.   

                              

·        Normally a variable directly contains a specific value.

·        A pointer, on the other hand, contains an address of a variable that contains a specific value. 

·        Pointers like any other variables must be declared before they can be used.

To handle pointers in C language we use two unary operators:  &, *

&:  Address operator (ampersand symbol)

*:  Pointer operator OR Indirection operator OR Value at address.

Examples:

int *j               Means the value at address contained in j is an int

            OR

in other words: j is an integer pointer.

int **k               Means the value at address contained in *k is an int.

 

Printing Pointers:

The value of a pointer may be seen by calling printf with the %p format specifier.

 Examples of Pointers: 

 Example #1: Show the output?

#include<stdio.h>

int main(void)

{ char g='z';

char c='a';    

char  *p;

p=&c;             

printf("*p is %c\n",*p);

printf("p is %p\n",p);

p=&g;

printf("& of g is %p\n", p);

printf("*p is %c\n",*p);

printf("p is %p\n",p);

return (0);

} 

Example#2. Show the output?
#include<stdio.h>

void test1(int m, int n)

{ m=5;

  n=24;

 }

void test2(int *m, int *n)

  { *m=5;

  *n=24;

   }

void test3(int a, int *b)

  {a=38;

   *b=57;

   }

 

  //Example 2 (Cont.)

int main(void)

   {int a=10, b=16;

    printf("a=%d, b=%d\n",a,b);

    test1(a,b);

    printf("a=%d, b=%d\n",a,b);   

    test2(&a,&b);

    printf("a=%d, b=%d\n",a,b);

    test3(a,&b);

    printf("a=%d, b=%d\n",a,b);

    return 0;

  }

Sample output from Example #1 Above                  Sample output from Example #2      

                                                                   

 

 

 

Examples of Functions returning more than one values using Output Parameters:

Example (i):

Consider the example of a function that accepts the radius of a circle from the main program and returns the area and circumference of a circle by using the output parameters:

 

#include <stdio.h>

void  area_circum (float radius, float *area, float * circum) ; /*function prototype */

void main (void)

{

      float  r, a, c ;

      printf (“Enter the radius of the circle \n”) ;

      scanf (“%f”, &r) ;

      area_circum (r, &a, &c);/*function call that passes address of the variables a and c*/

      printf (“The area is %f and circumference is %f\n”, a, c); /*output results */

} //end of main

 

void area_circum (float   radius, float   *area, float  * circum) // function

{

      *area = 3.14 * radius * radius; //indirect reference to variable a of main program

      *circum = 2 * 3.14 * radius ; //indirect reference to variable c of main program

}  // end of function area_circum( )

 

Sample Output:  

 

 

 

 

 

  

Here:

·        Variables area of function area_circum and a, of function main both refer to the same memory location.

·        Similarly, the variables circum of function area circum and c of main both refer to the same memory location.

 

Note that when the function is called in the main program, the output parameters have & attached to them before their name. When the output parameters are used in the function, they are used with * attached to them before their name.

 

Example#2:

/******************************************************************

Write a function that takes 3 integers a, b , c from main program as input parameter

and returns the sum of the three, the product of the three, and the average by using

output parameter. Write a main program to test the function.

**************************************************************/

 

#include<stdio.h>

void myfunction(int a,int b,int c,int *sum,int *prod,float *average) // function

{

 

 *sum=a+b+c;

 *prod=a*b*c;

 *average=(a+b+c)/3.0;

 

 }  // end of function

 

 int main (void) // main program or main function

 {

      int x,y,z,su_m,product;

      float av_g;

 

      printf("Enter three integer input parameters a, b and c :");

 

      scanf("%d %d %d",&x,&y,&z);

 

      myfunction(x, y, z, &su_m, &product, &av_g);  // function call

 

          printf("*******************************************************");

      printf("\nThe sum = %d\nThe product = %d\nThe avg = %f\n", su_m, product, 

                     av_g);

      return 0;

 

 }  // end of main

 

Sample Output:

 

Enter three integer input parameters a, b and c : 12 15 3

**************************************************

The Sum = 30

The Product = 540

The avg = 10.000000