Functions in C Programming - Syntax and Example

<<Previous

Next >>





What is function in C programming?

A lengthy C program can be broken into many small routines referred to as functions. Each function in a C program contains block of statements enclosed by { } which performs one specific task. One of the program's functions is named main() which is always required and listed first. Therefore, a C program is not just one long program, it is made up of many small functions. A function in C is also known as procedure or method or subroutine. The function can be called many times.

C functions - Purpose

Usually, we want our code to be only 20 to 30 lines. However, in the real world, the software is a set of program files or source code that would be much longer. It can run several thousand or million lines of code. If you put an entire program in main(), you would need to spend a lot of time to find any specific piece of code. Also, the same code might need to be used at several points of the software and this causes repetitive code. That is why we break our program into sections. These sections are called functions in C. In this section, we are going to see function definition, function declaration, and their types.

Advantages of functions in C

  • Easy to write and maintain the programs.
  • Provides code reusability and modularity.
  • Easy to track and debug the errors.
  • Called many times and from any place in a program.
  • Used to improve understanding of programs.
  • Used to achieve functionality.

C Function - Components

  • Function declaration  :   Declaration signals the compiler about a function name, return type, and parameter lists.
  • Function definition    :  Definition provides the actual body of the function.
  • Formal parameter      :  Formal Paramaters are the names used inside the function to refer to its arguments.
  • Actual arguments      :  Actual arguments are the values that are passed as arguments when the function is called.

Notice that the terms "parameters" or "arguments" are used interchangeably.

Function declaration

A function must be declared before it is used. A function declaration or prototype is an interface specification. It consists of the function's name, return type, and parameter lists.

Function Declaration Syntax

return-type functionName(data-type parameter1, data-type parameter2,...); //parameter1 and 2 are the formal parameters

For Example:

int add(int a,int b);

A function is declared without a parameter name but a data type must be required. This is also a valid declaration. So the above function declaration can also be written as

int add(int, int);

Notice that the function declarations are usually placed at the top of the source file.

Function Definition

A function definition provides the actual workings of the function. It contains return type, function name which is used to refer to it in a program, list of parameters or arguments which are passed from outside and body of the function which enclosed by curly braces "{ }" .

Function Definition Syntax

return-type functionName(data-type parameter1, data-type parameter2..){

//function body

}

The return type can be any data type. The functions may or may not return the value. If the functions don't return any value, then use the void for the return type.

Function Example

This function adds two int parameters a and b and returns the addition of two number


int add(int a, int b)
{
   int result;
   result = a + b;
  return(result);
}

Calling a function in C

C function can be called from the main() function. It can be called by using function name and with or without arguments. When a program (calling function) calls the function, then the control is transferred to that particular function. A called C function performs the specific task and then returns the control to the calling function.

Syntax:
      variable-name = functionName(arguments list);//arguments list are the actual parameter.

Here the variable-name is optional. If the function returns a value, then that value will be stored into the given variable name. Suppose, if the function return type is void, then the variable is not required.

While calling, we need to pass the required arguments to the called function. In general, the arguments are passed in two ways that are:

  • call by value
  • call by reference

Both these methods pass the variable to the called function from the calling function. There is also a way to return a value from a called function back to the calling function by using the "return" statement.

Difference between call-by-value and call-by-reference

call by valuecall by reference
The copy of the actual value is passed along with the function callAn Address of actual value is passed to the function call.
A memory location is allocated separately for an actual and formal parameter. Both actual and formal parameters use the same memory location.
Any modification of the parameter's value inside the called function is not affected the original value of the parametersChanges made on the parameter's value inside the called function is reflected the original value of the parameters

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














Functions in C Programming - Syntax and Example

<<Previous

Next >>





What is function in C programming?

A lengthy C program can be broken into many small routines referred to as functions. Each function in a C program contains block of statements enclosed by { } which performs one specific task. One of the program's functions is named main() which is always required and listed first. Therefore, a C program is not just one long program, it is made up of many small functions. A function in C is also known as procedure or method or subroutine. The function can be called many times.

C functions - Purpose

Usually, we want our code to be only 20 to 30 lines. However, in the real world, the software is a set of program files or source code that would be much longer. It can run several thousand or million lines of code. If you put an entire program in main(), you would need to spend a lot of time to find any specific piece of code. Also, the same code might need to be used at several points of the software and this causes repetitive code. That is why we break our program into sections. These sections are called functions in C. In this section, we are going to see function definition, function declaration, and their types.

Advantages of functions in C

  • Easy to write and maintain the programs.
  • Provides code reusability and modularity.
  • Easy to track and debug the errors.
  • Called many times and from any place in a program.
  • Used to improve understanding of programs.
  • Used to achieve functionality.

C Function - Components

  • Function declaration  :   Declaration signals the compiler about a function name, return type, and parameter lists.
  • Function definition    :  Definition provides the actual body of the function.
  • Formal parameter      :  Formal Paramaters are the names used inside the function to refer to its arguments.
  • Actual arguments      :  Actual arguments are the values that are passed as arguments when the function is called.

Notice that the terms "parameters" or "arguments" are used interchangeably.

Function declaration

A function must be declared before it is used. A function declaration or prototype is an interface specification. It consists of the function's name, return type, and parameter lists.

Function Declaration Syntax

return-type functionName(data-type parameter1, data-type parameter2,...); //parameter1 and 2 are the formal parameters

For Example:

int add(int a,int b);

A function is declared without a parameter name but a data type must be required. This is also a valid declaration. So the above function declaration can also be written as

int add(int, int);

Notice that the function declarations are usually placed at the top of the source file.

Function Definition

A function definition provides the actual workings of the function. It contains return type, function name which is used to refer to it in a program, list of parameters or arguments which are passed from outside and body of the function which enclosed by curly braces "{ }" .

Function Definition Syntax

return-type functionName(data-type parameter1, data-type parameter2..){

//function body

}

The return type can be any data type. The functions may or may not return the value. If the functions don't return any value, then use the void for the return type.

Function Example

This function adds two int parameters a and b and returns the addition of two number


int add(int a, int b)
{
   int result;
   result = a + b;
  return(result);
}

Calling a function in C

C function can be called from the main() function. It can be called by using function name and with or without arguments. When a program (calling function) calls the function, then the control is transferred to that particular function. A called C function performs the specific task and then returns the control to the calling function.

Syntax:
      variable-name = functionName(arguments list);//arguments list are the actual parameter.

Here the variable-name is optional. If the function returns a value, then that value will be stored into the given variable name. Suppose, if the function return type is void, then the variable is not required.

While calling, we need to pass the required arguments to the called function. In general, the arguments are passed in two ways that are:

  • call by value
  • call by reference

Both these methods pass the variable to the called function from the calling function. There is also a way to return a value from a called function back to the calling function by using the "return" statement.

Difference between call-by-value and call-by-reference

call by valuecall by reference
The copy of the actual value is passed along with the function callAn Address of actual value is passed to the function call.
A memory location is allocated separately for an actual and formal parameter. Both actual and formal parameters use the same memory location.
Any modification of the parameter's value inside the called function is not affected the original value of the parametersChanges made on the parameter's value inside the called function is reflected the original value of the parameters

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy