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

Lecture 7-Functions With Examples

C programming

Uploaded by

Marcel Oderoh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 7-Functions With Examples

C programming

Uploaded by

Marcel Oderoh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Functions in Programming Language

Functions in C programming allow developers to break down a program into smaller, reusable
parts. For students studying Bachelor of Commerce (B.Com.), understanding basic programming
concepts like functions is helpful, even if their focus is on business and economics. Functions in
C can be applied in various scenarios, such as calculating taxes, discounts, or simple financial
operations.

What is a Function in C?

A function in C is a block of code that performs a specific task. Functions can take inputs
(parameters), process data, and return an output. They make programs easier to understand,
maintain, and reuse.

Syntax of a Function

return_type function_name(parameters)
{
// function body
// code to perform a task
return value; // optional return statement
}

• return_type: The data type of the value the function will return (e.g., int, float, void
if it doesn’t return anything).
• function_name: The name of the function.
• parameters: Input values (optional) that the function will use.
• function body: The actual code that performs the task.

Types of Functions in C

1. Predefined Functions: Functions that come built-in with the C language (e.g.,
printf(), scanf()).
2. User-defined Functions: Functions that the user writes to perform specific tasks.

Why Use Functions?

• Modularity: Break large problems into smaller tasks.


• Code Reusability: Write once, reuse multiple times.
• Readability: Makes code more organized and easier to understand.
• Ease of Debugging: Functions can be tested individually.
Examples of Functions

Here are some examples that can help commerce students understand how C programming
functions can be used in business-related problems.

1. Example 1: Function to Calculate Simple Interest

This function calculates simple interest based on the formula:

Where:

• P is the principal amount,


• R is the rate of interest,
• T is the time in years.

Code

#include <stdio.h>

// Function to calculate Simple Interest


float calculateSimpleInterest(float principal, float rate,
float time)
{
return (principal * rate * time) / 100;
}

int main()
{
float principal, rate, time, interest;

// Input values
printf("Enter Principal amount: ");
scanf("%f", &principal);
printf("Enter Rate of interest: ");
scanf("%f", &rate);
printf("Enter Time (in years): ");
scanf("%f", &time);

// Calculate simple interest by calling the function


interest = calculateSimpleInterest(principal, rate,
time);

// Output the result


printf("Simple Interest = %.2f\n", interest);

return 0;
}

• Explanation: This program defines a function calculateSimpleInterest which


calculates the interest. The main() function takes inputs from the user and calls the
function to calculate and display the result.

2. Example 2: Function to Calculate Discount on a Product

This function calculates the discount on a product based on a given percentage discount.

c
Copy code
#include <stdio.h>

// Function to calculate discounted price


float calculateDiscount(float price, float discountPercentage) {
return price - (price * discountPercentage / 100);
}

int main()
{
float price, discount, discountedPrice;

// Input original price and discount percentage


printf("Enter the original price of the product: ");
scanf("%f", &price);
printf("Enter the discount percentage: ");
scanf("%f", &discount);

// Calculate discounted price


discountedPrice = calculateDiscount(price, discount);

// Output the result


printf("The discounted price is: %.2f\n", discountedPrice);
return 0;
}

• Explanation: The function calculateDiscount takes the original price and discount
percentage as inputs, calculates the discount, and returns the final price after the discount
is applied.

3. Example 3: Function to Find the Maximum of Two Numbers

This function finds the maximum value between two numbers. This can be helpful for scenarios
like comparing two financial values (e.g., profits from two different products).

c
Copy code
#include <stdio.h>

// Function to find the maximum of two numbers


int findMax(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}

int main()
{
int a, b, max;

// Input two numbers


printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

// Find the maximum using the function


max = findMax(a, b);

// Output the result


printf("The maximum number is: %d\n", max);

return 0;
}

• Explanation: The function findMax compares two numbers and returns the larger of the
two. This can be useful for determining the highest value in financial comparisons.
3. Example 4: Function to Calculate Compound Interest

For finance students, calculating compound interest is important. The formula for compound
interest is:

Where:

• A is the amount (principal + interest),


• P is the principal,
• R is the rate of interest,
• T is the time.

Copy code
#include <stdio.h>
#include <math.h>

// Function to calculate compound interest


float calculateCompoundInterest(float principal, float rate,
float time) {
return principal * pow((1 + rate / 100), time);
}

int main()
{
float principal, rate, time, amount;

// Input values
printf("Enter Principal amount: ");
scanf("%f", &principal);
printf("Enter Rate of interest: ");
scanf("%f", &rate);
printf("Enter Time (in years): ");
scanf("%f", &time);

// Calculate compound interest using the function


amount = calculateCompoundInterest(principal, rate, time);
// Output the result
printf("Amount after Compound Interest = %.2f\n", amount);

return 0;
}

• Explanation: This program calculates the amount after applying compound interest using
the calculateCompoundInterest function.

Modular Approach Using Functions

Functions allow you to write modular and organized code. For example, a program that
calculates the financial statement of a company could be divided into several functions:

• A function to calculate revenue.


• A function to calculate expenses.
• A function to calculate net profit.

By splitting the tasks into functions, you can focus on each part independently and improve the
program’s readability and reusability.

5 Examples of Common Library Functions:


Example 6: Example of User Defined Function

7. Example 7: Pass by Value

8. Example 8: Pass by Reference


Conclusion

Functions are a powerful tool in C programming for solving complex problems in a modular
way. For Bachelor of Commerce (B.Com.) students, understanding how to implement functions
can help in automating business and financial calculations, such as interest, discounts, and profit
comparisons. These examples show how basic financial calculations can be simplified using C
functions.

You might also like