0
0
C Sharp (C#)programming~20 mins

Return values and void methods in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Return values and void methods
📖 Scenario: You are creating a simple calculator program that can add two numbers and also print a greeting message.
🎯 Goal: Build a program with one method that returns the sum of two numbers and another method that prints a greeting message without returning anything.
📋 What You'll Learn
Create a method called AddNumbers that takes two integers and returns their sum.
Create a method called PrintGreeting that prints a greeting message and returns nothing (void).
Call both methods from the Main method and display the sum.
💡 Why This Matters
🌍 Real World
Methods that return values and void methods are common in all software programs to organize code and perform tasks.
💼 Career
Understanding how to write and use methods is essential for any programming job, as it helps create reusable and clear code.
Progress0 / 4 steps
1
Create the AddNumbers method
Create a method called AddNumbers that takes two integers named num1 and num2 and returns their sum as an integer.
C Sharp (C#)
Need a hint?

Use static int AddNumbers(int num1, int num2) and return the sum with return num1 + num2;.

2
Create the PrintGreeting method
Create a method called PrintGreeting that prints the message "Hello, welcome to the calculator!" and returns nothing (void).
C Sharp (C#)
Need a hint?

Use static void PrintGreeting() and inside it use Console.WriteLine to print the greeting.

3
Call both methods in Main
Inside the Main method, call PrintGreeting() first, then call AddNumbers with 5 and 7 as arguments and store the result in an integer variable called sum.
C Sharp (C#)
Need a hint?

Call PrintGreeting() and then call AddNumbers(5, 7) storing the result in sum.

4
Print the sum result
Add a line in the Main method to print the text "Sum: " followed by the value of the sum variable.
C Sharp (C#)
Need a hint?

Use Console.WriteLine("Sum: " + sum); to print the sum.