Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Functions

Function

In programming, a function is a block of code that performs a specific task and can be called from other parts of a program. It can take input (arguments), perform some operations on them, and then return a result. Functions are used to modularize code, improve code readability, and reduce code duplication.

Syntax:

Here’s the syntax for a function in C programming language:

return_type function_name(argument1, argument2, ..., argumentN) {
    // code to perform the task
    return result;
}
  • return_type specifies the type of data that the function will return. For example, it could be int, float, double, char, or void (if the function doesn’t return anything).
  • function_name is the name of the function. It should be a descriptive name that indicates what the function does.
  • argument1, argument2, ..., argumentN are the input arguments that the function takes. They are optional, but if the function requires input, then it should specify the data types and names of the arguments inside the parentheses.
  • The body of the function is enclosed in curly braces {}. This is where you write the code that performs the task.
  • return statement is used to return a value from the function. If the function doesn’t return anything, then the return type should be void and no return statement is necessary.

Example:

Suppose you are designing a program to perform some mathematical calculations on a set of numbers. Rather than writing the code for each calculation directly in your main program, you can define functions to perform each calculation and then call those functions from your main program.

Here’s an example program that uses functions to perform some mathematical calculations:

#include <stdio.h>

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

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

int main() {
    int x = 5;
    int y = 7;
    int z;

    z = sum(x, y);
    printf("Sum of %d and %d is %d\n", x, y, z);

    z = product(x, y);
    printf("Product of %d and %d is %d\n", x, y, z);

    return 0;
}

In this program, we define two functions called sum and product, which take two integer arguments and return the sum and product of those arguments, respectively.

In the main function, we create two integer variables called x and y, and then call the sum function with those variables as arguments to compute the sum of x and y. We store the result of the sum function in a variable called z, and then use printf to print out the result.

We then call the product function in a similar way to compute the product of x and y, and again use printf to print out the result.

When we run this program, the output will be:

Sum of 5 and 7 is 12
Product of 5 and 7 is 35

Declaring, Defining and Calling

In C programming, a function needs to be declared, defined, and called in order to be executed. Here is what each of these steps means:

  • Declaring a Function: Before you can use a function in your program, you need to declare it. A function declaration tells the compiler about the function name, return type, and parameter list. It does not contain the actual code for the function. Here’s an example of declaring a function:
int max(int a, int b);  // function declaration
  • Defining a Function: After declaring a function, you need to define it. The function definition contains the actual code that performs the function’s task. It includes the function header, which specifies the function name, return type, and parameter list, as well as the function body, which contains the code that performs the function’s task. Here’s an example of defining a function:
int max(int a, int b) {  // function definition
    if (a > b)
        return a;
    else
        return b;
}
  • Calling a Function: Once you have declared and defined a function, you can call it in your program to perform its task. To call a function, you simply provide its name and the arguments it requires (if any) in parentheses. The function then executes and returns its result (if it has a return value). Here’s an example of calling a function:
result = max(x, y);  // calling the max function

In summary, declaring a function tells the compiler about the function name, return type, and parameter list. Defining a function provides the actual code that performs the function’s task. Calling a function executes the function and returns its result.

Here’s an example code of with demonstration.

#include <stdio.h>

// function declaration or prototyping
int max(int a, int b);

int main() {
    int x = 5, y = 10, result;
    
    // calling the max function
    result = max(x, y);
    
    // printing the result
    printf("The maximum of %d and %d is %d\n", x, y, result);
    
    return 0;
}

// function definition
int max(int a, int b) {
    if (a > b)
        return a;
    else
        return b;
}

Call by Value and Call by Reference

Call by value and call by reference are two different ways of passing function arguments to a function in C.

  • In call by value, the function argument is copied into the formal parameter of the function. In this case, changes made to the parameter have no effect on the argument. For Example
void increment(int x) {
    x = x + 1;
    printf("Inside function: %d\n", x);
}

int main() {
    int num = 5;
    increment(num);
    printf("Outside function: %d\n", num);
    return 0;
}
 

In the above example, the increment() function takes an integer argument x by value. The function increments the value of x by 1 inside the function, but this change is not reflected in the original value of num outside the function.

  • In call by reference, the function argument is passed by reference, which means that a reference or pointer to the argument is passed to the function. The function can modify the original value of the argument passed to it. Changes made to the argument inside the function are reflected in the original value of the argument outside the function. For Example
void increment(int *x) {
    (*x)++;
    printf("Inside function: %d\n", *x);
}

int main() {
    int num = 5;
    increment(&num);
    printf("Outside function: %d\n", num);
    return 0;
}

Another example, converting lower case to upper case

#include <stdio.h>
#include <ctype.h>  // We need this library for the toupper() function

void toUpperCase(char str[]) {
    int i = 0;
    while (str[i] != '\0') {
        str[i] = toupper(str[i]);
        i++;
    }
}

int main() {
    char str[] = "hello world";
    printf("Before: %s\n", str);  // This will output "hello world"
    toUpperCase(str);
    printf("After: %s\n", str);  // This will output "HELLO WORLD"
    return 0;
}

The return Statement

The return statement has two important use cases, First, it cause an immediate exit from the function that it is in. Second, it may be used to return a value.

Exit early and Return a value

  • To exit the function early: A function can also use the return statement to exit the function early if certain conditions are met. This can be useful in cases where the function cannot complete its operation due to some error or unexpected condition. For example, a function that searches for a specific value in an array can use the return statement to exit the function early if the value is found, as follows:
int find_index(int array[], int size, int value) {
    for (int i = 0; i < size; i++) {
        if (array[i] == value) {
            return i;
        }
    }
    return -1; // Return -1 if value not found
}

In this example, the find_index function searches for the index of the first occurrence of value in the array and returns the index if it is found using the return statement. If the value is not found, the function returns -1 using another return statement after the loop.

  • To return a value from the function: A function can perform some operations on its inputs and return a result to the calling code using the return statement. For example, a function that adds two integers and returns the result can use the return statement as follows:
int add(int a, int b) {
    int result = a + b;
    return result;
}

In this example, the add function calculates the sum of a and b and returns the result to the calling code using the return statement.

Returning Pointers

Function of Type void

A function of type void is a function that does not return any value. This means that the function performs some operations or tasks, but it does not produce any result or output that can be used by the calling code.

Here is an example of a function of type void:

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5;
    int y = 10;

    printf("Before swap: x = %d, y = %d\n", x, y);

    swap(&x, &y);

    printf("After swap: x = %d, y = %d\n", x, y);

    return 0;
}

Functions of type void can be used to perform operations that update the state of a program or data structure, but do not need to return a value. In this case, the swap function modifies the values of its input arguments, which allows the calling code to access the updated values after the function call.

What does main( ) Return?

The main() function in C/C++ is a special function that serves as the entry point for a program. It is called by the operating system when the program is run, and it is the first function to be executed.

In C/C++, the main() function is declared with a return type of int, which means that it is expected to return an integer value to the operating system when it finishes executing. The return value is a status code that indicates whether the program terminated successfully or encountered an error.

By convention, a main() function that terminates without errors returns a value of 0, which signifies success. However, you can choose to return any integer value that makes sense for your program, depending on how you want to handle success and failure cases.

Here’s an example of a main() function that returns an integer value:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");

    return 0; // Return 0 to indicate success
}

In this example, the main() function prints the message “Hello, World!” to the console using the printf() function, and then returns 0 to the operating system to indicate that the program has completed successfully.

    Leave a Reply

    Your email address will not be published.

    Need Help?