Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

User Defined Functions

Posted By: Paige Hughes     Category: C Programming     Views: 3704

This article explains about Requirement of User Defined Functions, Multi-Function Program, Function Calling Fundas, Returning values from a function, Handling of Non-Integer Functions, Scope and Lifetime of Variable, Automatic Variables, External Variables.

-  It’s one of the strength of C that functions are easy to define and use
-  Library and user-defined functions
-  printf,scanf, sqrt, cos etc are library functions
-  At the time of programming, user defined functions are needed to be developed
-  A user defined function can be added to lib

Requirement of User Defined Functions

  • Only main can serve the purpose but not without some serious problems
  • Large program becomes difficult to debug, maintain, and test
  • Functional parts can be independently developed and then used collectively
  • These subprograms (functions) are easy to debug, test and maintain
  • There are times when some type of operation is done repetitively in a program
  • For such tasks, if the function is written, it saves time and space
  • It facilitates top-down modular programming style to be used to program
  • The length of source program can be used by calling functions at appropriate places
  • It is easy to locate and isolate a faulty function for further investigation
  • A function may be used by many other programs, so no need for starting from scratch


Multi-Function Program

  • A function is a self-contained code that performs a particular task
  • After defining, it can be treated as a black box which does a specific job upon calling
  • The inner working of a function becomes invisible to the rest of the program
  • Every C program can be (and should be) designed using these black boxes

Function Calling Fundas

  • Any function can call any other function
  • A function can call itself!
  • A called function, in turn, can call another function
  • A function can be called more than once
  • Except the starting point, there are no other predetermined relationships, rules of precedence or  hierarchies among the funcs


Change In Definition

  • The function arguments in classical and modern (or ANSI) way are different
  • Modern compiler supports both the methods
  • Function name(argument list)
  • Argument declaration;
  • { statements; return return-value}
  • Function name(arg list with declration)
  • { statements; return return-value}

Returning values from a function

  • The function can return one item back
  • It can return without anything
  • It can be defined as not returning anything
  • It makes the difference when you call the function
  • No args and no return type functions
  • Functions having args or return type or both 

Handling of Non-Integer Functions

  • Returning anything else than integer is important to be specified
  • The declaration and definition
  • The function prototype
  • Type-specifier Function_Name ( Argument list) { body of a function}
  • Defining before and after main 
  • Nesting function calls and use of stack
  • The stack frame and local variable effects
  • The function calling itself, the recursion
  • The terminating criteria and change of value of terminating variable
  • Passing array to a function only needs to refer to an array name


Scope and Lifetime of Variable

  • Variable’s behavior becomes different when defined under different storage classes
  • The scope is ‘over what part of the program the variable is available’
  • The longevity is ‘the period during which a variable retain its value’
  • The variables can be either local or global depending on their place of definition


Automatic Variables

  • They are created upon call and destroyed upon exit of a function
  • They are private or local to a function
  • They are also called local or internal vars
  • Auto is default
  • Their value can not be changed by other functions accidentally or intentionally
  • They can be defined in a block


External Variables

  • Variables that are alive and active through the program are external or global variables
  • They are declared outside any function
  • They can be accessed by any function 
  • Same name of Local and Global variables
  • They are visible after declaration
  • Initialized to zero by default
  
Share: 

 
 

Didn't find what you were looking for? Find more on User Defined Functions Or get search suggestion and latest updates.

Paige Hughes
Paige Hughes author of User Defined Functions is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!