Write a program that defines a function to calculate the sum of two integers and prints the result. Call this function by passing two integer values.
Explanation
sum(a, b)is a function that takes two arguments.- It adds them and stores the result in a variable named
add. - It then prints the result.
sum(x, y)calls the function with x = 10 and y = 20, so it prints 30.
Dry Run
Input: a = 10, b = 20
Step 1: add = 10 + 20 = 30
Step 2: Print 30
function sum(a, b) {
let add = a + b;
console.log(add);
}
let x = 10;
let y = 20;
sum(x, y);
#include <iostream>
using namespace std;
void sum(int a, int b) {
int add = a + b;
cout << add << endl;
}
int main() {
int x = 10, y = 20;
sum(x, y);
return 0;
}
#include <stdio.h>
void sum(int a, int b) {
int add = a + b;
printf("%d\n", add);
}
int main() {
int x = 10, y = 20;
sum(x, y);
return 0;
}
public class Main {
public static void sum(int a, int b) {
int add = a + b;
System.out.println(add);
}
public static void main(String[] args) {
int x = 10, y = 20;
sum(x, y);
}
}
def sum(a, b):
add = a + b
print(add)
x = 10
y = 20
sum(x, y)
