Function is a self contained block of statement that perform well defined or coherent task in c programming. Every c program consists of one or more functions, one of them is the main() function. There are two types of functions in c programming.
1. Inbuilt functions
2. User defined functions
The functions which are predefined in the library of the c compilier for specific purpose are called inbuilt or libray or predefined functions. The internal working mechanism of such functions is not known to the user. Library functions cannot be modified by the user .
Example : printf(), scanf(), getchar()
The functions which are defined by the user as per their requirement in c programming are called user defined function. The user defined function can be modified by user and internal working mechanism of the function is known to the user.
Example :
#include <stdio.h> int result, sum(a,b); int main() { int a = 10, b = 20; result = sum(a,b); printf("The sum of a and b is %d\n",result); } int sum(a,b){ result = a + b; return result; }
Here, the function sum() is a user defined function.
i. Function prototype or function declaration
ii. Arguments or parameter
iii. Function call or calling function
iv. Function definition or called function
The declaration of a function before using it in the program is called function prototype. The function prototype is used to check the return type function name, number of arguments and their data types. It is always written above the main function.
Example :
int sum();
Variables used within the parenthesis "()"" of a function are called arguments. Arguments are used to transfer the value from calling function to the called function.
Example :
int sum(a,b);
Function call is similar to function prototype expect that three is no return type infront of function name. It is written within the body of main () function. When the compiler encounters function call statement, the control will be transferred to the called function.
Example :
int main()
{
result = sum(a,b);
}
It consists of two parts, function header and function body. Function header is similar to function prototype and there is no semicolon at the end followed by function body. The function body contains the actual statement or code to be executed whenever the function is called.
Example :
int sum(a,b)
{
result = a + b;
return result;
}
Depending upon the return type and number of arguments that the function accept, user defined functions can be categorized into four types.
Lets have a look at one single example with all user defined function types.
In this type of user defined funcitons, no value is transferred from calling function to called function. No value is returned back to the calling function from called function.
Example :
#include <stdio.h> void checkPrimeNumber(); int main() { checkPrimeNumber(); // argument is not passed return 0; } // return type is void meaning doesn't return any value void checkPrimeNumber() { int n, i, result = 0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=2; i <= n/2; ++i) { if(n%i == 0) { result = 1; } } if (result == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); }
In this type of user defined funcitons, values are transferred from calling function to called function. But, no value is returned back to the calling function from called function.
Example :
#include <stdio.h> void checkPrimeNumber(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d",&n); checkPrimeNumber(n); // argument is passed return 0; } // return type is void meaning doesn't return any value void checkPrimeNumber(n) { int i, result = 0; for(i=2; i <= n/2; ++i) { if(n%i == 0) { result = 1; } } if (result == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); }
In this type of user defined funcitons, no value is transferred from calling function to called function. But, the value is returned back to the calling function from called function.
Example :
#include <stdio.h> int checkPrimeNumber(); // no argument is passed int main() { int n, i, result = 0; n = checkPrimeNumber(); for(i=2; i <= n/2; ++i) { if(n%i == 0) { result = 1; } } if (result == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); } // the value n is returned int checkPrimeNumber() { int n; printf("Enter a positive integer: "); scanf("%d",&n); return n; }
In this type of user defined funcitons, values are transferred from calling function to called function. And the value is returned back to the calling function from called function.
Example :
#include <stdio.h> int checkPrimeNumber(int n); // argument is passed int main() { int n, result; printf("Enter a positive integer: "); scanf("%d",&n); checkPrimeNumber(n); if (result == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); } // the value n is returned int checkPrimeNumber(n) { int i, result = 0; for(i=2; i <= n/2; ++i) { if(n%i == 0) { result = 1; } } }
The function which calls itself again and again until a defined condition is satisfied is known as recursive function. The function definition where the function call statement call itself is called recursive function and the process is called recursion. The difference between normal function and recursive function is that the normal function is called by main function whereas recursive function is called by the recursive function call itself.
Example :
#include<stdio.h> int recursion(x); int main () { int x; printf ("Enter the number \n"); scanf ("%d",&x); recursion(x); return 0; } int recursion(x) { printf("\n%d",x); if(x == 1){ return 1; }else{ return recursion(x-1); } }
Output:
If you entered 12, the result will be:
12
11
10
9
8
7
6
5
4
3
2
1
Print series in C
1
11
111
1111
11111
111111
1111111
11111111
111111111
1111111111
#include<stdio.h> int main () { int i = 1,x=0; for(int i=1;i<=10;i++) { x = x*10+1; printf("\n%d",x); } }
Leave a comment