Python - Functions

abys_learning_2024

ABYS

Posted on July 19, 2024

Python - Functions

FUNCTIONS, an awesome topic I learnt today. It's a shortcut for all lazy i.e., smart people who don't wanna waste their time typing inputs for several times.

What Is a Function?

In programming, rather than repeatedly writing the same code , we write a function and use it whenever and whereever it is needed.
It helps to improve modularity, code organization and reusability.

So, now let's see how to create a function.
A function contains,

  • function name - an identifier by which a function is called
  • arguments - contains a list of values passed to the function
  • function body - this is executed each time the function is called function body must be intended
  • return value - ends function call and sends data back to the program
def function_name(arguments): # key function name(arguments)
  statement                   # function body
  statement

  return value                # return value

Enter fullscreen mode Exit fullscreen mode

Some examples of how to use functions.

#Write a function greet that takes a name as an argument and prints a greeting message.

def greet(name):
    return(f"Hello, {name}!")
greet("ABY")

Hello, ABY!

Enter fullscreen mode Exit fullscreen mode

Here, we can replace return by print too.

#Write a function sum_two that takes two numbers as arguments and returns their sum.

def sum_two(a,b):
    return a+b

result = add(3,7)
print(result)

10

Enter fullscreen mode Exit fullscreen mode

#Write a function is_even that takes a number as an argument and returns True if the number is even, and False if it is odd.

def is_even(num):
    return num % 2 == 0

num = 5
print(is_even(num))

False

Enter fullscreen mode Exit fullscreen mode

#Write a function find_max that takes two numbers as arguments and returns the larger one.

def find_max(a,b):
    if a > b:
      return a
    else:
      return b

print(find_max(7,9))

9

Enter fullscreen mode Exit fullscreen mode

#Write a function multiplication_table that takes a number n and prints the multiplication table for n from 1 to 10.

def multiplication_table(n):
    for I in range (1,11)
    result = n * i 

print(f"{n} * {i} = {result}")
n = multiplication_table(int(input("Enter a no: ")))

Enter fullscreen mode Exit fullscreen mode

and the result is,

Enter a no: 5 # I've entered 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Enter fullscreen mode Exit fullscreen mode

#Write a function celsius_to_fahrenheit that takes a temperature in Celsius and returns the temperature in Fahrenheit.

This is how we normally do it..

celsius1 = 27
fahrenheit1 = (celsius1 * 9/5) + 32
print(f"{celsius1}°C is {fahrenheit1}°F")

celsius2 = 37
fahrenheit2 = (celsius2 * 9/5) + 32
print(f"{celsius2}°C is {fahrenheit2}°F")

celsius3 = 47
fahrenheit3 = (celsius3 * 9/5) + 32
print(f"{celsius3}°C is {fahrenheit3}°F")

27°C is 80.6°F
37°C is 98.6°F
47°C is 116.6°F

Enter fullscreen mode Exit fullscreen mode

It's cumbersome right??
Soo, what's the shortcut? Ofc using a function.

def celsius_to_fahrenheit(celsius):
  return (celsius * 9/5) + 32

celsius = float(input("Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is {fahrenheit}°F")

Celsius: 37.5
37.5°C is 99.5°F

Enter fullscreen mode Exit fullscreen mode

I've used input function to make it more compact...

#Write a function power that takes two arguments, a number and an exponent, and returns the number raised to the given exponent. The exponent should have a default value of 2.

def pow(num,exp = 2):
  return num ** exp


result = pow(5,exp = 2)
print(f"The number {num} raised to power 2 is ",{result})

Enter fullscreen mode Exit fullscreen mode

You can opt to use input fns and variables as well..

By now, it's understandable that for one problem we can use multiple
programs to solve it. It depends which we prefer to use.

.....

💖 💪 🙅 🚩
abys_learning_2024
ABYS

Posted on July 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Python - Functions
python Python - Functions

July 19, 2024

Python: Fun Function Facts
python Python: Fun Function Facts

July 4, 2020