Module - 4
Module - 4
Content
A large C program is divided into basic building blocks called C function. C function contains
set of instructions enclosed by “{ }” which performs specific task in a C program.
Example:
1.Developing a simple calculator program.
1.By using functions, we can avoid rewriting same logic/code again and again in a program.
2.We can call C functions any number of times in a program and from any place in a program.
3.We can track a large C program easily when it is divided into multiple functions.
Declaration: the function's name, return type, and parameters (if any)
Example:
2.Definition syntax:
Example:
Function Call
When we call any function control goes to function body and execute entire code. It is called
inside a program whenever it is required to call a function. It is only called by its name in the
main() function of a program.
Syntax Example
function-name(); add();
function-name(parameter/argument); add(a,b);
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result); Output:
Going to calculate the sumof two numbers:
}
Enter two numbers 10 24
int sum(int a, int b)
The sum is 34
{
return a+b;
}
Calling and Called function in c
Parameter Passing
Actual parameters are the values that are passed to a function when it is called.
Formal parameters are the variables declared in the function header that are used to receive the values of
the actual parameters passed during function calls.
Call by value and Call by reference
Call by Value:In call by value ,the copy of actual parameter values are copied to
formal parameters and these formal parameters are used in called function.The changes
made on the formal parameters does not affect the values of actual parameters.
Call by Value Example: Swapping the values of the two variables
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in
main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do
not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10
Call by value and Call by reference
Call by Reference:In Call by Reference,the memory location address of the
actual parameters is copied to formal parameters. This address is used to access
the memory locations of the actual parameters in called function.
Call by reference Example: Swapping the values of the two variables
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and
b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters
do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20,
b = 10
}
Difference between call by value and call by reference
Recursive Function
Any function which calls itself is called recursive function.A recursive function performs the tasks by
dividing it into the subtasks.
Factorial Program using recursion in C
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
Iterative functions
Iterative function is a function that repeats lines of code until a set of conditions is met.
there are three types of iteration statements: for, while, and do-while.
Difference between recursive and iterative function