+1 (812) 783-0640 

Understanding C functions

Students need to understand the various functions used in C programming in order to produce great applications. We can help you master these functions and all you need to do is take our C tutoring services.

Simple C Functions

Write a small program that calculates the sum and the difference of two integers with three user-defined functions:

//takes two integer arguments and returns the sum of the two arguments

intCalculateSum (int num1, int num2);

//takes two integer arguments and returns the difference of the two arguments

intCalculateDifference(int num1, int num2);

//takes two integer arguments and two integer pointer arguments
//1. calculate the sum and stores the result in sumptr
//2. calculate the difference and store the result in diffPtr
void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);

Call all three functions from main.
Print all the results inside the function definitions (value at) and ALSO print the results again back in the main function after the function has been called.

Main function logic (and hints):

Declare variables num1, num2, sum1, diff1, sum2, diff2
Ask and get 2 numbers from the user (num1 and num2)
Pass num1 and num2 to the function CalculateSum and it will return the sum to the variable sum1
Print sum1 onto the screen
Pass num1 and num2 to the function CalculateDifference and it will return the difference to the variable diif1
6.Print diff1 onto the screen

Pass num1 and num2 and “address of” sum2 and “address of” diff2 to the function CalculateBoth
Print sum2 and diff2 results onto the screen 
Solution 

lab4.c 

#include

//takes two integer arguments and returns the sum of the two arguments

intCalculateSum(int num1, int num2);

//takes two integer arguments and returns the difference of the two arguments

intCalculateDifference(int num1, int num2);

//takes two integer arguments and two integer pointer arguments

//1. calculate the sum and stores the result in sumptr

//2. calculate the difference and store the result in diffPtr

voidCalculateBoth(int num1, int num2, int *sumPtr, int *diffPtr);

int main() {

// implement the main function logic

int num1, num2, sum1, diff1, sum2, diff2;

printf(“Enter num1: “);

scanf(“%d”, &num1);

printf(“Enter num2: “);

scanf(“%d”, &num2);

sum1 = CalculateSum(num1, num2);

printf(“sum1 = %d\n”, sum1);

diff1 = CalculateDifference(num1, num2);

printf(“diff1 = %d\n”, diff1);

CalculateBoth(num1, num2, &sum2, &diff2);

printf(“sum2 = %d\n”, sum2);

printf(“diff2 = %d\n”, diff2);

return 0;

}

intCalculateSum(int num1, int num2) {

int result = num1 + num2;

printf(“\nInsideCalculateSum: num1=%d num2=%d sum=%d\n\n”,

num1, num2, result);

return result;

}

intCalculateDifference(int num1, int num2) {

int result = num1 – num2;

printf(“\nInsideCalculateDifference: num1=%d num2=%d diff=%d\n\n”,

num1, num2, result);

return result;

}

voidCalculateBoth(int num1, int num2, int *sumPtr, int *diffPtr) {

*sumPtr = num1 + num2;

*diffPtr = num1 – num2;

printf(“\nInsideCalculateBoth: num1=%d num2=%d sum=%d diff=%d\n\n”,

num1, num2, *sumPtr, *diffPtr);

}