Functions in C
By:-
Dr. Vani Kapoor Nijhawan
Assistant Professor, VIPS
Functions: Definition & Advantages
A function is a block of code defined to perform a particular task. {…..}
C has 2 categories of function:
1. Library functions 2. User defined functions
There are many advantages of using functions:
1. Length of the source program is reduced.
2. Easy to locate and debug errors.
3. Saves debugging time .
4. A function can be used by many other programs.
5. It facilitates top to down modular approach.
6. Promotes reusability of code.
Dr. Vani Kapoor Nijhawan 2
Multi Function Program Structure
Ref: E balagurusamy
complete program (abc.c)
void main() void function 1()
{ ……….. { ………. }
function 1(); // fun call
.....function 2();
……. function 1();
…}
void function 2() void function 3() // function definition
{ { ………… }
………….
function 3();
…….
}
Dr. Vani Kapoor Nijhawan 3
Elements of User Defined Functions
Ref: E balagurusamy
Function Declaration
Function Call
Function Definition // body define
Function definition includes the following:
1. Function name
2. Function Type
3. List of Parameters
4. Local Variable declarations
5. Function statements
6. a return statement
Dr. Vani Kapoor Nijhawan 4
Functions: Example
#include<stdio.h>
void add(); // function declaration
void main()
{
add(); //function call
}
void add()
{
int a,b,c;
printf(“enter numbers to be added”); //function definition
scanf(“%d %d”, &a, &b);// 10 34
c=a+b; // 10 ,34
printf(“result=%d”,c);
}
Dr. Vani Kapoor Nijhawan 5
User Defined Function: Example
void add(int,int); // function declaration
void main()
{ //function arguments / parameters
int a,b;
printf(“enter numbers to be added”);
scanf(“%d %d”, &a, &b);// 10 34
add(a,b); // function call ,add(10,34)
}
void add(int c, int d) // (10,34)
{
int e; // function definition
e=c+d; // 10 ,34
printf(“result=%d”,e);
}
Dr. Vani Kapoor Nijhawan 6
Function with Argument & Return Statement
float add(float,float); // function declaration
void main()
{ //function arguments / parameters
float a,b,c;
printf(“enter numbers to be added”); function definition
scanf(“%f %f”, &a, &b);// 10 34
c=add(a,b); // function call ,add(10,34)
printf(“Sum=%f”,c);
}
float add(float c, float d) // (10,34) //return type of a function
{
float e; // function definition
e=c+d; // 10 ,34
return e;
}
Dr. Vani Kapoor Nijhawan 7
Questions
WAP to calculate simple interest using functions.
WAP to check whether a number is an Armstrong number or not using
functions with return value.
WAP to calculate factorial of a number using functions with return statement.
Fibonacci Series.
Prime number.
Swapping of 2 nos with or without a temporary variable.
Dr. Vani Kapoor Nijhawan 8
Recursion: Definition & Conditions
Recursion is a process in which a function calls itself.
Necessary Conditions for recursion:
1. Base case / Stopping condition
2. Call to itself
Void main()
{ fun_a(); }
void fun_a() // recursive fun
{ ……..
fun_a();
}
Dr. Vani Kapoor Nijhawan 9
Armstrong No. (153,370,1634)
#include <stdio.h>
void main()
{ int n, sum=0, r, n1,i=0;
printf("Enter no");
scanf("%d", &n);
n1=n;
while(n>0)
{ n/=10;
i++;
} n=n1;
while (n>0)
{ r= n%10;
sum+= pow (r,i);
n/=10;
}
if(sum==n1)
printf("Armstrong");
else printf("Not an Armstrong");
}
Dr. Vani Kapoor Nijhawan 10
Questions
Q1. WAP to print the terms of Factorial using recursion.
#include <stdio.h>
int fact(int);
int main()
{ int n, sum=0, r, n1,a;
printf("Enter no");
scanf("%d", &n);
a= fact(n);
printf("%d", a);
}
int fact(int n)
{ if (n==0 || n==1)
return 1;
else return n* fact(n-1);
}
Dr. Vani Kapoor Nijhawan 11
Call By Value & Call By Reference
In call by value, the values of the function arguments are passed during
function call while in call by reference, the reference/address of the function
arguments are passed during function call.
In call by value, changes are made to the local copies of the arguments, while
in call by reference, changes are made at the address values.
In call by value, old values are retained. In call by reference, old values are
updated with new values.
In call by value, changes are temporary but in call by reference, changes
made are permanent.
Dr. Vani Kapoor Nijhawan 12
Call by Value Vs Reference
add(a,b) // 5 6
add(&a, &b) // & => address operator// * => value at address operator
void main() p=32144 23772
{int a,b; a 5 6 b
scanf(“%d %d”,&a,&b);
add(a,b); // *p where, p is a pointer
}
void add(int a1,int b1)
{ int sum;
sum=a1+b1;
printf(“sum=%d”,sum);
}
Dr. Vani Kapoor Nijhawan 13