Home>Article>Backend Development> Functions in C programming

Functions in C programming

WBOY
WBOY forward
2023-08-26 08:33:12 999browse

Functions in C programming

A function is a group of statements that perform a task together. Every C program has at least one function,main(), and all the most trivial programs can define other functions.

You can divide your code into separate functions. How you divide your code between different functions is up to you, but a logical division is such that each function performs a specific task.

FunctionDeclarationTells the compiler about the function’s name, return type, and parameters. FunctionDefinitionprovides the actual body of the function.

The C standard library provides many built-in functions that your program can call. For example,strcat()is used to concatenate two strings,memcpy()is used to copy one memory location to another, and many more functions.

A function can also be called a method, subroutine or procedure, etc.

Define function

The general form of function definition in C language is as follows:

return_type function_name( parameter list ) { body of the function }

The function definition in C programming consists of a function header and a function body. Following are all the parts of a function -

  • #Return Type- A function can return a value. return_type is the data type of the function return value. Some functions perform the required operation without returning a value. In this example, return_type is the keyword void.

  • Function Name- This is the actual name of the function. The function name and parameter list together form the function signature.

  • Parameters- Parameters are like placeholders. When calling a function, you pass a value to the parameter. This value is called the actual parameter or argument. The parameter list refers to the type, order, and number of function parameters. Parameters are optional; that is, a function may contain no parameters.

  • Function body- The function body contains the collection of statements that define the function.

Sample Code

/* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }

Function Declaration

FunctionDeclarationTells the compiler the function name and how to call the function. The actual body of the function can be defined separately.

The function declaration contains the following parts-

return_type function_name( parameter list );

For the function max() defined above, the function declaration is as follows-

int max(int num1, int num2);

The parameter name is not important in the function declaration, only Their type is required, so the following is also a valid declaration -

int max(int, int);

Function calls

When you create a C function, you need to define what the function must do. To use a function, you must call the function to perform a defined task.

When a program calls a function, program control is transferred to the called function. The called function performs a defined task and returns program control to the main program when its return statement is executed or its function closing closing brace is reached.

To call a function, you just need to pass the required parameters along with the function name, and if the function has a return value, you can store the return value. For example -

Sample code

Real-time demonstration

#include
       
        /* function declaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 100; int b = 200; int ret; /* calling a function to get max value */ ret = max(a, b); printf( "Max value is : %d
        

", ret ); return 0; } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }

Output

Max value is : 200

The above is the detailed content of Functions in C programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete