User-defined Functions in C

In C functions must be both declared and defined. We declare a function using a function prototype statement. The function prototype consists of the function name, the function’s return type, and its parameter list. The function definition consists of all the information in the function prototype, followed by the function itself delimited by curly braces. 

Our first function is a void function with no parameters. We call the function only to get its side effect, not a return value. Because it is a void function, it can only be used as a statement, not in an expression.

#include <stdio.h>

//function declaration
//ie function prototype
void greeting();

int main(void){

    //call the function from within main()
    greeting();
    
    return 0;

}

//function definition
void greeting(){
    printf("Saluton Mundo!\n");
}


Next, let’s call a function that accepts parameters but still does not return a value to the calling function. A function that does not return a value is void. Like our previous function, we can cannot include the calling function in another expression.

#include <stdio.h>

void printMessage(int i);

int main(void){

    printMessage(5);
    printf("\n\n");
    printMessage(3);

    return 0;

}

void printMessage(int i){
    while(i--){
        printf("Do you have stairs in your house?\n");
    }
}

Note that a function can be called multiple times. This is part of the value of functions.

Prototype declarations consist of only a function header containing three elements: the return type, the function name, and the formal parameter list. Be aware that function prototypes are terminated with a semicolon.

Our next program contains a function that passes arguments and returns a value, in this case, square of the parameter. Since this function returns a value, we can use it as if it were a variable or a literal value in another expression or statement. We will use fgets() to retrieve the input from standard input, instead of scanf(), as fgets() is somewhat safer to use.

#include <stdio.h>
#include <stdlib.h>

int getNumber(void);
//return number squared
int getSquare(int a);

int main(void){

    int a = getNumber();

    int aSquare = getSquare(a);

    printf("%d squared is %d\n", a, aSquare);

    a = getNumber();

    printf("%d squared is %d\n", a, getSquare(a));


    return 0;

}

int getSquare(int a){
    return a * a;
}

int getNumber(void){

    //accept an int of up to 10 digits
    char buffer[10];

    printf("Enter a number of up to ten digits in length: ");
    fgets(buffer, sizeof(buffer), stdin);

    return atoi(buffer);


}

The function definition contains the code for a function; it has two parts – the function header and the function body. The function body is a compound statement, meaning that is a code block delimited by curly braces that can be used in place of one single statement. A function header has three parts: the return type, the function name or identifier, and the formal parameter list. Technically speaking, the parameters are the values received by a function, whereas the arguments are the values sent to the function in the actual function call; however, we often see the terms parameters and arguments used interchangeably, and in some languages this distinction is not maintained at all.

If the return type is not specified the C compiler will assume a return type of int. If the function type is void we do not have to specify a return statement; however, we can include a return statement in a void function if we want the function to conditionally return execution back to the calling function early. We will look at an example of this now.

#include <stdio.h>


void factorialNumber(int n);


int main(void){

    for(int i = 0; i < 10; i++){
        factorialNumber(i);
    }

    return 0;

}


void factorialNumber(int n){
    //local variable;
    int j = 1;
    //if n is 0, return early
    //or if n is less than zero
    if((!n) || (n<0)){
        return;
    }

    printf("!%d = ", n);

    while(n){
        j*=n--;
    }

    printf("%d\n", j);

 
}

In the definition of a function, the parameters are listed in the formal parameter list. This lists defines and declares all of the local variables that will be assigned copies of the data sent to the function via the function call. In the case of arrays, the parameter contains a copy of the memory address of the first element in the function. Multiple parameters should be separated by commas.

Function calls have an extremely high level of precedence, which means they can be easily used in other expressions as we can be sure the function will resolve before the returned value is used.

#include <stdio.h>
#include <stdlib.h>

//fuction protoypes
int getNum(void);
int getDigits(int a);

int main(void){
    
    printf("Enter a number and we will print it backwards: ");

    getDigits(getNum());

    return 0;

}


int getNum(void){

    char buffer[10];


    return atoi(fgets(buffer, sizeof(buffer), stdin));
}

int getDigits(int a){
    while(a > 0){
        printf("%d", a % 10);
        return getDigits(a /= 10);
    }
    printf("\n");
    return 0;
}

Well, that’s enough mischief for today. If you’d like to learn more about C, following a series of structured lessons with plenty of example code, purchase a copy of my book on C at http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s