KEMBAR78
Programming Methodologies Functions - C Language | PPTX
ICT 1405 Programming Methodologies
8. Functions
Function?
● Also known as procedure or subroutine in other programming languages.
● A function is a group of statements that together perform a task.
● A function can be called many times.
● A function is a block of code that only runs when it is called.
Why do we need function?
● Reduce code redundancy.
○ If functionality is performed at multiple places in software, then rather than
writing the same code, again and again, it can create a function and call it
everywhere.
● Make code modular.
○ It becomes really simple to read and use the code if the code is divided into
functions
● Provide abstraction.
○ Can use library functions without knowing about their internal work.
Advantages of Functions
● Easier to Code
○ A lengthy program can be divided into small functions.
○ A function is written for a specific task.
○ A programmer can focus the attention on a specific problem.
● Easier to Modify
○ If there is an error in the program, it is corrected in the corresponding
function.
● Easier to Maintain & Debug
○ Each function contains independent source code.
○ A change in one part of the code does not affect other parts.
○ In case of an error, the infected function is debugged only.
○ The user does not need to examine the whole program.
● Reusability
○ They can be executed many times, can call a function whenever it needs.
○ It can be executed in different parts of the program to display the line
repeatedly.
● Less Programming Time
○ A program may be made up of many functions, which are written as
independent programs.
○ Different programmers can work on different functions simultaneously,
which saves a lot of time in the long run.
Functions
Library AKA Built-in
Pre-defined in C.
The functions which are declared in
the C header files such as printf(),
scanf() etc.
User-defined AKA Tailor-made
Created by the programmer.
Declaration of a function
● Syntax:
return_type function_name (parameter1, parameter2,...)
{
//code to be executed
}
● return_type : Data type specifier of the data returned by the function.
● function_name : The identifier by which it will be possible to call the
function.
● Parameters :
○ Each parameter consists of a data type specifier followed by an
identifier. (for example: int age)
● Allow passing arguments to the function when it is called.
● Different parameters are separated by commas.
● Function's body contains statement/s. It is a block of statements surrounded
by braces { }.
Functions
Built-in User-defined
No return type - No parameters
With return type - no parameter/s.
With parameter/s - no return type.
With parameter/s and return type.
No Parameter/s - No Return Type
Syntax:
void functionName (){
//function implementation
}
Example:
void addition(){
int total = 0;
int x = 10;
int y = 20;
total = x + y;
printf(“Total is : %d” , total);
}
With Return Type - No Parameter
Syntax:
returnType functionName (){
//function implementation
//return statement
}
Example:
int addition(){
int total = 0;
int x = 10;
int y = 20;
total = x + y;
return total;
}
With Parameter/s - No Return Type
Syntax:
void functionName (dataType parameter1, dataType parameter2){
//function implementation
}
Example:
void addition(int x, int y){
int total = 0;
total = x + y;
printf(“Total is : %d” , total);
}
With Parameter/s and Return Type.
Syntax:
returnType functionName (dataType parameter1, dataType parameter2){
//function implementation
//return statement
}
Example:
int addition(int x, int y){
int total = 0;
total = x + y;
return total;
}
Calling the Function
● Calling or invoking the function locates the function in the memory, furnishing it
with arguments and causing it to execute.
● When a function is called then the control passed to the function where it is
actually defined.
● The actually statements are executed and control passed again to the calling
program.
Syntax:
variable= function_name(argument1, argument1, …, argumentN);
Prototyping a Function
● While writing a program, function cannot be used without specifying its type or
without telling the compiler about it.
● Therefore, before calling a function, it must be either declared or defined.
● Thus, declaring a function before calling a function is called function declaration
or prototype which tells the compiler that at some point of the program we will
use the function of the name specified in the prototype.
Syntax:
returnType functionName(dataType, dataType, … );
OR
returnType functionName(dataType parameter1, dataType parameter2, … );
END

Programming Methodologies Functions - C Language

  • 1.
    ICT 1405 ProgrammingMethodologies 8. Functions
  • 2.
    Function? ● Also knownas procedure or subroutine in other programming languages. ● A function is a group of statements that together perform a task. ● A function can be called many times. ● A function is a block of code that only runs when it is called.
  • 3.
    Why do weneed function? ● Reduce code redundancy. ○ If functionality is performed at multiple places in software, then rather than writing the same code, again and again, it can create a function and call it everywhere. ● Make code modular. ○ It becomes really simple to read and use the code if the code is divided into functions ● Provide abstraction. ○ Can use library functions without knowing about their internal work.
  • 4.
    Advantages of Functions ●Easier to Code ○ A lengthy program can be divided into small functions. ○ A function is written for a specific task. ○ A programmer can focus the attention on a specific problem. ● Easier to Modify ○ If there is an error in the program, it is corrected in the corresponding function.
  • 5.
    ● Easier toMaintain & Debug ○ Each function contains independent source code. ○ A change in one part of the code does not affect other parts. ○ In case of an error, the infected function is debugged only. ○ The user does not need to examine the whole program. ● Reusability ○ They can be executed many times, can call a function whenever it needs. ○ It can be executed in different parts of the program to display the line repeatedly.
  • 6.
    ● Less ProgrammingTime ○ A program may be made up of many functions, which are written as independent programs. ○ Different programmers can work on different functions simultaneously, which saves a lot of time in the long run.
  • 7.
    Functions Library AKA Built-in Pre-definedin C. The functions which are declared in the C header files such as printf(), scanf() etc. User-defined AKA Tailor-made Created by the programmer.
  • 8.
    Declaration of afunction ● Syntax: return_type function_name (parameter1, parameter2,...) { //code to be executed }
  • 9.
    ● return_type :Data type specifier of the data returned by the function. ● function_name : The identifier by which it will be possible to call the function. ● Parameters : ○ Each parameter consists of a data type specifier followed by an identifier. (for example: int age) ● Allow passing arguments to the function when it is called. ● Different parameters are separated by commas. ● Function's body contains statement/s. It is a block of statements surrounded by braces { }.
  • 10.
    Functions Built-in User-defined No returntype - No parameters With return type - no parameter/s. With parameter/s - no return type. With parameter/s and return type.
  • 11.
    No Parameter/s -No Return Type Syntax: void functionName (){ //function implementation } Example: void addition(){ int total = 0; int x = 10; int y = 20; total = x + y; printf(“Total is : %d” , total); }
  • 12.
    With Return Type- No Parameter Syntax: returnType functionName (){ //function implementation //return statement } Example: int addition(){ int total = 0; int x = 10; int y = 20; total = x + y; return total; }
  • 13.
    With Parameter/s -No Return Type Syntax: void functionName (dataType parameter1, dataType parameter2){ //function implementation } Example: void addition(int x, int y){ int total = 0; total = x + y; printf(“Total is : %d” , total); }
  • 14.
    With Parameter/s andReturn Type. Syntax: returnType functionName (dataType parameter1, dataType parameter2){ //function implementation //return statement } Example: int addition(int x, int y){ int total = 0; total = x + y; return total; }
  • 15.
    Calling the Function ●Calling or invoking the function locates the function in the memory, furnishing it with arguments and causing it to execute. ● When a function is called then the control passed to the function where it is actually defined. ● The actually statements are executed and control passed again to the calling program. Syntax: variable= function_name(argument1, argument1, …, argumentN);
  • 16.
    Prototyping a Function ●While writing a program, function cannot be used without specifying its type or without telling the compiler about it. ● Therefore, before calling a function, it must be either declared or defined. ● Thus, declaring a function before calling a function is called function declaration or prototype which tells the compiler that at some point of the program we will use the function of the name specified in the prototype. Syntax: returnType functionName(dataType, dataType, … ); OR returnType functionName(dataType parameter1, dataType parameter2, … );
  • 17.