Programming Methodology
Top down/Bottom up approach
• Top down: Starts with high-level design and ends with low-level
implementation.
• Divide the problem P into sub-problems P1, P2, P1.1, etc., called
modules. This procedure is called as modularization.
• The C- programming language uses the top-down approach of solving
a problem in which the flow of control is in the downward direction.
• Some times redundancy of code may exists.
• The individual modules are thoroughly analyzed (debugging).
• Relationship among modules are not required always.
P
P1 P2 P3
P1.1
int main(){
int a, b;
scanf(“%d”%d”, &a, &b);
addition(a,b);
//subtraction(a,b);
//multiplication(a,b);
return 0;
}
void addition(int x, y){
int z;
z=x+y;
printf(“addition of two numbers is %d\n”, z);
}
void subtraction(int x, y){
int z;
z=x+y;
printf(“addition of two numbers is %d\n”, z);
}
• Bottom up: start working from the most basic level of problem solving and
moving up in conjugation of several parts of the solution to achieve required
results.
• Modules and sub-modules are designed and solved individually, these modules
are then integrated together to achieve the desired result.
• Object oriented programming languages like C++, Java, etc., uses this approach.
• Working in exactly opposite direction of top-down approach.
• Redundancy can be omitted.
• Relationship among modules is required for better work-flow.
• Works on the concept of data-hiding and encapsulation.
Function
• Standard library functions: printf, scanf, sqrt, etc…
• User defined function:
• Every C program has at least one function, which is main().
• Block of code that perform a task.
• Division of code can be done in any manner, but logically the division is such
that each function performs a specific task.
• A function declaration tells the compiler about a function's name, return type,
and parameters.
• A function definition provides the actual body of the function.
• Function names should be unique.
• Defining a function as follows:
return_type function_name(parameter list)
{
Body or group of statements;
}
• Parameter list: If a function is required to use arguments, it must declare variables that
accept the values. These variables are called the formal parameters of the function.
• Formal parameters act like local variables inside the function and are created
upon entry into the function and destroyed upon exit.
• Return Type − A function may return a value. The return_type is the data
type of the value the function returns. Some functions may not return a
value. In this case, the return_type is the keyword void.
• Function Name − Actual name of the function.
• Parameters − When a function is called, a value is passed 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.
• Function Body − Collection of statements that define what the function
does.
• Function Declaration: Tells the compiler about a function name and how to call the function.
• Function declaration is required when you define a function in one source file and you call that function
in another file.
• Function prototype: return_type function_name(parameter list);
• Doesn’t contain the body.
• Tell the compiler that the function may be used at a later stage in the program.
• Syntax of a function call:
• Function_name(arguments);
• Parameters – used in functions; Arguments – used in function call. Both are used to exchange the values.
• Parameter names are not important in function declaration only their type is required.
• int largest(int a, int b, int c);
• int largest(int , int , int );
• If the function returns a value, then you can store the returned value.
#include <stdio.h>
int minimum(int x, int y); // function prototype
int main () {
int a, b, c;
scanf(“%d%d”, &a, &b);
c = minimum(a, b); // calling a function to get minimum value
printf( “minimum of %d and %d is : %d\n", a, b, c);
return 0;
}
int minimum(int x, int y) {
int z;
if (x<y)
z=x;
else
z=y;
return z; // function returning minimum
}
#include <stdio.h>
int minimum(int x, int y) {
int z;
if (x<y)
z=x;
else
z=y;
return z; // function returning minimum
}
int main () {
int a, b, c;
scanf(“%d%d”, &a, &b);
c = minimum(a, b); // calling a function to get max value
printf( “minimum of %d and %d is : %d\n", a, b, c);
return 0;
}
• Function call type:
• Call by value: Copies the actual value of an argument into the formal parameter.
Though the changes are made to the parameter inside the function have no effect
on the argument.
• Call by reference: Copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual argument used in the
call. If any changes made to the parameter affect the argument.
• In general, C uses call by value to pass arguments.
• Advantage of using functions:
• Program can be divided in to sub-tasks (functions).
• Codes can be reused in other programs.
• The program will be easier to understand, maintain and debug.
• Keyword:
• Meaning is already defined (or predefined) by the compiler and define the syntax.
• We have 32 key words in C.
• int, double, char, unsigned, if, for, while, switch, break, continue, goto, sizeof, return,
etc.
• Function:
• Define some code/statements/blocks in a program.
• Can be pre-defined or user-defined.
• All predefined functions are keywords in any programming languages, but
not vice-versa.
Types of user-defined functions
• No arguments passed and no return value – Directly calling a function using calling function and
printing the desired output from called function.
• No arguments passed but a return value - Directly calling a function using calling function and
gets the desired values and stores in the variable in the calling function from the called function.
• Argument passed but no return value – Passing values to called function and printing the desired
output in the function called function.
• Argument passed and a return value – passing values to called function and getting the desired
values to the calling function.
• Exercise:
• Computing maximum of 4 numbers using top down approach
• Check whether the given number is odd/even, prime or not, palindrome, factorial.