Function

 Function 

Function declaration :-



In C language, a function can be declared using the following syntax:

return_type function_name(parameter_list);

Here, return_type is the data type of the value that the function returns, function_name is the name of the function, and parameter_list is the list of input parameters that the function accepts.

For example, a function that takes two integers as input parameters and returns their sum can be declared as follows:

int add(int a, int b);

In this example, the function name is add, the return type is int, and the function accepts two int parameters a and b.

Note that the keyword short cannot be used as a substitute for any of the parts of the function declaration syntax in C. The keyword short is used to specify a short integer data type. For example, a short int data type can be declared as follows:

short int num;

calling a function:-

To call a function in C language, you need to use the function name and provide the necessary input parameters. The syntax for calling a function in C is:

function_name(argument_list);

Here, function_name is the name of the function you want to call and argument_list is the list of input parameters that the function expects.

For example, if you have a function called add that takes two integers as input parameters and returns their sum, you can call it as follows:

int result = add(2, 3);

In this example, the add function is called with input parameters 2 and 3. The returned value, which is the sum of the two input parameters, is assigned to the variable result.

Note that the keyword short cannot be used as a substitute for any of the parts of the function calling syntax in C. The keyword short is used to specify a short integer data type, and it has no role in calling functions.

The form of C function:-

The syntax for declaring a function in C with the short keyword is the same as declaring any other function. The short keyword is used to specify a short integer data type for the function parameters or return type.

The general syntax for declaring a function in C with the short keyword is:

short return_type function_name(short parameter1, short parameter2, ...);

Here, return_type is the data type of the value that the function returns, function_name is the name of the function, and parameter1, parameter2, and so on are the input parameters that the function accepts, each specified with the short keyword.

For example, a function that takes two short integers as input parameters and returns their sum can be declared as follows:

short add(short num1, short num2);

In this example, the function name is add, the return type is short, and the function accepts two short parameters num1 and num2.

It is important to note that using short data types for function parameters and return types can limit the range of values that the function can handle, as the short data type has a smaller range of values compared to int or long.

Return values and their type :-

In C language, a function can return a value of any data type, including short, int, long, float, double, and others. The return type of a function is declared at the beginning of the function declaration.

The syntax for declaring a function with a return value in C using the short keyword is:

short function_name(short parameter1, short parameter2, ...) {
    // Function body
    return short_value;
}

Here, function_name is the name of the function, parameter1, parameter2, and so on are the input parameters that the function accepts, each specified with the short keyword. The function body contains the code that performs the desired operations, and the return statement returns a value of the short data type.

For example, a function that takes two short integers as input parameters and returns their sum can be defined as follows:

short add(short num1, short num2) {
    short sum = num1 + num2;
    return sum;
}

In this example, the add function takes two short integers as input parameters, calculates their sum, and returns the result as a short value.

It is important to note that the data type of the return value should match the return type declared in the function declaration. If the function is declared to return a short value, then the return statement should return a value of the short data type.

No arguments  :-

In C language, a function can be declared without any input parameters. The syntax for declaring a function with no input parameters is:

return_type function_name(void);

Here, return_type is the data type of the value that the function returns, function_name is the name of the function, and void indicates that the function does not accept any input parameters.

For example, a function that returns a fixed short value can be declared as follows:

short get_value(void);

In this example, the function name is get_value, the return type is short, and the function does not accept any input parameters.

To define the function and specify its behavior, you can use the following syntax:

short get_value(void) {
    return 10;
}

In this example, the get_value function returns a fixed short value of 10.

When calling a function with no input parameters, you need to pass an empty argument list, like this:

short result = get_value();

In this example, the get_value function is called with an empty argument list, and the returned value is assigned to the variable result.

no return value:-

In C language, a function can be declared to not return any value. Such functions are commonly called "void" functions. The syntax for declaring a "void" function in C with the short keyword is:

void function_name(short parameter1, short parameter2, ...);

             Here, function_name is the name of the function, parameter1, parameter2, and so on are the input parameters that the function accepts, each specified with the short keyword. The return type is specified as void, indicating that the function does not return any value.

For example, a function that takes two short integers as input parameters and prints their sum can be declared as follows:

void print_sum(short num1, short num2);

In this example, the function name is print_sum, and the function accepts two short parameters num1 and num2.

To define the function and specify its behavior, you can use the following syntax:

void print_sum(short num1, short num2) {
    short sum = num1 + num2;
    printf("The sum is: %hd\n", sum);
}

In this example, the print_sum function takes two short integers as input parameters, calculates their sum, and prints the result to the console using the printf function.

When calling a "void" function, you simply use the function name followed by the input parameters, like this:

print_sum(2, 3);

In this example, the print_sum function is called with input parameters 2 and 3. The function calculates their sum and prints the result to the console. Since the function does not return any value, there is no need to assign the result to a variable.

arguments but no return :-

In C language, a function can accept input parameters and perform a computation or some other action without returning any value. The syntax for declaring a function that accepts input parameters but does not return any value in C using the short keyword is:

void function_name(short parameter1, short parameter2, ...) {
    // Function body
}

Here, function_name is the name of the function, parameter1, parameter2, and so on are the input parameters that the function accepts, each specified with the short keyword. The return type is specified as void, indicating that the function does not return any value.

For example, a function that takes two short integers as input parameters and prints their product can be declared as follows:

void print_product(short num1, short num2);

In this example, the function name is print_product, and the function accepts two short parameters num1 and num2.

To define the function and specify its behavior, you can use the following syntax:

void print_product(short num1, short num2) {
    short product = num1 * num2;
    printf("The product is: %hd\n", product);
}

In this example, the print_product function takes two short integers as input parameters, calculates their product, and prints the result to the console using the printf function.

When calling a function with input parameters but no return value, you simply use the function name followed by the input parameters, like this:

print_product(2, 3);

In this example, the print_product function is called with input parameters 2 and 3. The function calculates their product and prints the result to the console. Since the function does not return any value, there is no need to assign the result to a variable.

recursion, Nesting of function :-

Recursion and nesting of functions are powerful concepts in C programming that allow you to create complex algorithms and solve problems in elegant ways. Here are the basic concepts using short words:

1- Recursion:

Recursion is a technique in which a function calls itself directly or indirectly to solve a problem. The syntax for a recursive function in C using the short keyword is similar to that of a normal function, but it includes a call to itself within the function body:

short recursive_function(short n) {
    if (n == 0) {
        return 1;
    }
    return n * recursive_function(n - 1);
}

In this example, the recursive_function takes a short integer n as input and recursively calculates its factorial by calling itself with a smaller input until it reaches the base case n == 0. The function then returns the calculated factorial.

2- Nesting of functions:

Nesting of functions is a technique in which a function is defined inside another function. The nested function can only be called from within the enclosing function, and it has access to the local variables of the enclosing function. Here's an example of a nested function in C using the short keyword:

short enclosing_function(short x) {
    short nested_function(short y) {
        return x + y;
    }
    return nested_function(x * 2);
}

In this example, the enclosing_function takes a short integer x as input and defines a nested function nested_function that takes a short integer y as input and returns the sum of x and y. The enclosing function then calls the nested function with x multiplied by 2 as input and returns its result. The nested function can only be called from within the enclosing function and has access to the local variable x of the enclosing function.
 













1 Comments

Previous Post Next Post