Dan Gookin explains how functions work in the C language. C features functions as part of its standard library. The header files help with function prototyping. You can craft your own functions. Functions accept values in an argument list. Functions are typed by the data type value they return, or void when returning no values. Functions help remove repetition from your code and make code more readable.
- [Instructor] It's possible to write code that uses only the two dozen or so C language keywords, but to really program, you use a function. A function is a machine that does something, like a mini program, a function can take input, and a function can generate output, and it can do both or it can do neither. The C library comes with a host of built-in functions. These are prototyped in the various header files, but the function's code is in the libraries mixed in with your code by the Linker. You can use these provided functions, and you can also create your own functions to do specific tasks for your program.
All programs have and use at least one function, the main() function, where code execution starts. Functions have types based on the information they output. A function is cast based on its output. For example, a function that returns an integer is an integer function such as the main() function. Functions that return memory locations are pointer functions. Functions that return no values use the void data type, which means that they don't return a value. And in C, functions can return only one value.
Functions accept input in the form of arguments. These can be constant values, variables, or expressions, which are passed to the function for manipulation or examination. A function can accept any number of arguments or a function can accept no arguments. To call a function, you specify it as a statement or expression in your code. As a statement, the function appears on a line by itself with any required arguments appearing in parentheses. The full load of arguments are required, and they must match the data type needed or the compiler complains.
If a function returns a value, it can be captured in a variable, used as an expression within another function, or ignored. This function has two arguments, a string literal and a variable. Prize, it's called as its own statement and any return value is ignored. In this statement, the square root function is called with argument b. The value returned is assigned to variable a. Here, the value returned from the square root function is used as an argument within the printf function. When you code a function, you set its type for the value returned or use void when no value is returned.
Then comes the function's name. Functions are named just like variables. Start with a letter followed by letters and numbers. Only the first 31 characters of the name are significant. Function names are case-sensitive and traditionally written in lowercase or camelCase. After the function name comes a list of arguments in parentheses typed as necessary. Functions with no arguments list void in the parentheses. A set of curly braces encloses the function's statements. Here are three sample function declarations.
The first is count. It accepts two arguments, both integers, which are named start and end, and used within the function. It returns an integer value. The second is named repeat. It requires a single integer argument r. The function returns no values, so its data type is void. And the third function, something, accepts no arguments and returns no values. Before you can use a function in your code, you must introduce it to the compiler. That way, the compiler can check that the function is used properly, called with the proper arguments, and so on.
To introduce a function, you prototype it. This involves writing a statement that appears before the function is used. The statement is a repeat of the function's declaration, but it ends with a semicolon. Or alternatively, you can write the function first in your code before the function is called elsewhere. The C language offers no rules for when a function is necessary or required. It's best, however, to add a function when you use the same statements or perform the same process at several points in the code. For example, you prompt for input several times, so write a function to carry out that repetitive task.
You can build a function to handle a specific task. For example, put the statements that handle a complex calculation into a function. This isn't a rule, but it can help make your code more readable. And you can write a function whenever the mood hits you. The one goal I recommend you keep is to have readable code. Functions can help you accomplish that.
- Working the C development cycle
- Writing a simple program
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Specifying characters and strings
- Working with math operators
- Creating a for loop
- Prototyping a function
- Creating recursive functions