2430 : Introduction to Programming in C

Functions

First of all, below you can find the solution to your last homework/classwork program, where you have to print out a positive integer backwards.
#include <stdio.h>
#include "/home/cc/atdp4/su04/staff/atdp4-rc/inc/simpio.h"

int main() {
	int n, rightdigit;
	printf("Enter a positive integer: ");
	n = GetInteger();

	do {
		rightdigit = n % 10;
		printf("%d", rightdigit);
		n /= 10;
	} while (n != 0);

	printf("\n");
	return 0;
}
Functions are like mini-programs, which are separate subroutines from your main function, and are called from your main function.
Remember that functions have a data type -- of the return value, arguments -- information that you pass into the functions, a function name -- used to identify the function you are calling and declaring, a return value -- what you are passing back to the main program, as well as declarations and definitions -- what the function does.
Below is an example of a program that has a function called "Addition," which simply adds two integers and return the sum as an integer.
int Addition(int a, int b);

int main() {
	int x, y, answer;
	printf("Enter first integer: ");
	x = GetInteger();
	printf("Enter second integer: ");
	y = GetInteger();
	answer = Addition(x, y);
	printf("%d + %d = %d\n", x, y, answer);
	return 0;
}

int Addition(int a, int b) {
	int ans;
	ans = a + b;
	return ans;
}
Classwork program #1
Your classwork program is to work on problem #5 on page 180 on your textbook. The RaiseIntToPower() function takes in two arguments or parameters, x and k, the base and the power, correspondingly. Below you can find my solution to this problem.
#include <stdio.h>

int main() {
	int i, result;
	for (i = 0; i <= 10; i++) {
		result = RaiseIntToPower(2, i);
		printf("%d^%d = %d\n", 2, i, result);
	}
}

int RaiseIntToPower(int x, int k) {
	int i, answer = 1;
	for (i = 0; i < k; i++) {
		answer *= x;
	}
	return answer;
}

Homework Assignment
Write the function RaiseRealToPower according to the instructions on page 180 problem #6. Then write an absolute value function abs() that takes in one argument of type double, which returns the absolute value of this number. Then, write the ApproximateEqual() function according to the instructions on page 181 problem #10.
After you have these 3 functions working, write a function sqrt() that takes in a double as an argument, and return the square root of the number. Then, write a function cbrt() that takes in a double as an argument, and return the cube root of the number.
In main, test out both functions by printing out the results of square roots and cube roots of numbers from 1 to 10.
Read Chapter 11 on Arrays.