From the course: C Essential Training

Understanding functions - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Understanding functions

- [Instructor] It's possible to write code that uses only C language keywords but to really program, you use a function. A function is a programming machine that does something, like a mini program. A function can take input, a function can generate output. It can do both or it can do neither. The C library comes with a host of built-in function. These are prototyped in the various header files but the function's code is kept in libraries, mixed in with the linker. You use these library functions but you can also create your own functions to do specific tasks. All C programs have and use at least one function, the main function where code execution starts. Like all C functions, the main function has a data type, the same as a variable. The data type reports what kind of value, if any, the function generates. For the main function, it outputs an integer value, set at line seven by the return statement. A function's data type and its output must match. This exercise file shows five functions, each of a different basic type. The valchar function is a character function, returning a character value. Valint is an integer function, returning an integer. Valfloat returns a floating point value. And valdouble returns a double precision value. The void data type function, valvoid, returns nothing, it's void. Regardless of the type, C functions can return only a single value. The return keyword is what sends the value, which can be literals, the value held in a variable or returned from another function back to the calling statement. The main function returns its value to the operating system. Functions that accept input have the values listed in their parentheses. These are called arguments. The repeat function in this exercise file accepts a single argument, an integer value. Variable R represents this value within the function. At line 15, the repeat function is called. Its argument is specified in parentheses. It can be a literal value, a variable, or an expression but the value must match the function's argument data type. Integer here. A function can accept any number of arguments or none. Here the total function accepts five arguments, each having a data type and a variable name. The function is called at line 17 in the main function. It's called within this printf function, which has been split across multiple lines. The title function is a character function. It returns a pointer, a string. Its argument is void, meaning that it has no argument. It doesn't expect any, so the call to this function at line 16 shows nothing in the parentheses. The value returned from such a function can be used immediately, as it is here in this printf statement, or it can be stored in another variable or even discarded. In or out or not at all. This is how functions in C return values and deal with arguments.

Contents