Function
Function
Two types of function
Library function
scanf, printf, gets, puts, getch, sqrt etc.
User defined function
Function
/*Include header files*/
/*Include function prototypes*/
int main(void)
{
}
return
type
ret-type f1(param-list)
{ calling the function inside the main
}
ret-type f2(param-list)
{
}
Function
Function with no parameters should contain the keyword void in
prototype
Function Prototype
Function prototype declares a function
before its use
prior to its definition
Ends with semicolon
Example:
Function:
void myfunc(void)
{
}
Prototype
void myfunc(void);
Function
#include <stdio.h> Output:
void myfunc(void); In main
int main(void) In myfunc
{ Back in main
printf("In main\n");
myfunc(); main calling function/ caller
printf("Back in main\n"); myfunc called function
return 0; Control returns to calling function
} from called function
void myfunc(void)
{
printf("In myfunc\n");
}
Function Prototype
Prototype declares three attributes of a function
Its return type
Number of parameters
Type of parameters
Compiler need to know the type of data returned by the function
Default int assumed
Report illegal type conversions
main does not require prototype
Variable length argument list
scanf, printf
Prototype: int myfunc(int a, …);
Function
C program contains at least one function
main() must be present exactly once in a program
No limit on number of functions defined
There can be function which is defined but not called
There can be function which is declared but not defined
One function can not be defined inside other function
Two or more function can not have same name
Function and variable name can not be same
No statements can be written outside of function
Minimal function is
dummy(){}
Function
#include <stdio.h> void one(void)
void one(void); {
void two(void); printf("In one\n");
void three(void); }
int main() void two(void)
{ {
printf("In main\n"); printf("In two\n");
one(); }
two(); void three(void)
three(); {
printf("Back in main\n"); printf("In three\n");
return 0; } Output:
} In main
In one
In two
In three
Back in main
Function
It is possible to call one function from other function
main() can be called from other function
Function
#include <stdio.h> void two(void)
void one(void); {
void two(void); printf("In two\n");
void three(void); three();
int main() printf("Back in two\n");
{ }
printf("In main\n"); void one(void)
one(); {
printf("Back in main\n"); printf("In one\n");
return 0; two();
} printf("Back in one\n"); Output:
void three(void) } In main
{ In one
printf("In three\n"); In two
} In three
Back in two
Back in one
Back in main
Declaration Vs. Definition
Declaration: specifies the type of the object
Function prototype
Definition: causes storage for an object to be created
Function: which contains the body is definition
It is legal to define a function fully before its use
Eliminates need of separate prototype
Function
#include <stdio.h>
void myfunc(void)
{ Output:
In main
printf("In myfunc\n"); In myfunc
} Back in main
int main(void)
{
printf("In main\n");
myfunc();
printf("Back in main\n");
return 0;
}
Function Scope
#include <stdio.h>
void myfunc(void)
{ Error, i is local to main
printf(“i is %d\n”, i);
}
int main(void)
{
int i=10;
myfunc();
return 0;
}
Local variable cease to exist when the function returns
Function Scope (global variable)
#include <stdio.h>
int i;
void myfunc(void) Use of global variable
{
printf("i is %d\n", i);
}
int main(void)
{
i=10;
myfunc();
return 0;
}
Function Scope (local variable)
#include <stdio.h>
void myfunc(void)
{ Output:
int i=1; i is 1
i is 10
printf("i is %d\n", i);
}
int main(void)
{
int i=10;
myfunc();
printf("i is %d\n", i);
return 0;
}
Return
If no return type specified: default int assumed
when the return statement is encountered: the function returns
immediately
return statement can be used without return value
return ;
Used mostly by void functions
If the return value is not assigned to anything it is lost, but no
harm done
Return
#include <stdio.h>
void myfunc(void);
int main(void)
Output:
{ In main
printf("In main\n"); In myfunc
myfunc(); Back in main
printf("Back in main\n");
return 0;
}
void myfunc(void)
{
printf("In myfunc\n");
return ;
printf("In myfunc (will never be printed)\n");
}
Return values
#include <stdio.h>
#include <math.h>
int main(void)
{
double answer;
answer=sqrt(161.0);
printf("%lf\n", answer);
return 0;
}
sqrt is prototyped in math.h
Return values
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%lf\n", sqrt(161.0));
return 0;
}
Return
More than one values can not be returned
return a, b;
Reference can be used
Discussed later
Parameterized Function
#include <stdio.h>
void sub(int, int);
int main(void) Writing variable name in prototype is not necessary
{
sub(2, 6);
Order of argument is important
sub(5, 9);
return 0;
}
void sub(int x, int y)
{
printf("%d\n", x-y);
}
Parameterized Function
#include <stdio.h>
int sub(int, int);
int main(void) Writing variable name in prototype is not necessary
{
printf("%d\n", sub(2, 6));
printf("%d\n", sub(5, 9)); Order of argument is important
return 0;
}
int sub(int x, int y)
{
return x-y;
}
Function Arguments
To take arguments a function must have special variables
Known as formal parameters
When sub is called is argument is copied in the matching
parameter
Argument:
The value that is passed to a function
formal parameter
The variable that receive the value of the argument inside the function
Parameterized Function
Local variables of a function can not have same name as formal
parameters
Parameterized Function
#include <stdio.h>
void sum(int, int);
int main(void)
{ Error:
sum(2, 6); redefinition of formal parameter 'x‘
sum(5, 9); redefinition of formal parameter ‘y'
return 0;
}
void sum(int x, int y)
{
int x, y;
printf("%d\n", x+y);
}
Parameterized Function
#include <stdio.h>
double volume (double s1, double s2, double
s3)
{
return s1*s2*s3;
}
int main(void)
{
double vol=volume(5.3, 0.4, 10.7);
printf("%lf\n", vol);
return 0; s1 s2 s3
} 5.3 … 0.4 …. 10.7
38192 38210 39704
Example
#include <stdio.h>
int fact (int n) int main(void) {
{ printf("fact is : %d\n", fact(5));
int i, result=1; printf("pow is : %d\n", pow(5, 3));
for(i=2; i<=n; i++) return 0;
{ }
result=result*i;
}
return result;
}
int pow(int n, int x)
{
int i, result=1;
for(i=0; i<x; i++)
Output:
{ fact is : 120
result=result*n; pow is : 125
}
return result;
}
Function Call
Call by value
Have no effect on the argument used to call
Call by reference
Address of an argument is copied into the parameter
By default C uses call by value
This is why scanf argument gets address
Call by Value
#include <stdio.h>
void swap(int x, int y)
{
int t; Output:
t=x; a=2, b=5
a=2, b=5
x=y;
y=t; a b
} 2 … 5
int main() 37172 37285
{
int a=2, b=5; x y
printf("a=%d, b=%d\n", a, b); 2 … 3
swap(a, b);
38192 38279
printf("a=%d, b=%d\n", a, b); x y
return 0; 3 … 2
}
38192 38279
Call by Reference
#include <stdio.h>
void swap(int *x, int *y) Output:
{ a=2, b=5
int t; a=5, b=2
t=*x; a b
*x=*y;
2 … 5
*y=t;
} 37172 37285
int main() x y
{
int a=2, b=5; 37172 … 37285
printf("a=%d, b=%d\n", a, b); 38192 38279
swap(&a, &b); a b
printf("a=%d, b=%d\n", a, b); 5 … 2
return 0;
37172 37285
}
Argument name can be different from formal parameter name
Returning multiple value
Using global variable
Call by Reference
Returning multiple value (global variable)
#include <stdio.h>
#define PI 3.1416
double area, perimeter;
void ap(double r)
{
area=PI*r*r;
perimeter=2*PI*r;
}
int main(void)
{
ap(5);
printf("area=%lf, perimeter=%lf\n", area, perimeter);
return 0;
}
Returning multiple value (reference)
#include <stdio.h>
#define PI 3.1416
void ap(double r, double *a, double *p)
{
*a=PI*r*r;
*p=2*PI*r;
}
int main(void)
{
double area, perimeter;
ap(5, &area, &perimeter);
printf("area=%lf, perimeter=%lf\n", area, perimeter);
return 0;
}
String functions: strlen
#include <stdio.h>
int strlen(char *s)
{
char *p=s;
while(*p)
p++;
return p-s;
}
int main()
{
printf("%d ", strlen("cse 109"));
return 0;
}
Recursion
Something is defined in terms of itself
Also known as circular definition
A function that calls itself
When a function call itself recursively, each invocation gets a fresh
set of all the automatic variables
Recursive code is more compact and often much easier to write
Recursion Example
#include <stdio.h>
void recurse(int i)
{
Output:
if(i<10) Boundary Condition 9
{ 8
recurse(i+1); 7
printf("%d\n", i); 6
} 5
4
}
3
int main() 2
{ 1
recurse(0); 0
return 0;
}
Criteria of Recursive Function
A terminating condition
Recursive definition
Factorial (Iterative)
#include <stdio.h>
long fact_i(int n)
{ Output:
int f=1, i; 3628800
for(i=n; i>0; i--)
f=f*i;
return f;
}
int main()
{
printf("%ld ", fact_i(10));
return 0;
}
Factorial (Recursive)
#include <stdio.h>
long fact(long n)
{ Output:
3628800
if(n<=1)
return 1;
return n*fact(n-1);
}
int main()
{
printf("%ld ", fact(10));
return 0;
}
Factorial (recursive)
int facti(int n){ int factr(int n){
int i, product = 1; if(n == 0)
for(i = 2; i <= n; i++) return 1;
product *= i; //terminating condition
return product; else //recursive
} definition
return
n*factr(n-1);
}
strcpy (Iterative)
void icpy(char *s, char *t)
{
while((*s++=*t++));
}
strcpy (Recursive)
#include <stdio.h> int main()
void rcopy(char *dest, {
char *source) char str[80];
{ rcopy(str, "Recursion Test");
if(*source) printf(str);
{ return 0;
*dest++=*source++; }
rcopy(dest, source);
}
else
*dest='\0'; Output:
} Recursion Test
Mutual Recursion
#include <stdio.h> int main()
void f1(int a) {
{ f1(30);
if(a) f2(a-1); return 0;
printf("%d ", a); }
}
void f2(int b)
{
printf("* ");
if(b) f1(b-1); Error
}
Mutual Recursion
#include <stdio.h>
void f2(int b);
void f1(int a);
int main() Output:
{ * * * * * * * * * * * * * * * 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
f1(30);
return 0;
}
void f1(int a)
{
if(a) f2(a-1);
printf("%d ", a);
}
void f2(int b)
{
printf("* ");
if(b) f1(b-1);
}
Itoa
void itoa (int n, char s[ ]){ void itoa(int n, char s[]){
if ((sign = n) < 0) static int i;
n = -n; /* make it
positive */ if(n/10){
i = 0; itoa(n/10,s); //recursive
do{ definition
s [i++] = n % 10; + ‘0’; s[i++] = n % 10 + '0';
} while ((n /= 10) > 0); s[i + 1] = 0;
if (sign < 0) s [i++] = ‘-’; }
s [i] = ‘\0’; else{
reverse (s); s[i++] = n % 10 + '0';
} //terminating
return;
}
}
Binary Search
int binsearch(int x, int v[ ], int n) int bsearch(int x, int v[], int low, int
{ high)
int low, high, mid; {
low = 0; static mid;
high = n -1;
while(low <= high){ if(low > high) return -1;
mid = (low + high)/2;
if (x < v[mid]) mid = (low + high)/2;
high = mid – 1; if(x < v[mid])
else if(x > v[mid]) return bsearch(x, v,low,mid-1);
low = mid + 1; else if(x > v[mid])
else return mid; return bsearch(x, v, mid + 1, high);
} else
return -1; return mid;
} }