Functions in C Programming

functions in C programming

Functions in C Programming

C functions are divided into a large program into the basic building blocks. It contains the set of programming statements enclosed by {}. Functions in c programming can be called multiple times to provide reusability and modularity to the C program. The function defines the collection of functions that creates a program. A function is a block of statements that executes only when it is called somewhere in the program. A function provides reusability and modularity of the same code for different inputs, hence saving time and resources. 

Function Aspects

There are three aspects of a C function.

 

Function declaration in C

A function is declared globally in a c program to tell the compiler about the function name, parameters, and return type

 

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
return_type function_name(parameters);
return_type function_name(parameters);
return_type function_name(parameters);

Program of creating function in c programming

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
//function declaration
void MyFunction();
int main (){
//function calling
MyFunction();
return 0;
}
//function definition
void MyFunction(){
printf("%s\n","Hello World!");
}
#include <stdio.h> //function declaration void MyFunction(); int main (){ //function calling MyFunction(); return 0; } //function definition void MyFunction(){ printf("%s\n","Hello World!"); }
#include <stdio.h>
//function declaration
void MyFunction(); 

int main (){
  //function calling
  MyFunction();
  return 0;
}
//function definition
void MyFunction(){
  printf("%s\n","Hello World!");  
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello World!
Hello World!
Hello World!

 

Function call 

The function can be called from anywhere in the program. The parameter list is not different in function calling and declaration. We have passed the same number of functions as it is declared in the function declaration. It is followed by parenthesis containing the function’s parameter(s) if it has any and semicolon 

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//Defining function
return_type function_name(parameters) {
statements;
}
//Calling function
function_name(parameters);
//Defining function return_type function_name(parameters) { statements; } //Calling function function_name(parameters);
//Defining function
return_type function_name(parameters) {
  statements;
}

//Calling function
function_name(parameters);

Program for function call in c programming

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
void HelloWorld(){
printf("%s\n","Hello World!");
}
int main (){
HelloWorld();
return 0;
}
#include <stdio.h> void HelloWorld(){ printf("%s\n","Hello World!"); } int main (){ HelloWorld(); return 0; }
#include <stdio.h>
 
void HelloWorld(){
  printf("%s\n","Hello World!"); 
}

int main (){
  HelloWorld();
  return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello World!
Hello World!
Hello World!

 

Function definition in C

Function definition contains the actual statements which are to be executed.

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
return_type function_name(type arg1, type arg2 .....) {
body of a function
return (expression);
}
return_type function_name(type arg1, type arg2 .....) { body of a function return (expression); }
return_type function_name(type arg1, type arg2 .....) {
    body of a function
    return (expression);
}

 

Types of function

There are two types of function they are follows

  • Standard library functions
  • User-defined functions

Different aspects of function calling

A function may or may not accept any argument and return any value. Based on these, there are four different aspects of function calls.

  • function without arguments and without return value
  • function without arguments and with return value
  • function with arguments and without return value
  • function with arguments and with return value

 

Advantage of functions in C

  • Avoid rewriting the same code again in a program by using function.
  • Call the  C functions many times in a program.
  • Reusability is the main part of C functions.

 

Also read, C function arguments and return value

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *