C-function

Functions:

  • A function is a self contained block of statement that perform a particular task of some kind.
  • A statement or a set of statements forming a block and given a name to call it from other part of the program, to perform a certain task is called a function.

Notes:

  • When a block of statement are to be executed repeatedly in one place then loop structure is used.
  • When a block of statement are to be executed at different part of the program, then function can be used.

Syntax of Defining a function

return_type: Return type can be void or non-void. If it is void the return statement is not needed. If it is non-void it is of any data type such as int, float, double, char, short etc.

  • Any C function by default returns an integer value. main() returns an integer value to the OS
  • If the function returns no value or multiple values then it must has to be declared as void

function_name: It can be anything, however it is advised to have a meaningful name for the functions so that it would be easy to understand the purpose of function just by seeing it’s name.

argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables, will be having two integer argument.

Block of code: Set of C statements, which will be executed whenever a call will be made to the function.

Advantages of using function:

  1. Re-usability of code
  2. Make the task modular and small in size
  3. Easier to debug the error
  4. It makes easier to under stand.

Types of C functions

There are two types of functions in C programming:

  1. Library function/ Predefined function
  2. User defined function

Library function:

Library functions are the in-built function in C programming which are defined in the system library under the header files. these are used for fixed purposes and are not defined by the programmer.

Example.

  • printf(),scanf(),putchar(),getchar(),puts(),gets() are defined under stdio.h header file
  • pow(), sqrt(),abs() etc. are defined under header file math.h
  • exit() is defined under stdlib.h
  • strcmp(),strcpy(), strlen() under string.h

User defined function:

C allows programmer to define their own function according to their requirement. These types of functions are known as user-defined functions.

The functions which are defined by the programmers for specific application is know as user defined function

Example. main(), add(),factorial().

User defined function example:

 Explanation:

  1. Through main() we call the function display(). this means that the control passes to display().
  2. That is the activity of the main() is temporarily suspended till the display function is executing.
  3. When the display function stops the executing, the control is transfered back to the main function.
  4. So  the main() resumes its execution.

C_return_statement_0

 

Function Prototype:

Like a variable a function is declared before being invoked known as function prototype. It has four parts

  1. Function type(return type)
  2. Function name
  3. Parameter List
  4. Semicolon
  • A function without an argument may not have a prototype.
  • The parameter names do not need to be same in prototype declaration and function definition.
  • The type must match with function definition.
  • When the declared type don’t match with the types in the function definition compiler generates an error message.

Argument(Actual Argument): An argument represents value you pass to the procedure when you call the procedure. The calling code supplies the argument when it calls the procedure.

Parameter(Formal Arguments): A parameter represents a value that the procedure expects you to pass when you call it.It is declared in the function definition.

Consider the following Code:

The return statement/expression:

It serves two purposes:

  1. On executing the statement, it immediately transfers the control back to the calling program/function.
  2. It returns the value present in the parenthesis to the calling function/procedure.

Valid return statements:

returns(a); return(a+b); return(25); return(5.9);

return;–> It returns garbages value.

Invalid return statement:

return(x,y); Reason: return statement can not return more than one value at a time.

 Function Communication:

Forward Communication: When the data is transfered from calling function to called function, it is called forward communication. it is possible only due to function parameters(Pass by value or pass by address).

Backward Communication:  When the data transfered from called function to calling function,  that is called backward communication. It is possible only because of function return statement.

 Category of functions:

  1. Return type having argument
  2. Return type having no argument
  3. No return type but having argument
  4. No return type having no argument

Return type having argument:

 

 

 Return type having no argument:

 No return type but having argument:

No return type having No argument:

Function returning more than one value:

Function calling conventions(parameter passing):

Arguments can be passed to the function in two different way;

  1. Call by value/ Pass by value
  2. Call by address/ Pass by address

1. Pass by value: Here the value of each actual argument is copied to the corresponding formal arguments of called function. With this method changes made to the formal argument in the called function have no effect on the value of actual argument of calling function.

Call by value example

2. Pass by address:   Here the address of the variable is passed as the actual argument to the called function which will be received by the formal arguments, so any changes made to the content of these addresses in the called function will be reflected in the calling function.

Call by address example

Function Recursion:

  • Recursion is a process in which function calls itself.
  • It is also called as circular function definition.
  • Nature of a function recursion is always an infinite loop or stack over flow.
  • Recursion is an alternative to iteration or looping.
  • A conditional statement if, switch case or conditional operator is used for termination of recursion.

 Advantages:

  • The main advantage of recursion is that for problems like tree traversal it make the algorithm a little easier or more “elegant”.
  • using recursion we can avoid unnecessary calling of functions.
  • Recursion is used to divide the problem into same problem of subtypes and hence replaces complex nesting code.

Disadvantages:

  • Process of recursion is slower as comparison to looping
  • Recursive implementation use more memory than looping
  • It could potentially crash the stack

Factorial example: