C Functions (original) (raw)

A **function in C is a set of statements that, when called, perform some specific tasks. It is the basic building block of a C program that provides modularity and code reusability. They are also called subroutines or procedures in other languages.

Function Definition

A function definition informs the compiler about the function's name, its return type, and what it does. It is compulsory to define a function before it can be called.

C `

return_type name () { // Body of function };

`

where,

**Example:

C `

void hello(){ printf("GeeksforGeeks"); }

`

In the above code, **hello is the name assigned to the function and its return type is **void.

Function Call

After defining a function, you can use it anywhere in the program by simply calling it with its name followed by **parentheses ().

**Example:

C `

#include <stdio.h>

// Function definition void hello() { printf("GeeksforGeeks"); } int main() {

// Function call
hello();
return 0;

}

`

In this example, inside the main function, calling the function by typing its name followed by parentheses. When it called, the function body is executed, and it prints "**Hello World".

Return Type of Functions

A function can return a value to its caller as a result. It is called the **return value, and the type of this value is called return type of the function. The function only returns one value, and the value time is the same as the function's return type.

The **return keyword is used to return some values from the function.

**Example:

C `

#include <stdio.h>

int getThree(){ int n = 3; return n; }

int main() {

// Print the value that
// is return by getThree()
// function
printf("%d", getThree());
return 0;

}

`

In this program, the **getThree() function is of integer type, meaning that when you call this function, it returns an integer value, which is **3.

**Note: void return type is used when no value is returned.

Function Parameters

A function can be provided some values by its caller. These values are called **arguments and are provided at the time of function call. In the function definition, we use the placeholder variables inside the parentheses () to receive these values. These placeholders are called **parameters.

C `

// Parameters in the function definition return_type func(type1 name1, type2 name2, ...){ // .... }

`

**name1, **name2, .... are the names given to the parameters of the function. They will be referred with the same name inside the function.

**Example:

C `

#include <stdio.h>

// Defining a function that // print square of given number void printVal(int num, float real){ printf("%d %f\n", num, real); }

int main() { int a = 3;

// Call the printVal function and pass
// desired values
printVal(a, 1.5);
return 0;

}

`

In the above program, we have passed two values to the **printVal() function: variable **a which is equal to **3 and a float value 1.5. There values are referred inside the function using the corresponding parameter name.

It is to be noted that the **number and types of argument passed in the function call should match the number and types of parameters defined in the function definition. Even the sequence should be same.

There are also other different ways to pass arguments to the function. Refer to this article to know more – **Parameter Passing Techniques in C

Forward Declaration

In C, a function must be defined before it is called, or the compiler will generate an error. To prevent this, we can declare the function ahead of the call and definition, providing the compiler with its name, return type, and parameters. This is known as a **forward declaration.

C `

return_type name();

`

The above statement tells the compiler about a **function's name and **return type. Mentioning parameters in the declaration is optional but valid.

C `

return_type name(type1, type2, ...);

`

This type of function declaration is known as a **function prototype or **function signature. If it appears before the function call, we can define the function anywhere in the program.

**Note: Function declarations are required, but since the function definition includes the declaration, it’s not necessary to declare it separately when the function is defined at the beginning.

Local Variables

Variables declared inside a function are called **local variables because they are only accessible within that function. We cannot access them outside the function.

**Example:

C `

#include <stdio.h>

int getThree(){ int n = 3; return n; }

int getThreeDummy(){

// Try to access a variable
// of another function
return n;

}

int main() {

printf("%d", getThreeDummy());
return 0;

}

`

**Output

main.c: In function ‘getThreeDummy’:
main.c:12:12: error: ‘n’ undeclared (first use in this function)
12 | return n;
| ^

In the above program, we attempt to access a variable from another function, but the compiler gives an error saying that the variable is undefined. This means the local variable is not accessible outside the function.

In contrast, **global variables can be accessed anywhere in the program, including inside functions.

Library Functions

These functions are part of C libraries, which are pre-defined and perform specific tasks. By using them, you can save time and avoid errors with different inputs, as these functions are already tested and optimized for use.

**Example:

C `

#include <stdio.h> #include <math.h>

int main() { int n = 3; int m = 6;

// Using fmax() from math.h to find the maximum
printf("%.0f", fmax(n, m));

return 0;

}

`

In the above program, the fmax() function in the code returns the larger of the two values passed as arguments. It is defined in the <math.h> header file.

Main Function in C

If you read the article carefully, you will notice that we use the main function in every program example. In C programming, there is an entry point where the program starts its execution. So **main() function is nothing it is an entry point of a program. The return value of main function indicates a program successfully or unsuccessfully execute or not.

Memory Management of C Functions

When a function is called, memory for its variables and other data is allocated in a separate block in a stack called a **stack frame. The stack in which it is created is called **function call stack. When the function completes its execution, its stack frame is deleted from the stack, freeing up the memory.

Refer to this article to know more - Function Call Stack in C

Recursion

**Recursion is a technique where a problem is solved by breaking it into smaller subproblems. It involves a function calling itself, known as a recursive call, and the function is called a recursive function.

Advantages of Functions in C

Functions in C is a highly useful feature of C with many advantages as mentioned below:

  1. The function can reduce the repetition of the same statements in the program.
  2. We can achieve recursion only if functions exist.
  3. Using functions, the code becomes more readable by converting repetitive tasks into functions.
  4. Functions can call any number of times.
  5. Once the function is declared you can just use it without thinking about the internal working of the function.

Disadvantages of Functions in C

The following are the major disadvantages of functions in C:

  1. Cannot return multiple values.
  2. Memory and time overhead due to stack frame allocation and transfer of program control.