A function is a self-contained block of statements which we can use whenever we want. A user-defined function is a type of function which is defined by the user.
Types of user-defined function in C
- Function with no arguments and no return value.
- Function with arguments and no return value.
- Function with no arguments and a return value.
- Function with arguments and a return value.
Type 1: Function with no arguments and no return value.
// example of a void function with no arguments. #include <stdio.h> void Add() { int a, b; // declaring variables. scanf("%d %d",&a, &b); // taking input from user. int sum = a + b; // storing the summation of given two numbers. printf("Summation of given two numbers = %d \n", sum); } int main() { Add(); // calling function from the main. return 0; }Note
- Above Function has no arguments and no return value. So it is a void function.
- After calling this function from the main, this function will take input from the user and print the sum of two numbers.
Type 2: Function with arguments and no return value.
// This is an example of a void function with arguments. #include <stdio.h> void Add(int a, int b) { int sum = a + b; // storing the summation of given two numbers. printf("Summation of given two numbers = %d \n", sum); } int main() { int x, y; // declaring variables. scanf("%d %d",&x, &y); // taking input from user. Add(x, y); // calling function from the main. return 0; }Note
- Above Function has arguments but no return value. So it is also a void function.
- Above function is passing parameters from the main after that, Add function will print the summation of these two numbers.
Type 3: Function with no arguments and a return value.
// This is an example of a no argument function which has a return value. #include <stdio.h> int Add() { int a, b; // declaring variables. scanf("%d %d",&a, &b); // taking input from user. int sum = a + b; // storing the summation of given two numbers. return sum; // return the sum value } int main() { int result = Add(); // calling function from the main and receiving the return value. printf("Summation of given two numbers = %d \n", result); return 0; }Note
- When we call the function from the main it will return a value.
- For receiving the return value we used result variable and assigned the Add() function into that variable so that the return data can be stored into the variable called result.
- We can use different names for the variable but the datatype of the must be same as the function return data type.
Type 4: Function with arguments and a return value.
// This is an example of a function which has arguments and a return value also. #include <stdio.h> int Add(int a, int b) { int sum = a + b; // storing the summation of given two numbers. return sum; // return the sum value } int main() { int x, y; // declaring variables. scanf("%d %d",&x, &y); // taking input from user. int result = Add(x, y); // calling function from the main. printf("Summation of given two numbers = %d \n", result); return 0; }Note
- In this case we take input into the main function and passed it through the Add() function which is the only difference from the previous example.
No comments:
Post a Comment