Lect 14 17 Functions
Lect 14 17 Functions
INTRODUCTION
•We are very much familiar with main(), printf() & scanf()
function
Function:
•User-Defined functions
•They are useful when program size is too large and complex.
Main Program
B1 B2
#include<stdio.h> Example
int add(int x, int y); // with out functions
int main()
{ int main()
add(); {
return 0; int num1, num2, res;
} printf("\nAdd First Number:\t");
scanf("%d", &num1);
int add()
{ printf("\nAdd Second Number:\t");
int num1, num2, sum; scanf("%d", &num2);
printf("\nAdd First Number:\t");
scanf("%d", &num1); res = num1+num2;
printf("\nAdd Second Number:\t");
printf("\nsum of Two Nums: %d", res);
scanf("%d", &num2); printf("\n");
sum = num1 + num2; return 0;
printf("\nAddition of Two Numbers: %d", sum); }
}
ELEMENTS OF USER DEFINED FUNCTION
Some similarities between functions and variables
• Both functions names and variables are considered
as identifiers and therefore they must follow the rules
for identifiers
Function definition
Function Call
Function declaration
Function Definition
• The function definition is an independent program
module that is specially written or implement the
requirements of the function.
printf(“%d”,sum(a,b));
ans = sum(a,b);
Formal and Actual Arguments
int sum(int, int); //declaration
void main()
{
int a=5, b=6,ans;
ans =sum(a , b); //calling function
printf(“Answer : %d”,ans);
}
//function definition
int sum(int x, int y)
{
int val;
val = x +y;
return val;
}
Actual arguments
• The arguments listed in the function calling
statement are referred to as actual arguments.
1. Function type
2. Function name
3. Parameter list
4. Termination semicolon
syntax
Example
int sum(int, int);
• The prototype declaration is always written
above the main() function definition.
Return values and their types
• We can pass n numbers of values to the called
function, but the called function can only
return one value per call.
• The return statement can take one of the
following form
return;
(or)
return(expression);
• The plain return does not return any value
• #include <stdio.h>
• int sum(); // function declaration
int main() {
int x = 20;
int y = 68;
printf("The numbers before swapping n1 and n2 %d %d \n",x, y);
swap(x, y);
printf("The numbers after swapping n1 and n2 %d %d \n",x, y);
return 0;
}
Call by Reference
• #include<stdio.h>
• void swap(int *n1 ,int *n2)
• {
• int temp = *n1;
• *n1 = *n2;
• *n2 = temp;
•
• }
int main() {
int n1 = 20;
int n2 = 68;
printf("The numbers before swapping n1 and n2 %d %d \n",n1, n2);
swap(&n1, &n2);
printf("The numbers after swapping n1 and n2 %d %d",n1, n2);
return 0;
}
C program to swap two numbers using functions
S.no C function syntax
• No direct approach.
#include <stdio.h>
void swap(int arr[])//Empty array
void swap(int arr[]);
{
int main()
int x=5,y=10,temp;
{
int arr[2];
temp=x;
swap(arr);
x=y;
printf("%d",arr[0]);
y=temp;
printf("%d",arr[1]);
return 0;
arr[0]=x;
}
arr[1]=y;
}
Swap two numbers with parameter and multiple return values
• #include <stdio.h>
• void swap(int arr[]);
• int main()
• {
void swap(int arr[])// array with values
• int arr[2]={1,2}; {
• swap(arr); int temp;
• printf("%d",arr[0]); temp=arr[0];
• printf("%d",arr[1]); arr[0]=arr[1];
• return 0; arr[1]=temp;
• } }
Passing Arrays to Functions
• Passing individual elements of an array.
• Passing entire array to a function.
// Passing individual Array Elements // Passing Complete Array
#include <stdio.h>
void printarrfn (int x,int y); #include <stdio.h>
int main() void printarrfn(int a[]);
{ int main()
int arr[5]={10,20,3,40,5}; {
int arr[5]={10,20,3,40,5};
printarrfn(arr[0],arr[4]);
printarrfn(arr);
}
}
void printarrfn(int x,int y)
{ void printarrfn(int a[])
printf("0th index:%d\n4th Index {
%d",x,y); for(int i=0;i<5;i++)
}
{
printf("%d\n",a[i]);
}
}
Recursive Function
• A function that calls itself is known as a recursive function. And,
this technique is known as recursion.
void function1()
{
... .. ...
function1();
... .. ...
}
int main()
{
... .. ...
function1();
... .. ...
}
Example: Sum of Natural Numbers Using Recursion
• #include <stdio.h>
int sum(int n) {
• int sum(int n);
if (n != 0)
// sum() function calls itself
• int main() { return n + sum(n-1);
• int number, result; else
return n;
• printf("Enter a positive integer: "); }
• scanf("%d", &number);
• result = sum(number);