A function is a group of statements that together perform a task. EveryC program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions.
Example 1 : Calculate the Factorial By using Function Method.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <stdio.h> #include <stdlib.h> int factorial(int x){ int result=1,i; for(i=x;i>0;i--){ result*=x; x--; } return result; } int main() { int a,b; printf("please enter an integer number\n"); scanf("%d",&a); b=factorial(a); printf("Factorial of %d is %d\n",a,b); return 0; } |
Output 1 :
Example 2 : Implement a C code that allow Access for three users only names { max , ali , oralndo} Using Function Method .
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <stdio.h> #include <stdlib.h> #include<string.h> int findname(char names[5][20],char name[20]){ int find=0,i; for (i=0;i<5;i++){ if (stricmp(names[i],name)==0){ find=1; } } return find ; } int main() { char names[3][20]={"max","ali","orlando"}; char name[20]; int find=0; printf("please enter your name\n"); gets(name); find=findname(names,name); if (find==1){ printf("welcome %s\n",name); } else printf("Denied Access !!!\n"); return 0; } |
Output 2 :
0 Comment to "Functions"
Post a Comment