The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.
The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.
Function: It is an independently coded subprogram that performs a specific task.
Need for user-defined function:
1) It helps to write, test, and debug the block of code.
2) If we need to call the same functionality by writing the same number of lines and the same code repeatedly that makes no sense.
Elements of user-defined function:
1) Function definition: It is an independent program module that is specially written to implement the requirements of the function.
2) Function call: To use this function we need to invoke it at a required place in the program.
3) Function declaration: The calling program should declare any function just like declaring a variable that is to be used later in the program.
Category of function:
1) Function with no arguments and no return value: When a function has no arguments, it doesn't receive any data from the calling function. Similarly, when it doesn't return a value, the calling function doesn't receive any data from the called function.
2) Function with arguments and no return value: When a function has some arguments, it does receive some data from the calling function. Similarly, when it doesn't return a value, the calling function doesn't receive any data from the called function.
3) Function with arguments and return value: When a function has some arguments, it does receive some data from the calling function. Similarly, when it does return a value, the calling function does receive data from the called function.
4) Function with no arguments but returns a value: When a function has no arguments, it doesn't receive any data from the calling function. Similarly, when it does return a value, the calling function does receive some data from the called function.
Nesting of functions: C permits the nesting of functions freely, main can call one-function, which can call second-function and so on.
Function with no argument and no return value:
function_with_no_argument_and_no_return_value.c
#include <stdio.h>
void subs_to_channel(void);
void main() {
subs_to_channel();
}
void subs_to_channel(void) {
printf("\nPlease Subscribe to this channel.\n");
}
Function with argument and no return value:
function_with_argument_and_no_return_value.c
#include <stdio.h>
void print_table(int);
void main() {
int num;
printf("\nEnter the number to print the table: ");
scanf("%d", &num);
print_table(num);
}
void print_table(int n) {
int i;
printf("\n");
for(i = 1; i<= 10; i++)
printf("%d x %d = %d\n", i, n, i*n);
}
Function with arguments and return value:
function_with_arguments_and_return_value.c
#include <stdio.h>
int square_of_num(int);
void main() {
int num, square;
printf("\nEnter the number to print the square: ");
scanf("%d", &num);
square = square_of_num(num);
printf("\nSquare of %d is %d.\n", num, square);
}
int square_of_num(int n) {
return n * n;
}
Function with no argument but return a value:
function_with_no_argument_but_return_a_value.c
#include <stdio.h>
int get_number(void);
void main() {
int num = get_number();
printf("\nYou entered: %d\n", num);
}
int get_number(void) {
int n;
printf("\nEnter a number: ");
scanf("%d", &n);
return n;
}
Nesting of function:
nesting_of_function.c
#include<stdio.h>
float simple_interest(int, float, float);
float amount(int, float, float);
void main() {
int principal;
float interest_rate, time_period;
printf("\nEnter the Principal: ");
scanf("%d", &principal);
printf("\nEnter the Rate of Interest: ");
scanf("%f", &interest_rate);
printf("\nEnter the Time Duration: ");
scanf("%f", &time_period);
printf("\nTotal Amount you have to pay: %0.2f", amount(principal, interest_rate, time_period));
}
float simple_interest(int p, float r, float t) {
float total_si = ((p * r * t) / 100);
printf("\nTotal Interest: %0.3f", total_si);
return total_si;
}
float amount(int principal, float rate_of_interest, float time_duration) {
return (principal + simple_interest(principal, rate_of_interest, time_duration));
}
Recursion:
recursion.c
#include<stdio.h>
int factorial(int);
void main() {
int num;
printf("\nEnter a number to know its factorial: ");
scanf("%d", &num);
printf("\nFactorial of %d is %d.\n", num, factorial(num));
}
int factorial(int n) {
int factorial_value;
if (n == 1)
return 1;
else
factorial_value = n * factorial(n - 1);
return factorial_value;
}