CS156: Intro to C, Part I

Spring 2018

Functions

See this page as a slide show

CS156 Functions: Fully Functional

Some slides from Syracuse University

Functions

#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

Not Methods

Functions: A Motivating Example

#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

Functions

An Example

#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

Some Functions We’ve Seen

Functions

Functions

returnType functionName(list of arguments) {
    statement1;
    statement2;
    …
    return expression;      // value to return
}

How to call a function

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.

Function Placement

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;
}

Functions

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;
}

Functions

int abs(int);
int power(int base, int n);


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;
}

Declaration and Definition - Example

    // 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));
    }

Functions

    #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;
    }

Function Arguments

Declaring a function prototype:

returnType functionName(list of arguments);

Function Arguments: passed-by-value

#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;
}

Passing Arguments

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

Passing Arguments

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

Functions with No Arguments

// define a function with no arguments or return value
void doNothing(){
}

int main() {
    doNothing();
    return 0;
}

Commenting Functions

Commenting Functions

// 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);
}

Commenting Functions

/*
 * 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

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-03-08T14:07

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building