Coding for DUMMIES 08 - Functions in C programming
Today we discuss about the functions in c programming language.
Lets see some other programs.
Like in this program we can use multiple return statements in a function.
In this program we can see that the pass by value, parameter passing method.In this program we can see that the pass by reference, parameter passing method.
In below you can see that in the function declaration, you can omit the specification of the number of elements.
In this program all variable are local variables.
In this program g is a global variable.
In here we can see formal parameters.
Now lets see some example programs.
1. Finding triangular numbers
2. Greatest common divisor
3. Absolute Value
Now lets look further in to the c programming functions using some questions.
Q1 - Display all prime numbers between two Intervals using a function
Q2 - Write a function to return the trip cost which calculated using the given distance in
kilometers. Note: Take 35 LKR as travelling cost per kilometer.
Q3 - Write a function to convert the LKR currency into US dollars.
Q4 - Write a function to input source and destination and print the ticket for the route. Note:
You can decide the payment calculator logic and passengers can ask more than one ticket
at a time for different destinations.
Now lets see some questions using recursive functions.
Q1 - Find sum of natural numbers using a recursive function.
Q2 - Calculate the power of a number using a recursive function.
finally lets see a case study to finish practicing functions .
Case Study
Figure 1 depicts a flow chart used to calculate the sales commission paid for sales leaders
in a particular organization weekly basis. In order to calculate the weekly sales
commission, it reads the sales leader’s information per current week such as employee
number (empId), number of items sold (NoOfItemsSold) and unit price of the item sold
(unitPrice). Sales commission is calculated based on the sales leader’s earnings
(leaderEarnings) and each and every sales leader is assigned to sell only one item. There
are 20 sales leaders currently working in this organization and the commission is paid in
the seventh day of each week regularly. Flow chart for calculating the sales commission
is given in Figure 1.
Q1 - Write a program for both main line and internal process of the flowcharts depicted in
Figure 1.
Q2 - Modify and re-write the program written in Question 8 to print the total amount paid as
the weekly sales commission for all sales leaders.
Q3 - Suppose that the management of this organization has decided to allow sales leaders to
sell more than one different item as they wish. Modify and re-write the program written
in Question 9 to satisfy this requirement. Note: 50% of total sales earnings is paid as the
sales commission for a sales leader.
Now we hope you have learned about functions in C and how to use them in programming well . so until next make sure to practice programming everyday. practice is what makes you a good programmer.
contribution : - Sandakelum Kumarasiri @SK2208 5737
Ranishka Gunathilake @RG761 5641
Mahesh Wijenayaka @MW1513 5734
Ranishka Gunathilake @RG761 5641
Mahesh Wijenayaka @MW1513 5734