~Functions and Arguments~

~Functions and Arguments~

ยท

4 min read


Definition:-

A function is simply a block of code designed to perform a specific task. We can call a function to print multiple things at once, calculate different numbers without having to repeat the operations again and again for each number, and so on.

We declare function by the command "def" followed by the variable in brackets and then end it with a colon ":". Whatever we do after the colons is called the body of the function and determines what action the function performs.


We are gonna understand everything with an example, eg: We will define a function that will automatically give 20% of any number that we want without repeating the calculations.

def fraction(x):
   output = x*20/100
   return output

Now, we have a function called "fraction" which will give the said results every time we call it along with a "return" call which returns the value of "output" written inside the body. How to call it issss-

fraction(550)

^ The output of this will be 110

And if we wanna take any other number, we can simply write the same command for any number we want. ( Ps. Try it out :p )


For a more complex example, as the best way to understand functions is through understanding more and more codes, we are gonna calculate the salary of workers working at an hourly wage of $15, in which they get an 8% tax deduction. We have to find out how much we have to pay according to how many hours they worked.

For this:

1.) We have to ultimately calculate their wage per hour so function can be called "wage". The variable will be the thing that will be changing accordingly, which is their hours worked, so the variable can be called "hours".

def wage(hours):

2.) Next, we do the calculations for finding out the final pay, and for that, we declare some local variable named "pay_before_tax" and "pay_after_tax" and do the necessary calculations.

To do it, we must firstly find out how many hours they worked, then multiply it by the hourly wage, and to get the final pay, we deduct 9% of the total amount from it to get the final pay. This is done as:

def wage(hours):

   pay_before_tax = hours * 15      #15 is the hourly wage

   pay_after_tax = pay_before_tax - pay_before_tax * 9 / 100

   return pay_after_tax

3.) Now the function has been written, to call it, we just have to simply type "wage(x)" where x is the number of hours that particular worker have worked.

For eg: If the worker has worked for 40 hours, we would do-

wage(40)

^ the above code will return 546


Now it is important to note that all the above examples have been for functions that have had only one argument. To put it simply, after defining a function, there's only one variable inside the brackets, also k/a argument.

We can declare functions with multiple arguments as well which seems to me are much more valuable than functions with one or even no arguments sometimes, and for that reason, we are gonna take one more final example to understand how a function works with multiple arguments.

This example is gonna be related to the previous one to make things much easier to understand. Earlier, we decided that the hourly wage of the workers would be $15 an hour with a 9% tax. But now, we are gonna be having a function in which we can decide all three of the parameters, which are:

  1. The number of hours worked

  2. The wage/hr

  3. The % of tax deduction

So, starting off, we are going to be declaring a function with three arguments :-

def salary(hour_pay, hours, tax):

Now, we define the conditions in the body of the argument similar to what we have done before.

def salary(hour_pay, hours, tax):

   before_tax=hours * hour_pay

   after_tax= before_tax - before_tax * tax / 100

   return after_tax

And now, we can call it using salary(x,y,z) where

x = hourly wage

y = number of hours worked

z = percentage of tax

Say, for example, if a person worked 60 hours at $20/hr and the tax cut was 7%, we will simply do:

 salary(20, 60, 7)

^ This will return 1116.0


And That sums up our topic of Functions and Arguments. We are gonna be learning about Data Types next and I shall again explain it here as best I can. Thanks for reading and I hope you understood what we did today :D


Peace~๐Ÿ•Š๏ธ

Yash K.

ย