Functions in C Programming
Functions in C Programming
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
or
return_type function_name(datatype,datatype,……);
Function Call
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.
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:
•The called function does not receive any data from calling function
•Calling function does not receive any data from the called function
•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
a b c
x y ptr1 ptr2
10 20 4000 4002
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
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
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.
main() Output:
{ 2 1 4 3 6 5 8 7 10 9
odd()
}