You are on page 1of 27

FUNCTION

PRESENTED BY,
SHRIKANTH DODDAMANI

SSOCIATE PROFESSOR
HOD, DEPARTMENT OF COMPUTER SCIENCE
KARNATAK ARTS, SCIENCE AND COMMERCE COLLEGE, BIDAR
Function
Use of Function:
1. Reduce the complexity
2. Reusability
3. Make the process and debugging easier

Types of Function
4. User-defined functions
5. Pre-defined functions
While implementing user defined functions we need to
remember following three things.

I. Function Declaration
II. Function Call
III. Function Definition
Function Declaration

Function declaration is done above the main function

Syntax: Parameters or arguments


return_type function_name(datatype var1, datatype var2,…..);

or

return_type function_name(datatype,datatype,……);
Function Call

Usually user-defined functions is called inside the main function.


Syntax:
function_name(list of parameters);
Function Definition:

Written outside the main function

Syntax;
return_type function_name(list of parameters)
{
local variable declarations;
Executable statements;
return statement;
}
Parameters are of two types
I. Actual Parameters
are parameters as they appear in function calls.
II. Formal Parameters
are parameters as they appear in function declarations and
function definitions.
Formal arguments are very similar to local variables inside
the function. Just like local variables, formal arguments are
destroyed when the function ends.

Note: A parameter cannot be both a formal and an actual


parameter, but both formal parameters and actual parameters
can be either value parameters or variable parameters.
Calling Function:
Function which is having function call.

Called Function:
Function definition.
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a,int b) // function definition
{
int result; result = a+b;
return result; // return statement
}
Categories of Functions or Types of Function Definition:

I. Function with no arguments and no return values


II. Function with no arguments and one return value
III.Function with arguments and no return values
IV. Function with arguments and one return value
V. Function with multiple return values
1. Functions with no arguments, no return value

•The called function does not receive any data from calling function
•Calling function does not receive any data from the called function

/* program to calculate the area of square */


#include <stdio.h>
void area(); //function prototype or declaration
int main()
{
area(); //function call
return 0;
}
void area() //function definition
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = square_side * square_side;
printf("Area of Square = %d",square_area);
}
2. Function with no arguments and one return value

•Called function does not receive any data from calling function
•One result will be sent back to the caller from the function.

#include <stdio.h>
int area(); //function prototype with return type int
int main()
{
int square_area;
square_area = area(); //function call
printf("Area of Square = %d",square_area);
return 0;
}
int area() //function definition
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = square_side * square_side;
return square_area;
}
3. Function with arguments and no return values

•Function will accept data from the calling function as there are arguments,
•Since there is no return type nothing will be returned to the calling program. So it’s a
one-way type communication.

#include <stdio.h>
void area( int square_side); //function prototype
int main()
{
int square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
area(square_side); //function call
return 0;
}
void area(int square_side)
{
int square_area; square_area = square_side * square_side;
printf("Area of Square = %d",square_area);
}
4. Function with arguments and one return value

• Both the calling function and called function will receive data from each other. It’s like
a dual communication.

#include <stdio.h>
int area(int square_side); //function prototype with return type int
int main()
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = area(square_side); //function call
printf("Area of Square = %d",square_area);
return 0;
}
int area(int square_side)
{
int square_area;
square_area = square_side * square_side;
return square_area;
}
5. Function with multiple return values

•we can use functions which can return multiple values by using input parameters and output
parameters.
•Those parameters which are used to receive data are called input parameters and the
parameters used to send data are called output parameters.
•This is achieved by using address operator(&) and indirection operator(*).
#include <stdio.h>
void area_volume(int l, int *a, int *v); //function prototype
int main()
{
int l,a,v;
printf("Enter the side of square :");
scanf("%d",&l);
area_volume(l,&a,&v); //function call
printf("Area = %d\n Volume = %d",a,v);
return 0;
}
void area_volume(int l, int *a, int *v)
{
*a = l*l;
*v = l*l*l;
}
Call-by-value and Call-by-reference

There are two ways in which we can pass arguments to the function
Call by value

•Values of actual arguments are passed to the formal arguments.


•Any change made in the formal arguments does not effect the
actual arguments
•When function is called it does not affect the actual contents of
the actual arguments

a b c

10 20 30 Actual Parameters Function Call


2102 2104 2106
Copied
x y z

10 20 30 Formal Parameters Function definition


2108 2110 2112
#include <stdio.h> /* function declaration */
void swap(int x, int y);
int main ()
{
/* local variable definition */
int a = 100; int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
/* calling a function to swap the values */
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
/* function definition to swap the values */
void swap(int x, int y)
{
int temp;
temp = x; Output:
x = y; Before swap, value of a :100
y = temp; Before swap, value of b :200
} After swap, value of a :100
After swap, value of b :200
Call by reference

•When we call a function by passing the addresses of actual


parameters then this way of calling the function is known as call
by reference. 

•Here both actual and formal parameters refers to same memory


location.

•Therefore, any changes made to the formal parameters will get


reflected to actual parameters.
int x=10, y=20; Int fun(int *ptr1, int *ptr2)
fun(&x,&y); {
Printf(“x = %d , y = %d”,x,y); *ptr1=20;
*ptr2=10;
}

x y ptr1 ptr2

10 20 4000 4002

4000 4002 5010 5012


Recursion

•When a called function in turn calls another function a chaining


occurs.
•Recursion is a special case of this process, where a function
calls itself .

Example:
void main()
{
printf(“This is recursive function \n”);
main();
}
Recursive functions are of two types:
1. Direct Recursion
2. Indirect Recursion
1. Direct Recursion

A function is called direct recursive if it calls the same function


again:

Structure of direct recursion

fun()
{
//sample code
fun();
//sample code
}
Eg:
int fun(int n)
{
if(n==1)
return 1;
else
return 1+fun(n-1);
}

int main()
{
int n=3;
printf(“%d”,fun(n));
retrun 0;
}

output:
3
#include <stdio.h>
int sum(int n);
int main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum=%d", result);
}
int sum(int num)
{
if (num!=0)
return num + sum(num-1); // sum() function calls itself
else
return num;
}
Output:
Enter a positive integer:
3
6
2. Indirect Recursion

A function (let say fun) is called indirect recursive if it calls


another function ( let say fun2) and then fun2 calls fun directly or
indirectly.

Structure of indirect recursion

fun() fun2()
{ {
//sample code //sample code
fun2(); fun();
//sample code //sample code
} }
Program to print numbers from 1 to 10 in such a way that when number is odd, add 1 and
when number is even, subtract 1.

void odd() void even()


{ {
if(n<=10) if(n<=10)
{ {
printf(“%d”,n+1); printf(“%d”,n-1);
n++; n++;
even(); odd();
} }
return; return;
} }

main() Output:
{ 2 1 4 3 6 5 8 7 10 9
odd()
}

You might also like