Functions are tricky yet very important part of a program. It makes a program look aesthetically pleasing by abstracting away all the complexities in a program. Want to perform a particular task, call the function and voila! But you can use custom functions only when you are able to create it well.
Often times we see ourselves debugging randomly to straighten out the error. However, if once we are confident of how functions are created and called, then we can use it seamlessly and almost bug free.
Here I have explained by creating a function to calculate discount in C.
#include <stdio.h>
//function prototype at the header
float discount(float price, int percentage);
//datatype name(arg1, arg2,...)
int main(void)
{
float mrp;
printf("Enter mrp: ");
scanf("%f", &mrp);
int off;
printf("Enter discount percent: ");
scanf("%i", &off);
//calling our discount function with both arguments
float sale = discount(mrp,off);
printf("Sale price: %.2f", sale);
}
float discount(float price, int percentage)
{
//use all the arguments to define function statement(s)
return price * (100 - percentage)/100;
/* return statement will not print anything, rather it gives the value
back to the main function where our custom function is used.
It can then print. Variable too can be used but don't forget to return.
*/
}
See how in the main function, I asked for user input then discount function calculated for us.
For those more comfortable in python, here’s a similar implementation.
# function defined at the top of the program
def discount (price, percentage):
bill = price * (100-percentage)/100
return bill
mrp = float(input("Enter the mrp: "))
off = int(input("Enter the discount %: "))
# calling the function
sale_price = discount(mrp,off)
print("Sale price: ", sale_price)
One difference you may have noted in our function implementation in C compared to python. In C, once you have declared a function in the header of the program, you can define it later. However in python, there's no "declare". There's the definition (which must be complete) or there's nothing.
This looks a bit messy. How about I just use a simple function that does nothing but prints Hare Krishna
#include <stdio.h>
void hk (int n);
int main(void)
{
hk(4); // calling the function
}
void hk (int n) // defining the function
{
int i;
for (i = 0; i<n; i++)
{
printf("Hare Krishna\n");
}
}
similar program in python,
def hk(n): # defining the function
for i in range(0,n):
print("Hare Krishna\n")
hk(4) # calling the function
Worth noting here, in our function, if we don’t print but return, the terminal will not show any value because return
just hands back the value to the main
function but doesn’t do anything on its behalf. Here in main function, I only called the function with arguments. If there was no printf
but only return
, our program would still be correct but we wouldn’t be able to see any output.
We use user-defined as well as built-in(library ) functions all the time. But, if we just pause and analyze the structure of a function, we are less likely to commit mistakes in our program. Also, this structure is specifically for user-defined functions If we are using, library functions, in C, just include a header file and in python, use directly.
So if I have to define Anatomy of a function
in pseudocode, it will be like this -
function (argument 1, argument2)
{
body of the function
return
}
main function
{
call the function
print to display the output
}
PS - When I first started learning programming, I thought keywords used will have some really complex meaning. However, that is not the case. So you see some unknown word in books/blogs/videos, most of the time it’s just plain english. Like inheritance means retaining characteristics from parents. Similarly, inheritance in programming means retaining characteristics from base class/object. This applies to other concepts and keywords as well.