Tuesday, October 12, 2010

Software Engg: Introduction to Progamming: Functions in C Language

Lecture No : 3
Topic : Functions in C-Language


In C, Structure Chart Modules are represented by Functions. Functions are like mini programs; small units of code designed to perform a single task. Functions can work independently, or they can share information with the calling module.

Functions Basics
   
When learning Function Basics, remember that most important things regarding functions come in 3s. A function can appear in a program in three ways, as a Declaration , Call or Definition. A function has three parts, the Return Type, Identifier (or name) and Parameter List. The Body of a function is represented by a code block (not optional) and has local declarations, expressions and a return type.




The Main Function

The main Function is the only function which does not need to be declared or called, because every C program must have a one and main is called by the operating system. The main Function does have a return type and a parameter list. When no information is sent to a function or returned from a function, the type is said to be void. It is a C data type meaning nothing..

Function Deceleration

Like a variable, a function must be declared before it can be used (called) in a program. A function declaration is called a prototype, and contains the function return type, name and parameter list but no code block. 

Example:  void ShiekhFun(void)

Because function prototypes are declarations of a global scope, they must appear OUTSIDE of main.
  
The Function Calling


A call to a function is made whenever we want to use the code in the function. A function call is a compound expression statement, and resolves to the value of the functions return type. A call is made by applying the call unary operator “( )” to a function name. 

Example: ShiekhFun( ); 

which executes shiekhFun’s code and resolves to an integer.
Example No: 1
Example No:2 with Output Results




No comments:

Post a Comment