Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript Functions – Reusable Blocks of Code

In the previous article, we learned about loops, which allow us to repeat code efficiently. However, as programs grow larger, repeating the same code in multiple places becomes difficult to manage.

To solve this problem, JavaScript provides Functions.

Functions allow us to group related statements into a reusable block of code that can be executed whenever needed.

In this article, we will learn:

  • What functions are

  • Why functions are needed

  • Function declaration

  • Function calling

  • Parameters and arguments

  • Return values

  • Function expressions

  • Arrow functions

  • Variable scope

  • Practical examples

  • Real-world applications


What is a Function?

A function is a reusable block of code designed to perform a specific task.

Think of a function as a machine.

Example:

Input
  ↓
Function
  ↓
Output

Why Functions are Needed

Without functions:

document.write("Welcome");
document.write("<br>");

document.write("Welcome");
document.write("<br>");

document.write("Welcome");
document.write("<br>");

Code becomes repetitive.

Using a function:

function showMessage()
{
    document.write("Welcome");
    document.write("<br>");
}

showMessage();
showMessage();
showMessage();

The same code can be reused easily.


Advantages of Functions

  • Reusability

  • Better organization

  • Easier debugging

  • Reduced code duplication

  • Improved readability


Function Declaration

A function is declared using the function keyword.


Syntax

function functionName()
{
    statements;
}

Example

function greet()
{
    document.write("Hello World");
}

Function Calling

Declaring a function does not execute it.

To execute it, we must call it.

Example:

function greet()
{
    document.write("Hello World");
}

greet();

Output:

Hello World

Dry Run

Function Created
      ↓
greet() Called
      ↓
Function Executes
      ↓
Hello World Displayed

Multiple Function Calls

function greet()
{
    document.write("Hello<br>");
}

greet();
greet();
greet();

Output:

Hello
Hello
Hello

Function with Parameters

Parameters allow functions to receive data.


Syntax

function functionName(parameter)
{
}

Example

function greet(name)
{
    document.write("Hello " + name);
}

greet("Rahul");

Output:

Hello Rahul

Understanding Parameters

function greet(name)

Here:

name

is a parameter.


Understanding Arguments

greet("Rahul");

Here:

Rahul

is an argument.


Dry Run

Argument = Rahul

Passed to parameter name

Output:

Hello Rahul

Function with Multiple Parameters

Example:

function add(a, b)
{
    document.write(a + b);
}

add(10, 20);

Output:

30

Dry Run

a = 10

b = 20

a + b

30

Example – Student Information

function student(name, age)
{
    document.write(
    name + " " + age
    );
}

student("Rahul",20);

Output:

Rahul 20

Return Statement

The return statement sends a value back from a function.


Syntax

function name()
{
    return value;
}

Example

function add(a,b)
{
    return a+b;
}

let result =
add(10,20);

document.write(result);

Output:

30

Dry Run

add(10,20)

10+20

return 30

Stored in result

Display result

Function Without Return

function add(a,b)
{
    document.write(a+b);
}

Directly displays output.


Function With Return

function add(a,b)
{
    return a+b;
}

Returns output for further use.


Example – Square of a Number

function square(num)
{
    return num*num;
}

let ans =
square(5);

document.write(ans);

Output:

25

Example – Even or Odd

function check(num)
{
    if(num%2==0)
    {
        return "Even";
    }
    else
    {
        return "Odd";
    }
}

document.write(
check(10)
);

Output:

Even

Function Expression

Functions can be stored inside variables.

Example:

let greet =
function()
{
    document.write(
    "Hello"
    );
};

greet();

Output:

Hello

Why Function Expressions?

Useful for:

  • Dynamic programming

  • Event handling

  • Callbacks


Anonymous Functions

Functions without names.

Example:

function()
{
    document.write(
    "Hello"
    );
}

Usually used inside variables or events.


Arrow Functions

Introduced in modern JavaScript.

Shorter syntax.


Traditional Function

function add(a,b)
{
    return a+b;
}

Arrow Function

let add =
(a,b)=>
{
    return a+b;
};

Short Arrow Function

let add =
(a,b)=>a+b;

Example

let square =
x => x*x;

document.write(
square(5)
);

Output:

25

Variable Scope

Scope determines where a variable can be accessed.


Global Scope

Variables declared outside functions.

Example:

let name = "Rahul";

function show()
{
    document.write(name);
}

Accessible everywhere.


Local Scope

Variables declared inside functions.

Example:

function show()
{
    let age = 20;

    document.write(age);
}

Only accessible inside function.


Example

function demo()
{
    let x = 10;
}

document.write(x);

Output:

Error

Because x is local.


Complete Program – Addition Function

<!DOCTYPE html>

<html>

<body>

<script>

function add(a,b)
{
    return a+b;
}

let result =
add(10,20);

document.write(
"Sum = " + result
);

</script>

</body>

</html>

Output:

Sum = 30

Program – Factorial Using Function

function factorial(n)
{
    let fact = 1;

    for(let i=1;i<=n;i++)
    {
        fact*=i;
    }

    return fact;
}

document.write(
factorial(5)
);

Output:

120

Dry Run

1×2×3×4×5

120

Program – Largest of Two Numbers

function largest(a,b)
{
    if(a>b)
    {
        return a;
    }
    else
    {
        return b;
    }
}

document.write(
largest(10,20)
);

Output:

20

Types of Functions

TypeDescription
Simple FunctionNo parameters
Parameterized FunctionUses parameters
Return FunctionReturns value
Anonymous FunctionNo name
Arrow FunctionModern syntax

Real-Life Applications

Functions are used in:

  • Login systems

  • ATM software

  • Shopping carts

  • Banking applications

  • Games

  • Mobile apps

Every button click on platforms like Google, Amazon, and Facebook typically triggers one or more functions.


Common Mistakes Beginners Make

Forgetting Function Call

Wrong:

function greet()
{
    document.write("Hello");
}

Output:

Nothing

Because function was not called.

Correct:

greet();

Missing Return Statement

Wrong:

function add(a,b)
{
    a+b;
}

Correct:

function add(a,b)
{
    return a+b;
}

Using Local Variables Outside Function

Wrong:

function demo()
{
    let x=10;
}

document.write(x);

Results in an error.


Advantages of Functions

  • Reusable code

  • Easier maintenance

  • Better structure

  • Improved readability

  • Reduced duplication


Conclusion

Functions are one of the most important building blocks of JavaScript. They help organize programs into small, reusable units that are easy to understand and maintain.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...