Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, May 12, 2020

Types of user defined function in C

Types of user defined function | welcome2protec


In this tutorial, we will learn about different approaches of user defined function. where you can solve the same problem by four different ways using funcitons.


There can be 4 different types of user defined function as given below:

  • Function with no argument and no return value
  • Function with argument but no return value
  • Function with no argument but return value
  • Function with argument and a return value

A signal program can be defined by 4 different ways as mentioned above. Let's understand with a simple example to find sum of two integers input given by user.

Example 1: Function with no argument and no return value

 /* Program two find sum of two integers input given by user */
 #include <stdio.h> 
 #include <conio.h>
 /*Function declaration OR function prototype*/
 void sum(void); 
 void main()  
 {  
  clrscr(); 
  /* Function call with no argument */
  sum();
  getch();  
 } 
 /* Creating user defined function OR function definition*/ 
 void sum()
 {
  int n1, n2, result;
  printf("Enter two numbers: ");
  scanf("%d%d", &n1, &n2);
  result = n1 +n2;
  printf("Sum = %d", result);
 }

Here, sum(); function is called from the main() with no arguments. Since, no value is returned from the function. Therefore, return type of sum() will be void.

Note: The empty parentheses in sum(); statement inside the main() function indicates that no arguments is passed to the function.

Output

 Enter two numbers: 10, 20
 sum = 30 

Example 2: Function with argument but no return value

 /* Program two find sum of two integers input given by user */
 #include <stdio.h> 
 #include <conio.h>
 /*Function declaration OR function prototype*/
 void sum(int, int); 
 void main()  
 {  
  int a, b;
  clrscr(); 
  printf("Enter two numbers: ");
  scanf("%d%d", &a, &b);
  /* Function call with argument */
  sum(a, b);
  getch();  
 } 
 /* Creating user defined function OR function definition*/ 
 void sum(int n1, int n2)
 {
  int result;
  result = n1 +n2;
  Printf("Sum = %d", result);
 }

The value of a and b entered by the user is passed to the sum(a, b) function, and this value will be copied to the formal parameters. 

Here, variables n1 and n2 will be replaced with the value of a and b in sum(int n1, int n2); function. 

Remember: When we pass the actual parameters while calling a function then this is known as function call by value. I this case the values of actual parameters are copied to the formal parameters.

Output

 Enter two numbers: 10 20
 sum = 30   

Example 3: Function with no argument but return value

 /* Program two find sum of two integers input given by user */
 #include <stdio.h>
 #include <conio.h>
 /*Function declaration OR function prototype*/
 int sum(void); 
 void main()  
 {
  int s;  
  clrscr(); 
  /* Function call with no argument */
  s = sum();
  Printf("Sum = %d", s);
  getch();  
 } 
 /* Creating user defined function OR function definition*/ 
 int sum()
 {
  int n1, n2, result;
  printf("Enter two numbers: ");
  scanf("%d%d", &n1, &n2);
  result = n1 +n2;
  return result;
 }

Output

Enter two numbers: 10 20
sum = 30 

Example 4: Function with argument and a return value

 /* Program two find sum of two integers input given by user */
 #include <stdio.h>
 #include <conio.h>
 /*Function declaration OR function prototype*/
 int sum(int, int); 
 void main()  
 {  
  int a, b, s;
  clrscr(); 
  printf("Enter two numbers: ");
  scanf("%d%d", &a, &b);
  /* Function call with argument */
  s = sum(a, b);
  Printf("Sum = %d", s);
  getch();  
 } 
 /* Creating user defined function OR function definition*/ 
 int sum(int n1, int n2)
 {
  int result;
  result = n1 +n2;
  return result;
 }

Output

 Enter two numbers: 10 20
 sum = 30 
  







Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us