0% found this document useful (0 votes)
2 views

Lab_5_Use_of_Functions

This document is a lab guide focused on the use of functions in programming, highlighting their importance for modular and organized code. It provides examples of various types of functions, including simple functions, functions with parameters, return values, recursion, and function pointers. Additionally, it lists tasks for further practice, such as finding the maximum of three numbers and implementing a recursive Fibonacci function.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab_5_Use_of_Functions

This document is a lab guide focused on the use of functions in programming, highlighting their importance for modular and organized code. It provides examples of various types of functions, including simple functions, functions with parameters, return values, recursion, and function pointers. Additionally, it lists tasks for further practice, such as finding the maximum of three numbers and implementing a recursive Fibonacci function.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 5

Use of Functions
Introduction
Functions are an essential part of programming. They allow for modular, reusable, and
organized code. In this lab, we will explore different types of functions, including
functions with parameters, return values, recursion, and function pointers.

Example 1: Simple Function


This function prints a greeting message when called.

#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet(); // Function call
return 0;
}

Example 2: Function with Parameters


This function takes two integers as arguments and prints their sum.

#include <stdio.h>
void add(int a, int b) {
printf("Sum: %d\n", a + b);
}
int main() {
add(5, 3);
return 0;
}

Example 3: Function with Return Value


This function multiplies two integers and returns the result.
#include <stdio.h>
int multiply(int x, int y) {
return x * y;
}
int main() {
int result = multiply(4, 5);
printf("Result: %d\n", result);
return 0;
}

Example 4: Recursive Function


This function calculates the factorial of a number using recursion.

#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
printf("Factorial of 5: %d\n", factorial(5));
return 0;
}

Example 5: Function Pointer


This example demonstrates the use of function pointers in C.

#include <stdio.h>
void greet() {
printf("Hello from function pointer!\n");
}
int main() {
void (*funcPtr)(); // Function pointer declaration
funcPtr = greet; // Assigning function to pointer
funcPtr(); // Calling function using pointer
return 0;
}
Tasks
1. Write a function that finds the maximum of three numbers.
2. Write a function that checks whether a given number is prime.
3. Implement a recursive function to calculate the nth Fibonacci number.
4. Write a program that uses a function pointer to switch between different mathematical
operations.

You might also like