C Program 4
C Program 4
#include<stdio.h>
global declarations;
void function_name(parameter list); //function declaration or function prototype
void main()
{
local declarations;
Statements;
Function_name(parameter list); //function call
...........
..........
}
void function_name(parameter list) //function definition
{
local declarations;
Statements;
}
• Where:
– return_type is the data type that the function returns.
– Function_name refers to the name of the function.
– The formal parameters are a comma separated list of variables that
receive the values from the main program when a function is called.
– The return statement returns the results of the function.
Program to add two integers. Marks a function add integers and display
sum in main() function.
#include<stdio.h>
int add(int a,int b); // function prototype
void main()
{
int a, b, sum;
printf(“enter two numbers\n”);
scanf(“%d%d”,&a,&b);
sum=add(a,b); //actual arguments
printf(“\n sum=%d”,sum);
}
int add(int a,int b) //formal arguments
{
int sum; //local declaration
sum=a+b;
return sum;
}
ARRAYS AND FUNCTION
2. Passing the whole Array
– Here , the parameter passing is similar to pass by reference.
– In pass by reference, the formal parameters are treated as aliases for actual
parameters.
– So, any changes in formal parameter imply there is a change in actual parameter.
• Example: program to find average of 6 marks using pass by reference.
include<stdio.h>
void average(int m[])
{
int i,avg;
int avg,sum=0;
for(i=0;i<6;i++)
sum=sum+m[i];
avg=(sum/6); Output:
printf(“aggregate marks=%d”,avg); aggregate marks=70
}
void main()
{
int m[6]={60,50,70,80,40,80,70};
average(m); //m is the base address
}
• Function with no arguments and with return value:
– In this , there is no data transfer from the calling function to the called function. But there
is a data transfer from called function to the calling function.
– When the function definition returns a value the calling function receives one value from
the called function.
Example: Program to illustrate function with no arguments and return value.
#include<stdio.h>
int add();
void main()
{
int sum;
sum=add ( );
printf(“\nsum=%d”,sum);
}
int add( )
{
int a,b,sum;
printf(“enter two numbers to add\n”);
scanf(“%d%d”,&a,&b);
sum=a+b;
return sum;
}
4. FUNCTION WITH ARGUMENTS AND RETURN VALUE:
– In this category there is a data transfer between the calling function and called function.
– When parameters are passed , the called function can receive values from the calling
function. When the function returns a value the calling function can receive a value from
the called function.
Example: program to illustrate function with arguments and return value
#include<stdio.h>
int add(int a, int b);
void main()
{
int a, b,sum;
printf(“enter two number to add\n”);
scanf(“%d%d”,&a,&b);
sum=add(a,b);
printf(“\n sum=%d”,sum);
}
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}
Parameter Passing Techniques
• The calling and called function need to communicate with each other to
exchange the data.
• The data flow between the calling and called functions can be divided into
– Call-by-value
– Call-by-reference
• Call by value:
– It is a one-way communication. The calling function can send data to
the called function, but the called function cannot send any data to the
calling function.
– The called function may change the values passed, but the original
values in the calling function remain unchanged.
void swap(int a,int b);
• Call by reference:
– We may often come across situations where we need to modify the values of actual
parameter in the called function.
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,flag=1;
printf(“enter the number\”);
scanf(“%d”,&n);
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(n<=1)
flag=0;
if(flag==1)
{
printf(“%d is a prime number”,n);
}
else
{
printf(“%d is not a prime number”,n);
}
}
Recursion
Direct Recursion:
it refers to a process where function calls itself directly as illustrated in
the figure below
Indirect Recursion:
It refers to a process where a function calls another function and that function calls back
the original function. The process of indirect recursion takes place as illustrated below:
0,1,1,2,3,5,8,13,.....