Definition of functions:-


A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. It is a unit of code that is often defined by its role within a greater code structure. Specifically, a function contains a unit of code that works on various inputs, many of which are variables, and produces concrete results involving changes to variable values or actual operations based on the inputs.

*Note:- Functions are also called as Methods, so don't get confused when methods are used instead of functions.

Need of functions in Programming:-

1. To improve the readability of code.
2. Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
3. Debugging of the code would be easier if you use functions, as errors are easy to be traced.
4. Reduces the size of the code, duplicate set of statements are replaced by function calls.
5. We can divide a large program into multiple small modules.
6. Function Hide the complexities.

Types of Functions in any programming languages:-


1. Pre-defined Functions
2. User-defined Functions

Pre-defined Functions:-
These are the functions that are built into Language to perform operations & are stored in the Standard Function Library.
some examples in C, C++, Java, Python.

User-defined Functions:-
User-defined functions are defined by the user to perform specific tasks. There are four different patterns to define a function −

-Functions with no argument and no return value.
-Functions with no argument but a return value.
-Functions with argument but no return value.
-Functions with argument and a return value.

Let's Dig in to all the types of function declaration mentioned above:

In the below programs we will be learning about how to define 'Functions with no argument and no return value' and 'Functions with no argument but a return value'  in Four Languages (C, C++, Python, Java)


NOTE:- All the viewers Read the comments in the Program very carefully because the explanation of the syntaxes and programs are given through comments 

C


 //***HELPFORCODERS***
 //This program is to get the sum of all natural numbers from 1 to the user's input
 //For Example:- Sum of 1 to 15 is 120

 #include<stdio.h>

 //Declaring a global variable to use it in the whole program
 int num;

 //Declaring a User-definedd Function get_data() to take the input from the user
 //This is a 'no argument' and 'no return ' type of Function
 void get_data()
 {
    printf("Enter the number till you want to calculate the Sum ");
    scanf("%d",&num);
 } 

 //Declaring one more function to show the output 
 //It is also a User-defined Function 
 //This is a 'no argument' but 'return' type of Function
 int show_data()
 {
    //Declaring a variable 'sum' to store the final answer and initialize it as 0
    int sum = 0;

    for(int i = 0; i<= num; i++)
    {
        sum = sum + i;
    }

    //Here we are returning the value in sum to the function
    //This value will get stored in show_data() function
    return sum;
 }

 // This is the main function of the program 
 // It is a pe-defined Function.
 int main()
 {
    // Here we are calling the get_data() function in the main function to take the input from the user 
    get_data();

    // Here we are printing the value stored in show_data() function 
    printf("%d",show_data());

    return 0;
 }


C++


 // **HELPFORCODERS***
 // This program is to get the sum of all natural numbers.
 // From 1 to a given input from user.
 // For example :- sum from 1 to 15 is 120.

 #include<iostream>
 using namespace std;

 // Declaring a global variable to use it in the whole program
 int num; 

 // Declaring the User-defined function get_data() to take the input from user.
 // This function is a type of no argument and no return type.
 void get_data()
 {
    cout<<"Enter the number till where you want the sum\n";
    cin>>num;
 }

 // Declaring one more function to show the output.
 // It is also User-defined function.
 // This function is type of no argument but returns a value.
 int show_data()
 {
    // Declaring a variable 'sum' to store the final answer and initializing it with 0.
    int sum=0;

    // Here the loop runs untill it reachs the number inputed by the user.
    // For this we are using that global variable declared at start of the program.  
    for (int i=1; i<=num; i++)
    {
        sum = sum + i;
    }

    // Here we returning the value in sum to the function.
    // This value will get stored in show_data().
    return sum;
 }

 // This is the main function of the program.
 // This is a Pre-defined function.
 int main()
 {
    // Here we are calling get_data() function.
    get_data();

    // Here we are printing the value stored in show_data() function.
    cout<<show_data();

    // Here we are returning 0;
    return 0;
 }


Python


 # **HELPFORCODERS***
 # This program is to get the sum of all natural numbers.
 # From 1 to a given input from user.
 # For example :- sum from 1 to 15 is 120.

#Declaring a User-definedd Function get_data() to take the input from the user
#This is a 'no argument' and 'no return ' type of Function
 def get_data():

	#Declaring a global variable to use it in the whole program
    global num
    
    #Taking the input from the user
    num = int(input("please enter the number till you want the sum to be calculated"))
    
#Declaring one more function to show the output 
#It is also a User-defined Function 
#This is a 'no argument' but 'return' type of Function
 def show_data():

	#Declaring a variable 'sum' to store the final answer and initialize it as 0
    sum = 0
    for i in range(0,num + 1):
        sum = sum + i
        
    #Here we are returning the value in sum to the function
    #This value will get stored in show_data() function
    return sum
    
