C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Functions in C

Function to multiply two numbers

Introduction

Functions in C help organize programs into smaller, reusable modules.
This program demonstrates how to multiply two numbers using a user-defined function.
The multiplication logic is written inside a function that takes two arguments and returns the product to the main function.

 

C Program: Function to multiply two numbers

Method 1: The function with int return type (With Arguments, With Return)

C

#include <stdio.h>

 

// Function declaration

int multiply(int a, int b);

 

int main() {

    int num1, num2, product;

 

    // Taking input from user

    printf("Enter first number: ");

    scanf("%d", &num1);

    printf("Enter second number: ");

    scanf("%d", &num2);

 

    // Function call

    product = multiply(num1, num2);

 

    // Display result

    printf("Product of %d and %d is: %d\n", num1, num2, product);

 

    return 0;

}

 

// Function definition

int multiply(int a, int b) {

    return a * b;

}

Output

 
OUTPUT :
Enter first number: 8
Enter second number: 6
Product of 8 and 6 is: 48

Description

  1. Function Declaration:
    int multiply(int a, int b); declares a function that accepts two integers and returns an integer.
  2. Function Definition:
    Performs multiplication and returns the product.
  3. Function Call:
    The function is called from main() with two arguments, and the returned value is stored in product.
  4. Output Display:
    The result is printed using printf().

 

C Program: Function to multiply two numbers

Method 2: The function with void return type (With Arguments, No Return)

C

#include <stdio.h>

 

// Function declaration

void multiplyNumbers(int a, int b);

 

int main() {

    int num1, num2;

 

    // Taking input from user

    printf("Enter first number: ");

    scanf("%d", &num1);

    printf("Enter second number: ");

    scanf("%d", &num2);

 

    // Function call

    multiplyNumbers(num1, num2);

 

    return 0;

}

 

// Function definition

void multiplyNumbers(int a, int b) {

    int product = a * b;

    printf("Product of %d and %d is: %d\n", a, b, product);

}

Output

 
OUTPUT :
Enter first number: 9
Enter second number: 7
Product of 9 and 7 is: 63

Description

  1. Function Declaration:
    void multiplyNumbers(int a, int b); — defines a function that accepts two integers and has no return type (void).
  2. Function Definition:
    • The multiplication is done inside the function.
    • The product is displayed directly using printf().
  3. Function Call:
    The function is called from main() with two integer values as arguments.
  4. No Return Value:
    The result is printed directly; no value is returned to the main function.