0% found this document useful (0 votes)
3 views

Types of Functions in C

C program

Uploaded by

Yash Jade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Types of Functions in C

C program

Uploaded by

Yash Jade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Functions in C

Functions in C:
Functions are sub-programs that are used to compute a value or perform a task. They
can’t be run independently. In C language, there are two kinds of functions are exists.

 Built-in functions
 User-defined functions

1. Built-in functions:
Built-in functions are used to perform standard operations.
Example:
 scanf(), printf()

2. User-defined functions:
User-defined functions are self-contained blocks of statements that are written by the
user to compute a value. It is used to compute and return the value of a specific data
type to the calling program.

Function Declaration:
The general syntax of a function declaration is given below :
 type name(type argu1, type argu2, ......, type argun)
 {
 <local declaration>
 ----------
 <Statement block>
 ----------
 return (variable);
 }

where type is the data type of the value returned by the function and arguments
expected.
argu1, argu2, ……, argun are the arguments that are variables that will receive the
values from the calling program. name is the name of the function by which the function
is called by the calling program.

Actual Arguments:
The arguments listed in the function calling statement that is called Actual Arguments.
Example:

Formal Arguments:
The arguments used in the function declaration that is called Formal Arguments.
Example:
Recursion:
A function calls itself again to compute a value that is called recursive function
or recursion. A function is called by the main program but in recursion, the same
function is called by itself repeatedly.
Example :
 int fact(int x)
 {
 if(x==0)
 return(1);
 else
 return(x*fact(x-1));
 }

C Program to Find Factorial of a


Number using Function
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 clrscr();
 printf("Enter a Postive Number: ");
 fact();
 getch();
 }
 fact()
 {
 int i,f=1,n;
 scanf("%d",&n);
 for(i=1; i<=n; i++)
 {
 f=f*i;
 }
 printf("The Factorial of a Given Number is %d",f);
 return f;
 }
Output:

120

C Program to Reverse a Number


using Function
C Program to Reverse a Number:
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int num, rev = 0, rem;
 printf("Enter the number to reverse: ");
 scanf("%d", &num);
 while (num != 0){
 rem = num % 10;
 rev = rev * 10 + rem;
 num = num/10;
 }
 printf("The reversed number is: %d", rev);
 getch();
 }
Output:
 Enter the number to reverse: 71874
 The reversed number is: 47817
C Program to Reverse a Number using Function:
 #include<stdio.h>
 #include<conio.h>
 Reverse(int n)
 {
 int sum=0;
 while (n!=0)
 {
 sum = sum*10 + n%10;
 n /= 10;
 }
 return sum;
 }
 void main()
 {
 int rev, num;
 clrscr();
 printf("Enter a Positive Number: ");
 scanf("%d", &num);
 rev = Reverse(num);
 printf("The Reverse of given number %d is: %d", num, rev);
 getch();
 }
Output:
C Program to Reverse a String
using Function
 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 void main()
 {
 char sk[15];
 clrscr();
 printf("Enter the String: ");
 gets(sk);
 printf("\nThe Reversed String is %s",strrev(sk));
 getch();
 }
Output:

You might also like