# Here we are calling the get_data() function to take the input from the user 
 get_data()

# Here we are printing the value stored in show_data() function 
 print(show_data())


Java


 // **HELPFORCODERS***
 // This program is to get the sum of all natural numbers.
 // From 1 to a given input from user.
 // For example :- sum from 1 to 15 is 120.

package com.company;
import java.util.*;

 public class Main
 {
	// Declaring a global variable to use it in the whole program
    public static int number;
    
    // Declaring the User-defined function get_data() to take the input from user.
	// This function is a type of no argument and no return type.
    public static void get_data()
    {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter the number till where you want the sum");
      number = sc.nextInt();
    } 
    
    //Declaring one more function to show the output 
	//It is also a User-defined Function 
	//This is a 'no argument' but 'return' type of Function
    public static int show_data()
    {
      //Declaring a variable 'sum' to store the final answer and initialize it as 0
      int sum=0;
      
      for(int i = 1; i<=number; i++)
      {
        sum += i;
      }
      //Here we are returning the value in sum to the function
      //This value will get stored in show_data() function
      return sum;
      
    }
    
    // This is the main function of the program 
	// It is a pe-defined Function.
    public static void main(String[] args)
    {
    
      // Here we are calling the get_data() function in the main function to take the input from the user 
      get_data();
      System.out.print("The Sum is:- ");
      
      // Here we are printing the value stored in show_data() function 
      System.out.println(show_data());
    }
 }


Now we will be learning about how to define 'Functions with argument but no return value' and 'Functions with argument and a return value'  in Four Languages (C, C++, Python, Java)

C


 //*HELPFORCODERS**
//This program is to get the factorial of any number

 #include<stdio.h>

 //Declaring a Method named fact1 having one parameter
 //This Method belogs to the type of Methods with argument but no return value
 //This Method is type of void or null
 //The type of argument also is integer
 void fact1(int a)
 {
    //Declaring a variable named facto1 and initializing it with 1
    int facto1 = 1;
    for(int i = 1; i<= a; i++)
    {
        //This is the loop to calculate the factorial
        //This loop runs till the number user has entered

        //This the Formula to calculate the factorial
        facto1= facto1 * i;
    }
    //Printing the statement for output
    printf("The factorial is %d", facto1);
 }

 //This is the second Method to calculate the Factorial
 //This method is named as fact2 and has one parameter
 //This function is integer type i.e. int
 //The type of argument also is integer
 int fact2(int b)
 {
    //Declaring a varialbe named facto2 and initializing it with 1
    int facto2 = 1;

    for(int j = 1; j<=b;j++)
    {
        //This the formula to Calculate Factorial
        facto2 = facto2 * j;
    }
    //Here we are returning the value stored in variable facto2 to the function fact2
    return facto2;
 }

 //This is the main function of the program
 int main()
 {
    //Declaring a variable called num to store the user input
    int num;
    
    //Printing a message to inform user to input
    printf("Enter the number of which you want the Factorial\n");
    
    //Taking user input in num
    scanf("%d",&num);
    
    //Below is the funcion call of fact1
    //You can remove the comment to check that 
    //fact1(num);
    
    //This the function call of fact2 within cout statement
    printf("Factorial of the number is %d", fact2(num));
    
    //Returning zero to main function
    return 0;               
 }


C++


 //**HELPFORCODERS***
//This program is to get the factorial of any number

 #include<iostream>
 using namespace std;

//Declaring a Method named fact1 having one parameter
//This Method belogs to the type of Methods with argument but no return value
//This Method is type of void or null
//The type of argument also is integer
 void fact1(int a)
 {
    //Declaring a variable named facto1 and initializing it with 1
    int facto1 = 1;
    
    //This is the loop to calculate the factorial
    //This loop runs till the number user has entered
    for (int i=1; i<=a; i++)
    {
        //This the Formula to calculate the factorial
        facto1 = facto1 * i;
    }
    
    //Printing the statement for output
    cout<<"The factorial of "<<a<<" = "<<facto1;
 }

//This is the second Method to calculate the Factorial
//This method is named as fact2 and has one parameter
//This function is integer type i.e. int
//The type of argument also is integer
 int fact2(int b)
 {
    //Declaring a varialbe named facto2 and initializing it with 1
    int facto2 = 1;
    
    //This is the same loop as above but with another variable called j 
    for (int j=1; j<=b; j++)
    {
        //This the formula to Calculate Factorial
        facto2 = facto2 * j;
    }
    
    //Here we are returning the value stored in variable facto2 to the function fact2
    return facto2;
 }

