Skip to main content

Posts

Showing posts with the label functions

Recursive Function in C programming

             If a statement within the body of a function calls the same function, the function is called recursive function. Actually, recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. This process is used for repetitive computations in which each action is stated in term of previous result. Many iterative or repetitive problems can be written in this form.                To solve a problem using recursive method, two conditions must be satisfied. They are: 1)       Problem could be written or defined in term of its previous result. 2)       Problem statement must include a stopping condition. /*   An example of recursive function to calculate factorial of a number.*/    #include<stdio.h>    #include<conio.h...

Accessing a function in C programming

                  A function can be called or accessed by specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas. For example, add(a,b) to call a function to add two numbers. If function call doesn’t require any arguments, any empty pair of parentheses must follow the name of function. The arguments appearing in the function call are referred as actual arguments. In function call, there will be one argument for each formal argument. The value of each argument is transferred into the function and assigned to the corresponding formal argument. An example /* to find the greatest number among three numbers.*/        #include <stdio.h>        #include<conio.h>          int greater(int x, int y)    /* function definition*/       ...

Return Statement in C programming

                It is the statement that is executed just before the function completes its job and control is transferred back to the calling function. The return statement serves mainly two purposes. They are: 1)       It immediately transfers the control back to the calling program after executing the return statement. 2)       It returns the value present in the parentheses to the calling function.          The syntax for return is :                      return expression;      Here, the value of expression is returned to the calling portion of the program. The expression is optional. If the expression is omitted, the return statement simply causes the control to revert back to the calling portion of the program without any ...

Function definition in C programming

A function definition is a group of statements that is executed when it is called from some point of the program. The general syntax is return_type function_name (parameter1, parameter2, ....., parametern) { - - - - - statements ; - - - - - } Where, ô€‚ƒ return_type is the data type specifier of data returned by the function. ô€‚ƒ function_name is the identifier by which it will be possible to call the function. ô€‚ƒ Parameters (as many as needed) : Each parameter consists of a data type specifier followed by an identifier like any regular variable declaration. (for eg: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas. ô€‚ƒ Statements is the function’s body. It is a block of statements surrounded by braces{}. ô€‚ƒ The first line of the function definition is k/a function header. #include<stdio.h> int addition (int a, in...

Functions in C programming

     A function is defined as a self contained block of statements that performs a particular task. This is a logical unit composed of a number of statements grouped into a single unit. It can also be defined as a section of a program performing a specific task. [Every C program can be though of as a collection of these functions.] Each program has one or more functions. The function main(0 is always present in each program which is executed first and other functions are optional.  Advantages of using Functions: The advantages of using functions are as follows: 1. Generally a difficult problem is divided into sub problems and then solved. This divide and conquer technique is implemented in C through functions. A program can be divided into functions, each of which performs some specific task. So, the use of C functions modularizes and divides the work of a program. 2. When some specific code is to be used more than once and at different places in the ...

Functions in C++

A function groups a number of program statements into a unit and gives it a name. This unit can then be invoked from other parts of the program. C++ has added many new features to functions to make them more reliable and flexible. C++ function can be overloaded to make it perform different tasks depending on the argument passed to it. Standard C and C++ use a feature called function prototyping . With function prototyping, you must use a description of the types of arguments when declaring and defining a function. This description is the “prototype.” When the function is called, the compiler uses the prototype to ensure that the proper arguments are passed in and that the return value is treated correctly. If the programmer makes a mistake when calling the function, the compiler catches the mistake. In a function prototype, the argument list contains the types of arguments that must be passed to the function and (optionally for the declaration) identifiers for the arguments....