Loading...
C User-defined Functions

C User-defined Functions

In this tutorial, you will learn about the C User-defined functions, its declaration, prototype, parameters with the help of examples.


User-defined Function

  • User-Defined function are those functions which are defined by user.
  • It allows performing the additional functions besides the in-build functions.
  • A user-defined function groups code to perform a specific task and that group of code is given a name (identifier).
  • When the function is called directly from any part of the program, it will executes the codes defined in the body of the function.

Function Declaration

  • In C programming, the function declaration tells the compiler about a function name and how to call the function.
  • A function declaration also tells about the number of parameters function takes, data-types of the parameters and the return type of the function.
  • Putting parameter names in function declaration is Optional in the function declaration, but it is necessary to put them in the definition.

Syntax :

returnType functionName (parameter1, parameter2,...)
{
  // function body
}

Example :

// function declaration
void hello() {
  printf("Hello World!");
}

In the above example,

  • the name of the function is hello()
  • the return type of the function is void
  • the empty parentheses mean it doesn't have any parameters
  • the function body is written inside {}

Note: We will learn about returnType and parameters later in this tutorial.


Advantages of Using User-Defined Functions

  • Reduction in size - This avoids writing of same code again and again reducing program size.
  • Reducing complexity - Functions make the program easier as each small task is divided into a function. or user defined functions.
  • Readability - Functions increase readability.
  • Easy to Debug and Maintain - During debugging it is very easy to locate and isolate faulty functions. It is also easy to maintain program that uses user-defined functions.
  • Code Reusability - Functions make the code reusable. We can declare them once and use them multiple times.

Calling a Function

  • when a program calls a function, program control is transferred to the called function.
  • A called function performs defined task and when it's return statement is executed, it returns program control back to main program.
  • To call a function, we need to pass the requirement parameters along with function name, and if function returns a value, then we can store returned value.

Example :

In the above program, we have declared a function named hello(). To use the hello() function, we need to call it.

Here's how we can call the above hello() function.

int main() {
  // calling a function
  hello();
}

Function Parameters

  • Parameters are variables to hold values of arguments passed while function is called.
  • A function may or may-not contain parameter list.
  • There are two types of parameters actual and formal parameters that are passed in a function.
  • The actual parameters are passed in a function like a = 20, b = 10.
  • And the formal parameters are those parameters which are received by the function like int c and int d.

As mentioned above, a function can be declared with parameters (arguments). A parameter is a value that is passed when declaring a function.

For example, let us consider the function below:

void printnum(int a) {
  printf("%d ", a);
}

Here, the int variable a is the function parameter.

We pass a value to the function parameter while calling the function.

int main() {
  int a = 7;
  // calling the function
  // a is passed to the function as argument
  printnum(a);
  return 0;
}

Example : program to print integer and floting value using Function with Parameters

// program to print integer and floating value.
#include <stdio.h>
  // print a number
void Number(int num1, float num2) {
  printf("The integer number is %d \n", num1);
  printf("The decimal number is %lf", num2);
}
int main() {
  int a = 2;
  double b = 2.5;
  // calling the function
  Number(a, b);
  return 0;
}

Output

The integer number is 2
The decimal number is 2.5

Working of above program

we have used a function that has one int parameter and one double parameter.

We then pass a and b as arguments. These values are stored by the function parameters num1 and num2 respectively.

Note: The type of the arguments passed while calling the function must match with the corresponding parameters defined in the function declaration.


Function Prototype

  • In C programming, the code of function declaration should be before the function call.
  • However, if we want to define a function after the function call, we need to use the function prototype.
  • It tells the return type of the data that the function will return.
  • It tells the number of arguments passed to the function.
  • It tells the data types of the each of the passed arguments
  • It also tells the order in which the arguments are passed to the function.

Syntax :

returnType functionName(dataType1, dataType2, ...);

Example :

// function prototype
void add(int, int);
int main() {
  // calling the function before declaration.
  add(2, 4);
  return 0;
}
  // function definition
void add(int a, int b)
{
  printf("%d");
}

Output

6

In the above code, the function prototype is:

void add(int, int);

This provides the compiler with information about the function name and its parameters. That's why we can use the code to call a function before the function has been defined.


Example : Add two numbers using Function Prototype

// using function definition after main() function
// function prototype is declared before main()
#include <stdio.h>
// function prototype
int add(int, int);
int main() {
  int sum;
  // calling the function and storing
  // the returned value in sum
  sum = add(10, 50);
  printf("10 + 50 = %d", sum);
  return 0;
}
// function definition
int add(int a, int b) {
  return (a + b);
}

Output

10 + 50 = 60

The above program is almost similar to previous example. The only difference is that here, the function is defined after the function call.

That's why we have used a function prototype in this example.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of User-Defined Functions in C.

Keep Learning : )

In the next tutorial, you'll learn about C Function Types.

- Related Topics