Some slides from Syracuse University
printf("Hello World!\n");
#include <stdio.h> #include <math.h> int main() { printf("square root of 5 is %f\n", sqrt(5.0)); printf("2½ cubed is %f\n", pow(2.5, 3)); return 0; }
square root of 5 is 2.236068 2½ cubed is 15.625000
#include <stdio.h> int main() { float sideA=1.2, sideB=2.1; float sideA2=2.2, sideB2=3.0; printf("Area is %f\n", sideA*sideB); printf("Perimeter is %f\n", 2*(sideA+sideB)); printf("Area is %f\n", sideA2*sideB2); printf("Perimeter is %f\n", 2*(sideA2+sideB)); return 0; }
Area is 2.520000 Perimeter is 6.600000 Area is 6.600000 Perimeter is 8.600000
#include <stdio.h> void printAreaAndPerimeter(float a, float b){ printf("Area is %f\n", a*b); printf("Perimeter is %f\n", 2*(a+b)); } int main() { float sideA = 1.2, sideB = 2.1; float sideA2 = 2.2, sideB2 = 3.0; printAreaAndPerimeter(sideA, sideB); printAreaAndPerimeter(sideA2, sideB2); return 0; }
Area is 2.520000 Perimeter is 6.600000 Area is 6.600000 Perimeter is 10.400000
main
— can take two arguments or none and returns an int
printf
— takes a string and a list of variables as arguments
scanf
— reads in values, stores them to variables
printf("%d %d %d", x, 9, x*3+15);
double power(double value, int n);
int
, void
, …)
returnType functionName(list of arguments) { statement1; statement2; … return expression; // value to return }
int
, its return type is int
.
float
, its return type is float
.
void
.
Examples of function calls:
printf("hi there\n"); hypot = sqrt(a*a+b*b); z = fun1('x', 2+2, x*37, "hello"); fun2(x, &y, 3.4-z);
Don't specify the types of the arguments when calling a function—only when you declare or define it.
Preprocessor directives function defintions int main() { declarations & statements return value; } |
#include <stdio.h> int abs(int a) { if (a < 0) return -a; return a; } int main() { int n = -25; printf("%d\n", abs(n)); return 0; } |
Another way to do it:
Preprocessor directives function prototypes int main() { declarations & statements return value; } function definitions |
#include <stdio.h> int abs(int); int main() { int n = -25; printf("%d\n", abs(n)); return 0; } int abs(int a) { if (a < 0) return -a; return a; } |
int abs(int); int power(int base, int n);
main
function
int abs(int a) { if ( a < 0 ) return -a; return a; } int power(int base, int n) { int p = 1; for (int i=1; i<=n; i++) p *= base; return p; }
main
function
// Function declarations (alias prototypes) void printAreaAndPerimeter(float a, float b); int main() { … } void printAreaAndPerimeter(float a, float b){ printf("Area is %f\n", a*b); printf("Perimeter is %f\n", 2*(a+b)); }
#include <stdio.h> int power(int base, int n); int main( ) { int a, b, p; printf("Enter the base "); scanf("%d", &a); printf("Enter the exponent "); scanf("%d", &b); p = power(a, b); printf("The power is %d\n ", p); return 0; } int power(int base, int n) { int p = 1; for (int i=1; i<=n; i++) p *= base; return p; }
Declaring a function prototype:
returnType functionName(list of arguments);
#include <stdio.h> int power(int base, int n); int main( ) { int a = 5, b = 2, p; p = power( a, b); … } int power(int base, int n) { int p = 1; for (int i=1; i<=n; i++) p *= base; return p; }
main
has three variables: a
, b
, p
power
has three variables: base
, n
, p
base
⇐ a
n
⇐ b
a
= 5 and b
= 2, base
gets a copy of 5 and
n
gets a copy of 2
void foo(int a, int b){ a = 3; b = 10; } int main(){ int a=2, b=0; foo(a,b); return 0; }
Memory | ||
main
| a
| 2 |
b
| 0 | |
foo
| a
| 3 |
b
| 10 |
void changeA(int a) { printf("changeA:a=%d\n", a); a = 1000006; printf("changeA:a=%d\n", a); } int main(){ int a = 12; printf("a=%d\n", a); changeA(a); printf("a=%d\n", a); }
a=12 changeA:a=12 changeA:a=1000006 a=12
// define a function with no arguments or return value void doNothing(){ } int main() { doNothing(); return 0; }
// Prints the area and perimeter // PRE: a and b are floating-pt numbers // POST: area and perimeter values printed, nothing returned void printAreaAndPerimeter(float a, float b) { float area = a*b; float perimeter = 2*(a+b); printf("Area of (%f,%f) is %f, perimeter is %f\n", a, b, area, perimeter); }
/* * Multiplies two numbers and returns product * Input: num1 and num2 are integers to be multiplied * Output: product returned */ int multiply(int num1, int num2 ) { return num1 * num2 ); } // end multiply