Programming for Problem Solving (3110003)

BE | Semester-1   Winter-2019 | 07-01-2020

Q5) (b)

Categorize User defined function’s components (elements).

Functions can be classified in one of the following categories based on whether arguments are present or not, whether a value is returned or not.

  1. Functions with no arguments and no return value         // void printline(void)
  2. Functions with no arguments and return a value           // int printline(void)
  3. Functions with arguments and no return value              // void printline(int a)
  4. Functions with arguments and one return value            // int printline(int a)
  5. Functions that return multiple values using pointer      // void printline(int a)

Functions with no arguments and no return value

  • When a function has no argument, it does not receive any data from the calling function.
  • When it does not return a value, the calling function does not receive any data from the called function.
  • In fact there is no data transfer between the calling function and called function.
Example:
#include <stdio.h>
void printline(void); 
void main()
{ 
    printline(); 
    printf("\n GTU \n"); 
}
void printline(void) { 
    int i; 
    for(i=0; i<10; i++) { 
        printf("-"); 
    } 
}
 

Functions with no arguments and return a value

  • When a function has no argument, it does not receive data from the calling function.
  • When a function has return value, the calling function receives one data from the called function.
Example:
#include <stdio.h>
int get_number(void); 
void main() { 
    int m; 
    m = get_number(); 
    printf("%d", m); 
}

int get_number(void) { 
    int number; 
    printf("enter number:"); 
    scanf("%d", &number); 
    return number; 
} 
 

Functions with arguments and no return value

  • When a function has an argument, it receives data from the calling function.
  • When it does not return a value, the calling function does not receive any data from the called function.
Example:
#include <stdio.h>
void sum(int, int); 
void main() { 
	int no1,no2; 
	printf("enter no1,no2:"); 
	scanf("%d%d", &no1, &no2); 
	sum(no1, no2); 
}
void sum(int no1, int no2) { 
	if(no1 > no2)
		printf("\n no1 is greatest"); 
	else
		printf("\n no2 is greatest"); 
}  
 

Functions with arguments and one return value

  • When a function has an argument, it receives data from the calling function.
  • When a function has return value, the calling function receives any data from the called function.
Example:
#include <stdio.h>
int sum(int); 
void main() 
{
	int no, x; 
	printf("enter no:"); 
	scanf("%d", &no); 
	x = sum(no); 
	printf("sum=%d", x); 
}
int sum(int no) { 
	int add = 0,i; 
	while(no > 0) { 
		i = no % 10; 
		add = add + i; 
		no = no / 10; 
	} 
	return add; 
}
 

Functions that returns a multiple value

  • Function can return either one value or zero value. It cannot return more than one value.
  • To receive more than one value from a function, we have to use a pointer.
  • So function should be called with reference not with value.
Example:
#include <stdio.h>
void mathoperation(int x, int y, int *s, int *d); 
void main()
{ 
	int x=20,y=10,s,d; 
	mathoperation(x,y,&s,&d); 
	printf("s=%d \nd=%d", s,d); 
}
void mathoperation(int a, int b, int *sum, int *diff) { 
	*sum = a + b; 
	*diff = a - b; 
}