You are on page 1of 6

Session I Functions 1.

To display using function (No values are sent in function call ) void main() { void display(void); } void display() { printf(Hello); } 2. To find the sum of two numbers using function. void main() { void sum (int a, int b); sum(2, 3); } void sum (int a, int b) { int c; c=a+b; printf(\nSum = %d,c); } 3. Functions with no arguments and no return values void main() { void sum(void); sum(); } void sum(void) { int a, b, c; a = 5; b = 8; c = a+b; printf(\n Sum of numbers: %d, c);}

Data Structures Lab

/*function prototype*/

/*function prototype*/

Session I 4. Functions with arguments and no return values void main() { void sum(int a, int b); int a, b; a = 5; b = 8; sum(a, b); } void sum(int a, int b) { int c; c = a + b; printf(\n Sum of numbers: %d, c); } 5. Functions with arguments and one return values. void main() { int sum(int a, int b); int a, b, c; a = 5; b = 8; c = sum(a, b); printf(\n Sum of numbers: %d, c); } int sum(int a, int b) { int t; t = a+b; return (t); } 2

Data Structures Lab

Session I 6. Functions with no arguments but return a value. void main() { int sum(void); int c; c = sum(); printf(\n Sum of numbers: %d, c); } int sum(void) { int a, b, t; a = 5; b = 8; t = a+b; return (t); } 7. Functions that return multiple values. void main() { void operation(int a, int b, int *sum, int *diff); int a, b, c, d; a = 5; b = 8; operation(a, b, &c, &d); printf(\n Sum of numbers: %d, c); printf(\n Difference of numbers : % d, d); } void operation(int a, int b, int *sum, int *diff) { *sum = a + b;

Data Structures Lab

Session I *diff = a b; } Nesting of Functions (Function with a function) void main() { void sum(void); sum(); } void sum(void) { void display(int c); int a, b, c; a = 5; b = 8; c = a+b; display(c); /*Nesting of Functions*/ } void display(int c) { printf("\n Sum of entered numbers: %d", c); } Recursion #include <stdio.h> void main() { int factorial (int n); int n, facto; printf("\n Enter value for n : "); scanf("%d", &n); facto = factorial(n); printf("\n Factorial of given number: %d ", facto); } int factorial (int n) { int facto; 4

Data Structures Lab

Session I

Data Structures Lab

if(n==1) { return(1); } else { facto = n * factorial(n-1); return (facto); } }

Arguments passing methods 1. Call by value #include<stdio.h> void main() { void swap(int a, int b); int a, b; a = 8; b = 5; printf("\nValue of a : %d",a); printf("\nValue of b : %d",b); swap(a, b); printf("\nAfter Execution of SWAP()"); printf("\nValue of a : %d",a); printf("\nValue of b : %d",b); } void swap(int a, int b) { int t; t = a; a = b; b = a; } 2. Call by reference #include<stdio.h> 5

Session I void main() { void swap(int *a, int *b); int a, b; a = 8; b = 5; printf("\nValue of a : %d",a); printf("\nValue of b : %d",b); swap(&a, &b); printf("\nAfter Execution of SWAP()"); printf("\nValue of a : %d",a); printf("\nValue of b : %d",b); } void swap(int *a, int *b) { int t; t = *a; *a = *b; *b = t; }

Data Structures Lab

You might also like