Computer Science

C Functions

C functions are blocks of code that perform a specific task and can be called from other parts of a program. They help in organizing code, making it more modular and easier to maintain. Functions in C can take input parameters, perform operations, and return results, providing a way to encapsulate and reuse code.

Written by Perlego with AI-assistance

3 Key excerpts on "C Functions"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • The SAGE Encyclopedia of Educational Research, Measurement, and Evaluation

    ...Functions use input to produce output, although this is not always the case. Some functions perform tasks with no explicit input values required, and others use input to perform a task with no specific output values. The main function is a requirement in C code when it is compiled to produce an executable program. A collection of functions written in a single file to be used across multiple programs is called a library (which may have a static library file extension, *.a or *.lib, or dynamic library file extension, *.so or *.dll). A library may contain header files (with the file extension *.h), which are preprocessing directives that are evaluated before source code (i.e., the program that a user writes). A primary example of this is the C Standard Library, and it contains a standardized set of header files that allows for basic commonly used functions to be used by programs. More specifically, a header file (e.g., <stdio.h>) contains the standard input/output functions that are not reserved words in the C language. Under the current C11 standard, there are 44 reserved words in C that cannot be used for naming variables or functions; for instance, a variable cannot be named int because the compiler understands that term to denote the integer data type. If the end goal is a user-written library, an executable program is not required because libraries typically do not have an execution thread of their own. There are four basic data types that can be specified in C. First, arithmetic data types are used to store alphanumeric symbols in memory (i.e., char, int, float, and double). Second, type modifiers can be used to denote the possibility of negative values (i.e., signed vs. unsigned) or the amount of memory needed (e.g., the number of decimal places) for that particular value (i.e., short vs. long). Third, enumerated data types are useful for situations involving loops and discrete calculations, such as dummy codes and categorical indicators...

  • Using LEDs, LCDs and GLCDs in Microcontroller Projects
    • Dogan Ibrahim(Author)
    • 2012(Publication Date)
    • Wiley
      (Publisher)

    ...A function can be thought of as a self-contained, testable program code, developed to perform a specific task. Functions are also created when it is required to repeat a certain algorithm at several different places of the main program. For example, it may be required to convert the temperature from °F into °C at several places in a program. Instead of repeating the code, it is more efficient and the program is more maintainable if a temperature conversion code is written in the form of a function. This function can then be called whenever it is required to make the required conversion. The general format of a function declaration is as follows: Functions usually (not always) perform a certain operation and return data to the calling program. The function type indicates the type of returned data, name is the name of the function, and the parameters (if any) should be separated by commas. The statements inside the function should be enclosed within a pair of curly brackets. An example function declaration is given below, which calculates the circumference of a circle and returns to the calling program. Here, the radius of the circle is passed as an argument to the function: Assuming that we wish to calculate the circumference of a circle whose radius is 2.5 cm, and store the result in a variable called Circ, the above function can be called, as shown below: Some more examples are given below. Example 3.8 Write two functions to calculate the area and volume of a cylinder. Show how these functions can be used in a program to calculate the area and volume of a cylinder whose radius and height are 3.0 and 12.5 cm, respectively. Store the area in variable c_area, and the volume in variable c_volume. Solution 3.8 Figure 3.6 shows the two functions. Function Cyl_Area calculates the area of a cylinder...

  • Principles of Quantitative Development
    • Manoj Thulasidas(Author)
    • 2012(Publication Date)
    • Wiley
      (Publisher)

    ...These lexical constructs drastically reduce the code required to implement repetitive operations. For instance, if we have a large number of trades and we want to price them, we can set up a loop to do it. Loops help us minimize repetitions to some extent and introduce the notion of code reusability. However, in order to really reuse code snippets, we need functions. Functions are self-contained units of code that carry out specific tasks. Using functions and loops, it is possible to organize a series of steps that accomplish most simple (and many complex) programming tasks. Ideally, functions take arguments and return results based on their values. However, in computing, functions may carry out other operations, and their behaviour may depend on the state of the program, not merely their arguments. Since arguments are, in reality, labels of memory locations in a conventional programming language, the implementation of function calls becomes a matter of choice – not ours, but of the language designer’s. How exactly does it pass an argument to a function? Consider a simple function call y = f(x). For a computer, it can mean, ‘Take the contents of the memory location x, and pass it to the function f(x) ’. It can also mean, ‘Pass the label x to the function f(x).’ The former is called passing argument by value, while the latter is passing it by reference. The intricacies of a function call do not end there. When an argument is passed to a function by value, in reality it is the label of a temporary memory location, containing a copy of the original argument, that is passed. This implicit copying may be innocuous for simple built-in types, but when functions start passing complex and large data structures (like trade objects), the copy process takes time. Worse, the copy may be imperfect...