0% found this document useful (0 votes)
14 views7 pages

Ex No 6

Code for exchange numbers

Uploaded by

priyajenat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Ex No 6

Code for exchange numbers

Uploaded by

priyajenat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.No.

6 Functions: call, return, passing parameters by (value,


Date: reference), passing arrays to function.

Aim:

To write a C program to demonstrate the concept of functions.

a. function Call and Return:

Algorithm:

Step1: Start

Step2: Declare the function as global

Step3: Call the function from main() function along with necessary parameters

Step4: Define the function and return the processed value to the calling function

Step5: Stop

Program:

#include <stdio.h>

// Function declaration

int add(int a, int b);

int main() {

int num1 = 10, num2 = 20;

int sum;

// Function call

sum = add(num1, num2);

printf("Sum: %d\n", sum);

return 0;

// Function definition

int add(int a, int b) {


return a + b;

Output:

b. Passing parameters by value:

Program:

#include <stdio.h>

// Function declaration

void modifyValue(int x);

int main() {

int num = 10;

printf("Before function call: %d\n", num);

// Function call

modifyValue(num);

printf("After function call: %d\n", num);

return 0;

// Function definition

void modifyValue(int x) {

x = 100;

Output:

c. Passing parameters by reference:

Program:
#include <stdio.h>

// Function declaration

void modifyValue(int *x);

int main() {

int num = 10;

printf("Before function call: %d\n", num);

// Function call

modifyValue(&num);

printf("After function call: %d\n", num);

return 0;

// Function definition

void modifyValue(int *x) {

*x = 100;

Output:

d. Passing Arrays to Function:

Program:

#include <stdio.h>

// Function declaration

void printArray(int arr[], int size);

int main() {

int numbers[] = {1, 2, 3, 4, 5};

int size = sizeof(numbers) / sizeof(numbers[0]);

// Function call
printArray(numbers, size);

return 0;

// Function definition

void printArray(int arr[], int size) {

for(int i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

Output:

Result:

Thus the C programs to demonstrate the concept of function call, return, passing
parameters by value and reference and passing arrays as parameters to function was executed
successfully and the output was verified.
Ex.No.7. Recursion

Date:

Aim:

To write a C program to implement recursion concept.

Algorithm:

Step1: Start

Step2: The condition under which the recursive function will stop calling itself.

Step3: The condition under which the function calls itself with modified arguments.

Step4: Write the function that adheres to the base and recursive cases.

Step5: Invoke the recursive function and display the results

Step6: Stop

Program:

#include <stdio.h>

int main()

int num;

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num < 0)

printf("Factorial of a negative number doesn't exist.\n");

}
else

int result = factorial(num);

printf("Factorial of %d is %d\n", num, result);

return 0;

int factorial(int n)

// Base case

if (n == 0)

return 1;

else

return n * factorial(n - 1);

Output:

Enter a positive integer: 5

Factorial of 5 is 120

Result:

Thus the C program to implement the concept of recursion was executed successfully and the
output was verified.

You might also like