Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

7.1 Function Basics

1 min read
Image result for Function Basics C+
A function is a group of statements that together perform a task. All C programs made up of one or more functions. There must be one and only one main function.
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task.

A program will be executing statements sequentially inside one function when it encounters a function call.
A function call is an expression that tells the CPU to interrupt the current function and execute another function.
The CPU 'puts a bookmark' at the current point of execution, and then calls (executes) the function named in the function call. When the called function terminates, the CPU goes back to the point it bookmarked, and resumes execution.

Function definition

return_type function_name( parameter list )
{
//statements
}

- The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value.

- function name is the identifier by which the function can be called.

- A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

- statements is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.

Example
/* Function returning addition of 2 integers */
int add(int a, int b)
{
int total ;
total= a+b;
return total;
}

                      7.2 Declaration, call & argument          NEXT >>

You may like these posts

  •   Constructor A constructor is a special kind of class member function that is executed when an object of that class is instantiated. Constructors are typically used to init…
  •   Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C++, this takes the form of a function that calls its…
  • A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for f…
  • Inheritance is one of the key feature of object-oriented programming including C++ which allows user to create a new class(derived class) from a existing class(base class). The…
  • You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of argu…
  • Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members. An object is an instanti…

Post a Comment