C User Defined Functions

Functions created by user or developer for the internal usage of current program or project is known as user defined function (UDF). A function, depending on whether arguments are present or not and whether a value returned or not, it may belong to one of the following types/categories.

CALL BY VALUE AND CALL BY REFERENCE

  1. CALL BY VALUE
    • Values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations.
    • Therefore, changes made inside functions are not reflected in actual parameters of the caller.
    • example
  2. CALL BY REFERENCE
    • Both the actual and formal parameters refer to the same locations.
    • Therefore, changes made inside the function are actually reflected in actual parameters of the caller.
    • example

EXAMPLES

  1. UDFs to print Static/Fix line, Line with N columns and Line with N columns and C char/symbol.
  2. Call by Value example
  3. Call by Reference example

EXAMPLES OF EACH UDF CATEGORY

1 ➟ FUNCTION WITH NO ARGUMENT AND NO RETURN VALUE

When a function has no arguments and it does not return a value to the caller, when there is no data transfer between calling function and called function.

// function declaration
void f1();


void f1()
{
    printf ("function f1 is called");
}

void main()
{
   // here main is the calling function

   f1();

   // f1() is the called function
}

2 ➟ FUNCTION WITH ARGUMENT AND NO RETURN VALUE

When a function has argument(s), but does not return a value to the caller, data is passed to the called function and no data is returned to the calling function

// function declaration
void f1(int);


void f1(int i)
{
    printf ("function f1 is called with data/argument(s) -> %d",i);
}

void main()
{
   // here main is calling function
   // function f1() is called with a data (arguments)

   f1(10);

   // f1() is the called function
}

3 ➟ FUNCTION WITH NO ARGUMENT AND RETURN VALUE

When a function has no argument, but it does return a value to the caller, no data is passed to the called function but data is returned to the calling function.

// function declaration
int f1();


void f1()
{
    int a = 10;
    int b = 10;

    int c = a + b;

    return c;
}

void main()
{
   // here main is calling function
   // function f1() is called with no data (arguments)
   int t = f1();
   printf ("returned value = %d",t);  
   // or
   printf ("returned value = %d",f1());
}

4 ➟ FUNCTION WITH ARGUMENTS AND RETURN VALUE

When a function has argument(s), and it also does return a value to the caller, data is passed to the called function and data is also returned to the calling function.

// function declaration
int f1(int , int );


void f1(int n1, int n2)
{    
    int c = n1 + n2;

    return c;
}

void main()
{
   // here main is calling function
   // function f1() is called with no data (arguments)
   int t = f1(100,200);
   printf ("returned value = %d",t);  
   // or
   printf ("returned value = %d",f1(100,200));
}

5 ➟ FUNCTION THAT RETURNS MULTIPLE VALUES (using pointer)

#include <stdio.h>
#include <conio.h>

// www.raviroza.com
// 7-Oct-2022, 8.00 am
 
// Function to return multiple values using pointers

void initialize(int *a, int *b, char *c)
{
    *a = 101;
    *b = 202;
    *c = 'R';
}
 
// Return multiple values from a function in C

void main(void)
{
    int a, b;
    char c;
 
    initialize(&a, &b, &c);
    printf("a = %d, b = %d, c = %c", a, b, c);
 
    getch();
}

/*
Output:

	a = 101, b = 201, c = R
*/

EXAMPLES

EXAMPLE-1 ➟ UDF to print Static/Fix line, Line with N columns and Line with N columns and C char/symbol.

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 7-Dec-2021, 11.00 pm

// function prototype
//-------------------
void line1();
void line2(int);
void line3(int,char);
//-------------------


// function definitions

// function to print line with fix columns
void line1()
{
	printf ("\n--------------------\n");
}

// function to print line  with n number of columns
void line2(int n)
{
	int i;
	printf ("\n");
	for(i=1; i<=n; i++)
	{
		printf ("-");
	}
	printf ("\n");
}

// function to print line  with n number of columns & given symbol/char
void line3(int n, char c)
{
	int i;
	printf ("\n");
	for(i=1; i<=n; i++)
	{
		printf ("%c",c);
	}
	printf ("\n");
}

int main()
{
	// to print line with fix columns
	line1();
	// to print line with n columns
	line2(5);
	// to print line with n columns & given symbol/char
	line3 (10,'@');
	getch();
}

EXAMPLE-2 ➟ CALL BY VALUE

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 8-Dec-2021, 10.30 am

void byval(int);

void byval(int i)
{
	i=i+1;
}

void main()
{
	int temp=10;
	byval(temp);        
	printf ("temp = %d",temp);	
	getch();
}

EXAMPLE-3 ➟ CALL BY REFERENCE

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 8-Dec-2021, 10.30 am

void byref(int*);

void byref(int *i)
{
	*i = *i + 1;
}

void main()
{
	int temp=10;
	
	byref(&temp);
	printf ("\n\ntemp = %d",temp);
	getch();
}

EXAMPLE-4 ➟ ARRAY (int,char) AS PARAMETER TO FUNCTION

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 28-Dec-2021, 7.57 am

void fun(int array[], int size)
{
	int i;
	for(i=0; i<size; i++)
		printf ("%d\n",array[i]);
}
void fun1(char array[])
{
	int i;
	for(i=0; i<strlen(array); i++)
		printf ("%c",array[i]);
}
void main()
{
	int arr[]  = {1,2,3,4,5,6,7,8,9,10};
	char *c = "ravi r oza";
	clrscr();

	fun(arr,10);
	fun1(c);
	getch();
}