Code Examples from Lectures on functions
#include <stdio.h>
// given x calculate value of polynomial with coefficients a, b, c
double evaluate_quadratic(double a, double b, double c, double x) {
return (a * x * x) + (b * x) + c;
}
int main(int argc, char *argv[]) {
double x;
printf("Enter x: ");
scanf("%lf", &x);
double value = evaluate_quadratic(2.0, -3.0, 4.0, x);
printf("2x^2 - 3x + 4 = %lf\n", value);
return 0;
}
#include <stdio.h>
#include <math.h>
// given x calculate value of polynomial with coefficients a, b, c
double quadratic_root(double a, double b, double c) {
return (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
}
double evaluate_quadratic(double a, double b, double c, double x) {
return (a * x * x) + (b * x) + c;
}
int main(int argc, char *argv[]) {
double a, b, c;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
printf("Enter c: ");
scanf("%lf", &c);
double root = quadratic_root(a, b, c);
printf("Calculated root is %lf\n", root);
double value = evaluate_quadratic(a, b, c, root);
printf("The value of the quadratic for %lf is %lf\n", root, value);
return 0;
}
#include <stdio.h>
// calculate x to the power of 3
double cube(double x) {
double result;
result = x * x * x;
return result;
}
int main(void) {
double a, b;
printf("42 cubed is %lf\n", cube(42.03));
a = 2;
b = cube(a);
printf("2 cubed is %lf\n", b);
return 0;
}
#include <stdio.h>
void print_hundred_messages(void);
void print_ten_messages(void);
void print_message(void);
int main(int argc, char *argv[]) {
print_hundred_messages();
return 0;
}
void print_hundred_messages(void) {
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
}
void print_ten_messages(void) {
print_message();
print_message();
print_message();
print_message();
print_message();
print_message();
print_message();
print_message();
}
void print_message(void) {
printf("C is good.\n");
printf("C is great.\n");
printf("We all love C.\n");
}
#include <stdio.h>
int f(int x) {
int y;
// these assignments will have no effect on variables
// outside the function
x = x + 1;
y = 3;
return x * y;
}
int main(int argc, char *argv[]) {
int x, y , z;
x = 1;
y = 2;
z = f(y);
// note the variables x & y are local to main
// and are not changed by assignment to variables
// of the same name in f
printf("x=%d y=%d z=%d\n", x, y, z);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int n) {
if (n < 2) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n-2);
}
int factorial(int n) {
if (n < 2) {
return 1;
}
return n * factorial(n - 1);
}
int main(void) {
int x;
printf("Enter number: ");
scanf("%d", &x);
printf("factorial %d = %d\n", x, factorial(x));
printf("fibonacci %d = %d\n", x, fibonacci(x));
return 0;
}
COMP1511 18s1: Programming Fundamentals
is brought to you by
the School of Computer Science and Engineering
at the University of New South Wales , Sydney.
For all enquiries, please email the class account at
cs1511@cse.unsw.edu.au
CRICOS Provider 00098G
Lecture example illustrating simple function