Module-III
a) Define function. Write a C program using functions with arguments and with
return value.
In this data is transferred between calling and called function. That means
called function receives data from calling function and called function also
sends the return value to the calling function.
Example
#include<stdio.h>
void add(int,int);
void main()
{
int a, b;
printf(“enter value”);
scanf("%d%d",&a,&b);
add(a,b);
}
void add (int x, int y)
{
int z ;
z=x+y;
printf ("The sum =%d",z);
return 0;
}
b) Write a C program using functions with no argument and no return value.
When a function has no arguments, it does not return any data from calling
function. When a function does not return a value, the calling function does not
receive any data from the called function. That is there is no data transfer
between the calling function and the called function.
#include <stdio.h>
void printmsg( )
{
printf ("Hello ! I Am A Function .");
}
int main( )
{
printmsg( );
return 0;
}
What is recursion? Write a C program to find factorial of a number using
recursion.
Recursion in C is a process where a function calls itself to solve a problem. It
allows a complex problem to be broken down into simpler sub-problems, each
of which is solved by invoking the same function. C Recursion requires a base
case that stops the recursive calls, preventing infinite loops.
Syntax
void do_recursion( )
{
... .. ...
do_recursion( );
... .. ...
}
int main( )
{
... .. ...
do_recursion( );
... .. ...
}
Program:
#include<stdio.h>
int fact(int n); /* Function Definition */
void main()
{
int num, res;
printf("Enter positive integer: ");
scanf("%d",&num);
res = fact(num); /* Normal Function Call */
printf("%d! = %d" ,num ,res);
}
int fact(int n) /* Function Definition */
{
int f=1;
if(n ==1 ) // base case
{
return(1);
}
else
{
f = n * fact(n-1); /* Recursive Function Call as fact() calls itself */
return(f);
}
}
What is inter-function communication? Write a C program for
exchanging of two values using Call by Value mechanism.
When a function gets executed in the program, the execution control is
transferred from calling a function to called function and executes function
definition, and finally comes back to the calling function. In this process, both
calling and called functions have to communicate with each other to exchange
information. The process of exchanging information between calling and called
functions is called inter-function communication
call by value or pass by value
In call by value parameter passing method, 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 effect the
values of actual parameters. That means, after the execution control comes
back to the calling function, the actual parameter values remains same. For
example consider the following program...
Program:
#include<stdio.h>
void main(){
int num1, num2 ;
void swap(int,int) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1, num2) ; // calling function
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
}
void swap(int a, int b) // called function
{
int temp ;
temp = a ;
a=b;
b = temp ;
}