Module-2 Functions
Module-2 Functions
BASICS OF C
What will you learn?
Functions
Function Definition
Calling Functions
Recursion
Function with Arrays
FUNCTIONS
Functions break large computing tasks into smaller
ones.
C comes with rich and valuable set of library functions that makes
programmer’s work easier.
FUNCTION
DEFINITION:
When a function is defined, space is allocated for that function in
memory. A function definition includes the following elements:
1. Function header
Example:
Any function can call any function. When a function calls another
function, the program flow is transferred to the called function, after
the completion of the execution of the called function the program
flow is transferred back to the calling function.
The arguments that are passed from the calling function are
called actual arguments and the arguments that receive
these values are called formal arguments.
FUNCTION CALLING EXAMPLE
Actual Parameters
//Calling Function
return <expression>;
The user can only use these functions but can’t change or modify them.
These functions are defined in the header file. When you include the
header file, these functions are available for use.
argument declaration
Executable statement
……
…… ……
}
CONTD…
In function structure all parts not essential, it is based on
the requirement of the program and some sections or
parts may be absent.
AND
void NO RETURN VALUE
addition()
{
int sum, a = 10, b = 20;
sum = a + b;
printf("\n sum of a = %d and b = %d is = %d", a,
b, sum);
}
FUNCTION WITH NO ARGUMENT
METHODS
OR
1. Call by Value
2. Call by Reference
CALL BY VALUE:
In this method, values of the variables are passed from the calling
function to the called function
1. In this method, the called creates the new variables to store the
values that are passed to it.
2. This means the called function uses a copy of the actual arguments
to perform the defined task.
4. This is because all the changes are made to the copy of the variables
but not to the actual variables.
EXAMPLE:
int main () void swap(int x, int y)
{ {
int a = 10; int z;
int b = 20; z = x;
printf(“%d%d”,a,b); x = y;
swap(a, b); y = z;
printf(“%d%d”,a,b); }
return 0;
X Y Z
} A B
10 20 10 20 10
1000 10 1100 20
2000 1000 2100 1100 2200
10 1000 20 1100 10
2200 2000 1000 2100 1100
RECURSIVE
FUNCTIONS
RECURSIVE
FUNCTIONS
A Recursive function is defined as a function that calls itself to solve a
smaller part of its task repetitively to solve the whole problem. This process
is called Recursion.
Recursive Case, in which first the problem at hand is divided into smaller
sub – parts. Second the function calls itself but with sub parts of the
problem obtained in the first step.
The following are the rules for designing a recursive function:
1. First determine the base case
2. Then determine the recursive case
3. Finally combine the base case and recursive case into a
function
To understand the recursive functions, take an example of
calculating the factorial of a number. The factorial of the number is
the product of the integral values from 1 to the number.
This means,
n! = n * (n – 1)!
Here, n! is obtained by multiplying n with (n – 1)!
Assume n=3 so 3! = 3 x 2 x 1
This can be written as
3! = 3 x 2!, where 2! = 2 x 1!
So, 3! = 3 x 2 x 1!, where 1! = 1
So, 3! = 3 x 2 x 1.
• If one should observe the problem carefully, here the product is
Factorial (0) = 1
FUNCTION WITH ARRAYS
#include<stdio.h> float findAverage(int marks[])
float findAverage(int marks[]); {
int i, sum = 0;
int main() float avg;
{ for (i = 0; i <= 4; i++)
float avg; {
int marks[] = {99, 90, 96, 93, 95}; sum += marks[i];
avg = findAverage(marks); }
printf("Average marks = %.1f", avg); avg = (sum / 5);
return 0; return avg;
} }