Dr. B. Hemanth Kumar Professor Dept of IT
Dr. B. Hemanth Kumar Professor Dept of IT
Hemanth Kumar
Professor
Dept of IT
Functions
Introduction
Function definition
Library Functions
User Defined Functions
Function prototype
Types of Function
Function Definition
Using Functions
Recursive functions
B.H.K
Introduction
#include <stdio.h>
int main( void )
{ int x, y,s;
FUNCTION printf(“Enter two integer values: ”);
scanf(“%d %d”, &x, &y);
s=x+y;
printf(“\nSum of %d and %d is %d\n”, x,y,s);
return 0;
}
O/P:
Enter two integer values: 6 7
Sum of 6B.H.K
and 7 is 13
Introduction
#include <stdio.h>
#include<math.h>
int main( void )
{ int x; float s;
FUNCTION printf(“Enter an integer value: ”);
scanf(“%d”, &x);
s=sqrt(x);
printf(“\n square root of %d is %f\n”, x,s);
return 0;
}
O/P:
Enter an integer
B.H.K
value: 25
Square root of 25 is 5.00000
Common Library Functions
#include <stdio.h>
printf()
#include <math.h>
fprintf()
sin(x)
scanf()
cos(x)
sscanf()
tan(x)
...
exp(x) // ex
log(x) // loge x #include <string.h>
log10(x) // log10 x strcpy()
sqrt(x) // x 0 strcat()
pow(x, y) // xy strcmp()
... strlen()
...
B.H.K
User Defined Functions
Basically there are two categories of
functions:
B.H.K
Function Definition
Function is a named independent fragment of code
that accepts zero or more argument values and
performs a specific task.
Every C program has at least one function which is
a main() function.
B.H.K
Advantages of Functions
Functions make programs easier to
understand
Functions can be called several times in
the same program, allowing the code to
be reused
Minimize the usage of Memory.
B.H.K
Creating a Function
Function Declaration(prototype)
Function definition
Function calling
B.H.K
Function prototype
A function declaration (prototype) tells
the compiler about a function's name,
return type, and parameters(arguments).
Syntax :
B.H.K
Simple Example
#include<stdio.h>
Function prototype double area(float,float);
main()
{ float a,b;
double A;
printf("Enter two values : ");
scanf("%f%f",&a,&b);
Function calling A=area(a,b);
printf("\nArea is :%f\n",A);
}
double area(float l,float b)
{
double a;
Function definition a=l*b;
return a;
B.H.K
}
Different types of functions
functions
without arguments and
without return type.
functions
with arguments and
without return type.
functions
without arguments and
with return type.
functions
with arguments and
with return type.
B.H.K
Introducing void key word
A function
without argument and
without return type
can be represented by using the key word
void
B.H.K
Function prototypes
functions without arguments and without return type.
Example :
void function_name(void)
no return type no arguments
void function_name( )
void factorial()
{ int n,i,f;
#include<stdio.h>
f=1;
void factorial();
printf("enter a +ve int number :”);
main()
scanf("%d",&n);
{
for(i=1;i<=n;i++)
printf("fact of 1st number-\n");
{
factorial();
f=f*i;
printf("fact of 2nd number-\n");
}
factorial();
printf(" FACT OF %d is :%d\n\n",
}
n,f);
}
B.H.K
Function with arguments and without return type
B.H.K
Function without arguments and with return type
B.H.K
Function with arguments and with return type
#include<stdio.h>
int factorial(int);
main() int factorial(int n)
{ int x,y, f; { int i,f;
printf("enter two +ve numbers"); f=1;
scanf("%d%d",&x,&y);
printf("fact of 1st number-\n"); for(i=1;i<=n;i++)
f=factorial(x); {
printf(" FACT OF %d is :%d\n\n", f=f*i;
x,f)
}
printf("fact of 2nd number-\n");
return f;
f=factorial(y);
}
printf(" FACT OF %d is :%d\n\n",
y,f)
}
B.H.K
Formal and Actual Parameters
#include<stdio.h>
Actual Parameters:
double area(float,float);
Parameters that are
main()
passed when calling the
{ float x,y;
function.
double A;
printf("Enter two values : ");
Actual parameters (x & y)
scanf("%f%f",&a,&b);
A=area(x,y);
printf("\nArea is :%f\n",A);
Formal Parameters:
}
Parameters that are
double area(float l,float b)
declared in function
{
definition.
double a;
a=l*b; Formalif parameters (l & b)
return a;
} B.H.K
Passing Arrays to functions
#include<stdio.h>
void display(int[]); void display(int[]);
main()
{ int x[5],i; void display(int a[])
for(i=0;i<5;i++) { int i;
{ printf("\nenter value: ");
scanf("%d",&x[i]); for(i=0;i<5;i++)
} {
printf("\n------Array elements-- printf(" %d \n",a[i]);
---\n"); }
display(x);
}
}
B.H.K
Recursive Functions
Calling a function within the same function
is a recursive function
There must be any conditional statement
in that function to terminate the recursion
process.
B.H.K
Recursive Function
#include<stdio.h>
int factorial(int n)
int factorial(int);
{
main()
int f;
{ int x,f;
if(n==1)
printf("\nenter value: ");
return 1;
scanf("%d",&x);
else
f=factorial(x);
f=n*factorial(n-1);
printf("\n---Factorial of %dis
return f;
%d -----\n",x,f);
}
}
Calling recursively
B.H.K
Call by value and Call by reference
Call by value: Passing actual value to the
calling function
Call by reference: Passing reference(
address) to the calling function
B.H.K
Call by Reference
#include<stdio.h>
void swap(int*,int*);
main() void swap(int*x,int*y)
{ int a,b; {
printf("Enter two values : "); int t;
scanf("%d%d",&a,&b); t=*x;
printf("\nA :%d, B : %d \n",a,b); *x=*y;
swap(&a,&b); *y=t;
}
Passing address
B.H.K
STRUCTURES
struct Books {
char title[50];
char subject[100];
int book_id; };
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700; B.H.K
STRUCTURES
struct Books {
char title[50];
int book_id; };
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
strcpy( Book1.title, "C Programming");
Book1.book_id = 6495407;
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 book_id : %d\n", Book1.book_id);
}
B.H.K
Structures as Function Arguments
struct Books {
char title[50]; int book_id; };
void printBook( struct Books book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
strcpy( Book1.title, "C Programming");
Book1.book_id = 6495407;
/* print Book1 info */
printBook( Book1 );
return 0; }
void printBook( struct Books book ) {
printf( "Book title : %s\n", book.title);
printf( "Book book_id : %d\n", book.book_id);
B.H.K
}