C features functions as part of its standard library, though you can also write your own, with each accepting values, returning values, processing or generating information, or a combination of all three. Functions are the workhorse of the C language, helping accomplish basic and complex tasks in the code, eliminating repetition, and making code more readable.
- [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.
- Writing your first C program
- Working the C development cycle
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Making new data types
- Typecasting variables
- Using math, assignment, and logical operators
- Creating for and while loops
- Nesting loops
- Creating functions
- Returning values from functions
- Passing arguments to functions
- Creating recursive functions
Skill Level Intermediate
C: Data Structures, Pointers, and File Systemswith Dan Gookin2h 57m Intermediate
1. The Fundamentals of C
2. Variables and Values
3. Operators and Math
4. Decisions and Loops
Next steps1m 38s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.