Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Lesson7 / 20

    Module 2 · Input, loops and methods: make programs respond · Lesson 7 of 20

    Give small jobs a name with methods

    The same minutes calculation is needed twice. Can you give it a name instead of copying it? Supply different inputs and predict the returned results.

    Predict two calls

    A method is a named piece of work that you can call. This lesson uses local functions written after the main statements in a top-level console program. We use the word method for the beginner idea; these particular helpers are local functions.

    Read both calls first. Predict their output, then run the complete program.

    C#
    using System;
    
    int firstPlan = CalculateMinutes(20, 3);
    int secondPlan = CalculateMinutes(15, 2);
    
    Console.WriteLine(firstPlan);
    Console.WriteLine(secondPlan);
    
    static int CalculateMinutes(int minutesPerDay, int days)
    {
        return minutesPerDay * days;
    }

    What are the two output lines?

    A 20, then 15
    B 3, then 2
    C 60, then 30
    D 30, then 60
    Show answer

    Correct answer: C

    The first call calculates 20 × 3 and the second calculates 15 × 2.

    Option A: The helper returns a product, not its first input.

    Option B: The helper does not return only the day count.

    Option D: The calls and WriteLine statements keep the shown order.

    Hint: Replace each parameter name with that call’s supplied number.

    Follow input → work → result

    Text
    CalculateMinutes(20, 3)
                     │  │
                     │  └── days = 3
                     └───── minutesPerDay = 20
    
                  20 × 3
                     │
                     └── return 60 ──> firstPlan

    The numbers supplied in a call are arguments. The names in the declaration are parameters. int before the helper name says the returned result is an integer. return sends the result back to the caller; the assignment stores it in firstPlan.

    The body does not print anything. This is useful separation: CalculateMinutes calculates, while the main statements choose what to display. A returned value can be checked, stored or used in another calculation. A helper that only prints would be harder to reuse when you need the number itself.

    static on this local function means it cannot secretly read local variables from the surrounding program. The needed values arrive through its parameters. You do not need classes or objects to use this pattern here.

    Which part declares the name used for the first input inside the helper?

    A The 20 in the call
    B int minutesPerDay in the declaration
    C Console.WriteLine
    D The variable firstPlan
    Show answer

    Correct answer: B

    minutesPerDay is the parameter name in the helper declaration.

    Option A: 20 is an argument: the value supplied by this call.

    Option C: WriteLine displays a value; it does not declare this parameter.

    Option D: firstPlan receives the returned result outside the helper.

    Hint: Look between the declaration’s parentheses.

    Return a decision

    A return type can also be bool. Here a helper reports whether the completed minutes meet the target. The same helper handles two different inputs.

    C#
    using System;
    
    Console.WriteLine(HasReachedGoal(30, 45));
    Console.WriteLine(HasReachedGoal(45, 45));
    
    static bool HasReachedGoal(int studiedMinutes, int targetMinutes)
    {
        return studiedMinutes >= targetMinutes;
    }

    Why does the second call return True?

    A The helper always returns True
    B 45 is greater than 45
    C The condition includes equality
    D bool means the first call was ignored
    Show answer

    Correct answer: C

    The >= operator accepts equality, so 45 meets a target of 45.

    Option A: The first call returns False.

    Option B: 45 equals 45; the equality part is what matters.

    Option D: Both calls run and produce results.

    Try it independently

    Write a helper named RemainingMinutes. It receives studied and target minutes. Return the difference when the target is larger; return zero when the target has already been met. For inputs 30 and 45 expect 15. For 50 and 45 expect 0. This small contract avoids a negative “remaining” amount.

    Try before comparing with the complete solution below.

    C#
    using System;
    
    Console.WriteLine(RemainingMinutes(30, 45));
    Console.WriteLine(RemainingMinutes(50, 45));
    
    static int RemainingMinutes(int studiedMinutes, int targetMinutes)
    {
        if (studiedMinutes >= targetMinutes)
        {
            return 0;
        }
    
        return targetMinutes - studiedMinutes;
    }

    Explain back

    Describe one helper using only its name, inputs and output. Then change the first call to RemainingMinutes(10, 60) and predict 50. Explain why the helper can stay unchanged when the input changes. Keep names specific enough to describe the job; a name such as DoStuff gives the caller little help.

    Reference: Microsoft — local functions and explicit inputs.

    Quick reference

    • Parameters name the inputs inside a helper.
    • Arguments are the supplied values at a call.
    • The return type describes the result’s type.
    • return sends a result back; it does not print it.
    • Prefer one clear responsibility per helper.
    • Static local functions receive needed surrounding data explicitly.
    Sign in to mark lessons done and keep your place in the course.Sign in

    TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·