Lecture 7-Functions With Examples
Lecture 7-Functions With Examples
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.
Here are some examples that can help commerce students understand how C programming
functions can be used in business-related problems.
Where:
Code
#include <stdio.h>
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);
return 0;
}
This function calculates the discount on a product based on a given percentage discount.
c
Copy code
#include <stdio.h>
int main()
{
float price, discount, discountedPrice;
• 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.
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>
int main()
{
int a, b, 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:
Copy code
#include <stdio.h>
#include <math.h>
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);
return 0;
}
• Explanation: This program calculates the amount after applying compound interest using
the calculateCompoundInterest function.
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:
By splitting the tasks into functions, you can focus on each part independently and improve the
program’s readability and reusability.
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.