//This is the main function of the program
 int main()
 {
    //Declaring a variable called num to store the user input
    int num;
    
    //Printing a message to inform user to input
    cout<<"Enter the number of which you want the Factorial\n";
    
    //Taking user input in num
    cin>>num;
    
    //Below is the funcion call of fact1
    //You can remove the comment to check that 
    //fact1(num);
    
    //This the function call of fact2 within cout statement
    cout<<"The Factorial of "<<num<<" = "<<fact2(num);
    
    //Returning zero to main function
    return 0;               
 }


Python



#*HELPFORCODERS**
#This program is to get the factorial of any number


#Declaring a Method named fact1 having one parameter
#This Method belogs to the type of Methods with argument but no return value
#This Method is type of void or null
#The type of argument also is integer
def fact1(a):
	
    #Declaring a variable named facto1 and initializing it with 1
    facto1 = 1
    
    #This is the loop to calculate the factorial
    #This loop runs till the number user has entered
    for i in range(1,a+1):
    
    	#This the Formula to calculate the factorial
        facto1 = facto1 * i
	
    #Printing the statement for output
    print(facto1)

#This is the second Method to calculate the Factorial
#This method is named as fact2 and has one parameter and also a return value
#This function is integer type i.e. int
#The type of argument also is integer
def fact2(b):
    facto2 = 1
    
    #This is the loop to calculate the factorial
    #This loop runs till the number user has entered
    for j in range(1,b+1):
    
    	#This the Formula to calculate the factorial
        facto2 = facto2 * j
    return facto2

num = int(input("Please enter the number"))

#Below is the funcion call of fact1
#You can remove the comment to check that 
#fact1(num)

#This the function call of fact2 within print statement
print("The factorial is ",fact2(num))


Java



//*HELPFORCODERS**
//This program is to get the factorial of any number


 package com.company;
 import java.util.*;

 public class Main
 {
 
 	//Declaring a Method named fact1 having one parameter
    //This Method belogs to the type of Methods with argument but no return value
    //This Method is type of void or null
    //The type of argument also is integer
    public static void fact1(int a)
    {
    	 //Declaring a variable named facto1 and initializing it with 1
         int facto1 = 1;
         
         //This is the loop to calculate the factorial
    	 //This loop runs till the number user has entered
         for (int i=1; i<=a ; i++)
         {	  
         	  //This the Formula to calculate the factorial
              facto1 = facto1 * i;
         }
         System.out.print("The factorial of ");
         System.out.print(a);
         System.out.print(" = ");
         System.out.print(facto1);
    }
    
    
    //This is the second Method to calculate the Factorial
    //This method is named as fact2 and has one parameter and also a return value
    //This function is integer type i.e. int
    //The type of argument also is integer
    public static int fact2(int b)
    {
         int facto2 = 1;
         for (int j=1; j<=b; j++)
         {
         	  //This the Formula to calculate the factorial
              facto2 = facto2 * j;
         }
         return facto2;
    }
    
    
    //This is the main function of the program
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number to find the factorial");
        int num = sc.nextInt();
        
        //Below is the funcion call of fact1
    	//You can remove the comment to check that 
        //fact1(num);
        
        System.out.print("The factorial of given number ");
        System.out.print(num);
        System.out.print(" = ");
        
        //This the function call of fact2 within print statement
        System.out.print(fact2(num));
    }
 }



Syntax of Declaring Functions in C, C++, Java, Python.

C



#include<stdio.h>     
int fnc_name()           // fnc_name is the name of the funtion  
                         // 'int' is the return type  i.e. the datatype of the returning value
{

    // code to be written
    // The contents of the function will always be inside the curly brackets

    return value;        // This line will return value to the function
}



C++



#include<iostream> 	 
using namespace std;
int fnc_name()           // fnc_name is the name of the funtion  
                         // 'int' is the return type  i.e. the datatype of the returning value
{

    // code to be written
    // The contents of the function will always be inside the curly brackets

    return value;        // This line will return value to the function
}



Java



 public class Main()             // Main Class
 {
    static void fnc_name()      //static means function belongs to main class 
                                // fnc_name is the name of the function
    
    {
        //code to be written
        // all the codes to be written in between the curly brackets
    }
 }



Python



 def fnc_name():       #fnc_name is the name of the funtion and 'def' is use to define a function
                      
    #code to be written

    return value      #it return the value to the function




So after declaring functions, so how to use it? To use any function in the program we should need to call it in the main function of the program.
So below is the syntax of calling the functions in C, C++, Java, Python.

C

    
int main()
 {
	func_name();	// Function can be called by writing the name of the function in the main function
 }



C++



 int main()
 { 
	func_name();	// Function can be called by writing the name of the function in the main function
 }



Python


 fnc_name()			  #  Function can be called by writing the name of the function.



Java


 public static void main(String[] args)
    {
       fact1(num);		// Function can be called by writing the name of the function in the main function
 	}



//ENJOY CODING\\

 

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post