Module 4
Module 4
Module 4
USER – DEFINED FUNCTIONS
INTRODUCTION:
A function is a block of code that performs a particular task.
Library functions are those functions which are already defined in C library, example printf (),
scanf (), strcat () etc. You just need to include appropriate header files to use these functions. These
are already declared and defined in C libraries.
A User-defined functions on the other hand, are those functions which are defined by the user at
the time of writing program. These functions are made for code reusability and for saving time and
space.
Function Call:
When a function is called, control of the program gets transferred to the function.
Syntax:
function_Name (Actual Argument list);
Example:
sum_Num (10,20);
When the function is called, the control flow of the program move to function definition and
statements executes the inside body of the function
Function Definition:
The function definition is an expansion of function declaration. It contains codes in the
body part of the function for execution program by the compiler – it contains the block of code for
the special task.
The syntax of the function definition
return_Type function_name (formal parameter_1, parameter_2....)
{
//statements
//body of the function
}
1. function name – name of the function is sum_Num ()
2. return type – the return type of the function is int
3. arguments – two arguments of type int are passed to the function
4. body of function – specifies actions to be performed
return statements
Return statements terminate the execution of the function and return value to calling
the function. Also, return statements control of the program control and moved to the calling
function.
Syntax:
return (expression);
Example:
return (result);
return (x+y);
Program Examples:
1. Write a C program to perform division of two numbers using functions.
#include <stdio.h>
int division(int x,int y); //function declaration or prototype
void main()
{
int a, b, div;
printf("Please enter 2 numbers for division\n");
scanf("%d%d",&a,&b);
div=division(a, b); //function call
printf("The result of division is :%d",div);
}
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
void main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
}
3. Write C program to find SUM and AVERAGE of two integer Numbers using User Define
Functions.
#include <stdio.h>
int sumTwoNum(int, int); /*to get sum*/
float averageTwoNum(int, int); /*to get average*/
void main()
{
int number1, number2;
int sum;
float avg;
/*function calling*/
sum = sumTwoNum(number1, number2);
avg = averageTwoNum(number1, number2);
4. Write C program to print Table of an Integer Number using User Define Function.
#include <stdio.h>
void printTable(int); /*function declaration*/
void main()
{
int number;
return 0;
}
void printTable(int num)
{
int i;
void Addition()
{
int Sum, a = 10, b = 20;
Sum = a + b;
printf("\n Sum of a = %d and b = %d is = %d", a, b, Sum);
}
#include<stdio.h>
int Multiplication(int, int);
void main()
{
int a, b, Multi;
printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);
Multi = Multiplication(a, b);
printf("\n Multiplication of %d and %d is = %d \n", a, b, Multi);
}
With this method, the changes made to the With this method, using addresses we would
dummy variables in the called function have have an access to the actual variables and
no effect on the values of actual variables in hence we would be able to manipulate them.
the calling function.
In call by values, we cannot alter the values In call by reference, we can alter the values
of actual variables through function calls. of variables through function calls.
Values of variables are passes by Simple Pointer variables are necessary to define to
technique. store the address values of variables
Write a C program to calculate the sum, mean and standard deviation of an array elements using
functions.
#include<stdio.h>
#include<math.h>
int sum(int a[], int n);
int sum1(int a[], int mean, int n);
void main()
{
int i, n, a[100];
float m, v, sd, sum, sum1;
printf(“Enter total number of elements\n”);
scanf(“%d”, &n);
printf(“Enter array elements\n”);
for(i=0;i<n;i++)
{
scanf(“%d”, &a[i]);
}
sum = sum(a, n);
printf(“Sum of the elements = %d\n”, sum);
m = sum/n;
printf(“Mean = %f\n”, m);
sum1 = sum1(a, m, n);
v = sum1/n;
sd = sqrt(v);
printf(“Standard deviation = %f\n”, sd);
}
int sum(int a[], int n)
{
int i, sum=0;
for(i=0;i<n;i++)
{
sum = sum + a[i];
}
return sum;
}
Recursion
INTRODUCTION
In C, when a function calls a copy of itself then the process is known as Recursion. To
put it short, when a function calls itself then this technique is known as Recursion. And the function
is known as a recursive function.
Syntax:
void main()
{
... .. ...
recurse();
... .. ...
}
void recurse()
{
... .. ...
recurse();
... .. ...
}
Example: Write a C program to find the Sum of Natural Numbers Using Recursion
#include <stdio.h>
int sum(int n);
void main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
}
int sum(int n)
{
if (n != 0)
return n + sum(n-1);
else
return n;
}
scanf("%d",&n);
printf("Fibonacci series\n");