Functions in C – how it’s work

blog img

In C, functions are blocks of code that perform specific tasks. Functions allow you to modularize your code, making it easier to read, maintain, and reuse. Here’s how functions work in C:

Components of a Function

1. Function Declaration (Prototype):

  • This tells the compiler that a function exists. It includes the function’s name, return type, and the parameters (if any) the function takes.
  • Example: int sum(int a, int b);

2. Function Definition:

  • This is where you write the actual code that the function will execute. It must match the function declaration in terms of return type and parameters.
  • Example:

c

int sum(int a, int b) {
    return a + b;
}

3. Function Call:

  • To execute the function, you need to call it in the main() function or another function by passing arguments (if needed).
  • Example: result = sum(num1, num2);

Basic Structure of a Function in C

c

#include <stdio.h>

// Function declaration
int sum(int, int);

int main() {
    int a = 5, b = 10;
    int result;

    // Function call
    result = sum(a, b);

    // Display the result
    printf("Sum: %d\n", result);

    return 0;
}

// Function definition
int sum(int x, int y) {
    return x + y;
}

How Functions Work in C

1. Function Declaration (Prototype):

  • The function declaration is written before main() or inside a header file. It informs the compiler about the return type, function name, and parameters.
  • Syntax:

c

return_type function_name(data_type parameter1, data_type parameter2);

2. Function Definition:

  • The function definition contains the actual code that the function will execute.
  • Syntax:

c

return_type function_name(data_type parameter1, data_type parameter2) {
    // Body of the function
    return value; // if the function has a return type
}

Function Call:

  • To execute the function, you call it by its name and pass any necessary arguments. The function processes the arguments and returns a value (if it has a return type).
  • Example:

c

int result = sum(5, 10);

Returning a Value:

  • If a function has a return type (like int, float, etc.), it must use the return statement to return a value. If the function returns void, it does not return any value.

How the Program Executes with Functions:

Execution begins in the main() function: This is where the program starts running.

Function Call: When the main() function encounters a function call (e.g., sum(a, b)), it pauses and jumps to the called function.

Argument Passing: The values of a and b are passed to the parameters of the sum() function. Inside the sum() function, the local variables x and y take on the values of a and b.

Function Execution: The sum() function performs its task (in this case, adding x and y) and then returns the result.

Returning to Main: After the function completes, control returns to the point in the main() function where it was called. The returned value is stored in the result variable.

Completion: The program prints the result and then continues with the rest of the code, if any.

Types of Functions in C

1. No Arguments, No Return Value:

  • The function performs a task but does not take any input or return any value.
  • Example:

c

void sayHello() {
    printf("Hello, World!\n");
}

Arguments, No Return Value:

  • The function takes input (arguments) but does not return any value.
  • Example:

c

void printSum(int a, int b) {
    printf("Sum: %d\n", a + b);
}

No Arguments, Returns a Value:

  • The function does not take input but returns a value.
  • Example:

c

int getNumber() {
    return 42;
}

Arguments, Returns a Value:

  • The function takes input and returns a value. This is the most common type of function.
  • Example:

c

int multiply(int a, int b) {
    return a * b;
}

Why Use Functions?

Modularity: Break large programs into smaller, manageable pieces.

Reusability: Once a function is defined, it can be used multiple times in different parts of the program or other programs.

Ease of Debugging: Since functions perform specific tasks, errors can be isolated and fixed more easily.

Maintainability: Functions make the code cleaner and more organized, making future modifications easier.

Code Example: Sum of Two Numbers Using a Function

c

#include   // Line 1

// Function declaration/prototype
int sum(int a, int b);  // Line 2

int main() {  // Line 3
    int num1, num2, result;  // Line 4
    
    // Asking the user to input two numbers
    printf("Enter two integers: ");  // Line 5
    scanf("%d %d", &num1, &num2);    // Line 6

    // Calling the function 'sum' with the two numbers as arguments
    result = sum(num1, num2);  // Line 7

    // Displaying the result
    printf("The sum is: %d\n", result);  // Line 8

    return 0;  // Line 9
}

// Function definition
int sum(int a, int b) {  // Line 10
    return a + b;  // Line 11
}

Explanation of Every Line

#include <stdio.h>:

This line includes the Standard Input/Output library. It is required to use the printf and scanf functions for input and output.

int sum(int a, int b);:

This is the function declaration or prototype. It tells the compiler that a function named sum exists, takes two int parameters, and returns an int. The actual code for this function is defined later in the program.

int main():

This is the starting point of the program. Every C program must have a main() function. The execution of the program starts here.

int num1, num2, result;:

Three integer variables are declared: num1 and num2 will store the user input, and result will store the result returned by the sum function.

printf("Enter two integers: ");:

This line prints a message to the user asking them to enter two integers.

scanf("%d %d", &num1, &num2);:

This line takes input from the user. The scanf function reads two integers and stores them in the memory addresses of num1 and num2. The %d format specifier is used for integers.

result = sum(num1, num2);:

This line calls the sum function, passing the values of num1 and num2 as arguments. The result of the function is stored in the result variable.

printf("The sum is: %d\n", result);:

This line prints the result of the sum. The %d format specifier is used to print the integer value of result.

return 0;:

This line indicates that the main() function has finished executing and returns a value of 0, indicating successful execution.

int sum(int a, int b):

This is the function definition. The function named sum takes two integers a and b as input and returns an integer.

return a + b;:

This line performs the addition of a and b and returns the result to the caller.

How the Program Works:

The program starts by executing the main() function.

The user is prompted to enter two integers.

The integers are stored in num1 and num2.

The sum function is called with num1 and num2 as arguments.

The sum function computes the sum of the two numbers and returns the result.

The result is printed on the screen.

The program terminates successfully.

Share your thoughts

Your email address will not be published. All fields are required.

Related post

  1. Easy to read and write the JSON file

    In C, you can handle JSON data by using libraries…

  1. Easy to create a file in C program.

    To create a file in C programming language, you typically…

  1. Easy to creating a Simple calculator in C.

    Here’s a simple calculator program in C that performs basic…

  1. Easy way to learn – Arithmetic operators in C

    Here’s a simple example of a C program that performs…

  1. How to learn constructor – in C

    In C, a constructor is not a built-in feature like…

Popular post

  1. Eclipse IDE – Create New Java Project.

    Opening the New Java Project…

  1. How to start the project in android studio

    Android Studio Open the Android…

  1. How to use ACOSH function in excel

    The ACOSH function returns the…

  1. Complete Header tags in html – easy to learn

    H tags can be used…

  1. Best features in Python programme – easy to learn

    Python is the most widely…