Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:51:14+00:00 2026-05-16T15:51:14+00:00

I am trying to get a function in JAVA to take a value in

  • 0

I am trying to get a function in JAVA to take a value in an array as input. Here is my attempt:

public static void main(String[]args)
{
    int [] a = new int [4];
    a[0]=6;
    a[1]=7;
    a[2]=5;
    a[3]=2;
    int g = 11;
    System.out.println calc(a[0], 11);
}
public static int calc(a[], int g)

I am doing this before running a recursion. However, I keep getting an error message. What is the correct way to do this? I want the function, however, to an input from an array. How do I get the function to take not just any int value but rather from the row of a given array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:51:15+00:00Added an answer on May 16, 2026 at 3:51 pm

    Change this:

    public static int calc(a[], int g)
    

    To this:

    public static int calc(int value, int g)
    

    When you do System.out.println(calc(a[0], 11)); you’re actually passing an int to function calc. That int resides in the first position a[0] of the array.

    If you want to pass the whole array to calc, your function signature must be:

    public static int calc(int[] a, int g)
    

    You’d call it this way:

    System.out.println(calc(a, 11));
    

    This line System.out.println calc(a[0], 11); in your code is also wrong. It should be:

    System.out.println(calc(a[0], 11));
    

    Suppose your calc function has this:

    public static int calc(int value, int g)
    {
        return value + g;
    }
    

    When you call:

    System.out.println(calc(a[0], 11));
    

    The output will be:

    17
    

    Why 17? Because you’re summing a[0] = 6 + 11 = 17.

    a[0] is a row of the array and you’re actually passing it to the function calc.

    In the recursive case, you could have and indexer i for example.

    for(int i = 0; i < a.length; i++)
    {
         // You could check some condition here:
         // if(a[i] > 10) { System.out.println(calc(a[i+1], 11)); }
         System.out.println(calc(a[i], 11));
    }
    

    Now when you execute this code you’ll have this output:

    17
    18
    16
    13
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get my function to return the data it got into another
I am trying to get the function feedAmount to print to the console but
I'm brand new to C++ and am having trouble trying to get a function
I'm trying to get the rememberMe() function to remember users and retain sessions for
I'm trying to get an Excel function to return more arguments than are passed
I'm trying to get the browser function in my Android application to work but
I am trying to use the http_get function. But I get a undefined reference
I'm trying to use the jQuery get function to render the results of an
Trying to get a list of all substrings currently I have a function but
I'm trying to get an jquery ajax callback function to update the background colour

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.