Functions in the C

Language 

 27 Ekim 2021 - Türkiye



Follow
Follow
Follow

8 dakikalık okuma

Functions in the C programming

The C language is similar to most modern programming languages ​​in that it enables the use of functions, self-contained “modules” of code that take input, perform calculations, and produce output. C functions must be TYPED (the return type and the type of all specified parameters). As always, a function is a module of code that receives information (referring to that information with local symbolic names called parameters), performs certain calculations, and (usually) returns new information based on the parameter information.

function

In order for a function to “see” (use) another function, the “prototype” of the function must be seen in the file before it can be used. If a function uses another function that is verbatim written about it in the file, then this is automatically true. If the function uses a function that is “below” in a file, the prototype should appear at the top of the file … see prototypes below.

In C all functions must be written in order to return a certain type of information and to receive certain types of data (parameters). This information is communicated to the compiler via a function prototype.

Here is the syntax for the function declaration or Prototype:

RETURN_TYPE 
name_of_function (PARAMETER_TYPE name_of_param,PARAMETER_TYPE name_of_param); 
 
// here are some examples of prototypes used at the top of a file: 
float sqrt( float x ); 
 
float average( int grades[], int length ); 

A prototype can appear at the beginning of a C source file to describe what the function returns and what it needs (return type and parameter list). followed by a semicolon. The function prototype is also used at the beginning of the code for the function. Therefore, the prototype can appear twice in a source code file in C. NO semicolon is used when the prototype appears with the code.

In C, the “main” function is treated like all functions, it has a return type (and in some cases it accepts input via parameters). The only difference is that the main function is “called” by the operating system when the user runs the program. Therefore, the main function is always the first code that is executed when a program is started.

	int    // the main function will usually returns a 0 if successful 
	main() // this is the name, in this case: main 
	{ 
	// this is the body of the function (lots of code can go here) 
	} 

	double
	// this is the return type
	max( double param1, double param2)
	// this is the name, followed by the parameters 
	{ 
	if (param1 > param2) 
		{ 
	return param1;
	// Notice: that param1 is of type double and the return 
	// type is also of type double 
		} 
	else 
		{ 
	return param2; 
		} 
	} 
 
 
 
	void
	// This is the return type 
	// (void means no value is computed and returned by the function!) 
	print_happy_birthday( int age ) 
	{ 
	printf("Congratulations on your %d th Birthday\n", age); 
	return;  // you can "terminate" a void function by using return. 
	// HERE it is REDUNDANT because the function is over anyway. 
	} 

Each C function must specify the type of data generated. For example, the above function max returns a value of type “double”. The line “return X;” must be found, where X is a value or a variable that contains a value of the specified type.

When a code snippet calls or calls a function, it does so using the following syntax:

variable = function-name (args, ...); 

The function name must exactly match the function name in the function prototype. Arguments are a list of values ​​(or variables that contain values) that are “passed” to the function. The number of arguments “passed” to a function must exactly match the number of parameters required for the function. The type of each argument must exactly match the type of each parameter. The type of the return variable must exactly match the return type of the function. The “variable” in the example above must be of a type that matches the return type of the function. The line “return X” is somewhere within the function.The value of X is then copied into the “variable”.

A parameter is the symbolic name of the “data” that goes into a function. There are two ways to pass parameters in C:

pass by value, pass by reference. Passing by value Passing by value means that the data is copied and saved via the parameter name. Any change to the parameter has NO effect on the data in the calling function. Original data in the calling function. Therefore, every change to the parameter is ALSO MADE TO THE ORIGINAL variable. There are two ways to pass a reference parameter:

ARRAYS

Arrays are always passed as a C reference. Any change to the parameter that contains the array changes the value of the original array. The ampersand symbol used in the function “prototyp.

function (& parameter_name) To convert a normal parameter into a passed parameter reference, we use the notation “& param”. The ampersand (&) is the syntax used to tell C that any change in the parameter also modifies the original variable that contained the data.

Pass by Value Example: In C, the default is to pass by value. For example:


	#include <stdio.h>
	//
        // C function using pass by value. (Notice no &) 
        // 
        void 
        doit( int x ) 
        { 
             x = 5; 
        } 
 
        // 
        // Test function for passing by value (i.e., making a copy) 
        // 
        int 
        main() 
        { 
          int z = 27; 
          doit( z ); 
          printf("z is now %d\n", z); 
 
          return 0; 
        } 
        

share code: https://onlinegdb.com/skXfZNv6b

Output:


z is now 27

Pass by Reference Example: I suggest that you use a C++ compiler such as g++ which allows the following pass by reference syntax (a much more modern style). The Syntax is to use the ‘&’ in front of the parameter name in the function declaration. The calling code and usage inside the function are the same as before. For example:

        // 
        // C++ using Reference Parameter! 
        // 
        void 
        doit( int & x ) 
        { 
             x = 5; 
        } 
 
 
        // 
        // Test code for passing by a variable by reference 
        // 
        int 
        main() 
        { 
          int z = 27; 
          doit( z ); 
          printf("z is now %d\n", z); 
 
          return 0; 
        }  
   11 | void doit( int &x )
      |                ^
main.c: In function ‘main’:
main.c:20:11: warning: implicit declaration of function ‘doit’ [-Wimplicit-function-declaration]
   20 |           doit( z );
      |    
 

REFERENCES:

  1. https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions.html


Paylaş:



En Yeni İçerikler