0% found this document useful (0 votes)
2 views

4_Functions

The document discusses modular programming, emphasizing the importance of breaking lengthy programs into smaller, manageable components to reduce errors and enhance debugging. It outlines the advantages of modularization, such as reusability and easier debugging, and explains the structure and definition of functions, including standard and user-defined functions. Additionally, it provides examples of function definitions and calls, as well as homework assignments related to implementing functions for various tasks.

Uploaded by

batmanflyinsky
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

4_Functions

The document discusses modular programming, emphasizing the importance of breaking lengthy programs into smaller, manageable components to reduce errors and enhance debugging. It outlines the advantages of modularization, such as reusability and easier debugging, and explains the structure and definition of functions, including standard and user-defined functions. Additionally, it provides examples of function definitions and calls, as well as homework assignments related to implementing functions for various tasks.

Uploaded by

batmanflyinsky
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Modular Programming

Lengthier programs
– Prone to errors
– tedious to locate and correct the errors

To overcome this
Programs broken into a number of smaller
logical components, each of which serves a
specific task.

04/21/25
1
Modularization

Process of splitting the lengthier and


complex programs into a number of
smaller units is called Modularization.

Programming with such an approach is


called Modular programming

2
04/21/25
Advantages of modularization

• Reusability

• Debugging is easier

• Build library

04/21/25
3
Functions

A function is a set of instructions to


carryout a particular task.

Using functions we can structure our


programs in a more modular way.
Functions

Standard functions
(library functions or built in functions)

User-defined functions
Written by the user(programmer)

04/21/25
5
Defining a Function

 Name
– You should give functions descriptive names
– Same rules as variable names, generally
 Return type
– Data type of the value returned to the part of
the program that activated (called) the
function.

04/21/25 6
Functions

Parameter list
– A list of variables that hold the values being
passed to the function
Body
– Statements enclosed in curly braces that
perform the function’s operations(tasks)

04/21/25 7
The general form of a function definition

return_type
function_name(parameter_definition)
{
variable declaration;
statement1;
statement2;
.
.
.
return(value_computed);
}
04/21/25
8
Functions: understanding

Return type Function


name Parameter
List

void main (void)

}
{
cout << “hello world\ Body
n”;

}
04/21/25
9
Functions
Return type Function Parameter
name List
void DisplayMessage(void)

}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 10
Functions
Return type Function Parameter
name List
void DisplayMessage(void)

}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 11
Functions
Return type Function Parameter
name List
void DisplayMessage(void)

}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}

04/21/25 12
Return type Function Parameter
name List
void DisplayMessage(void)
{ cout << “Hello from the function”

}
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 13
Return type Function Parameter
name List
void DisplayMessage(void)

}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 14
Return type Function Parameter
name List
void DisplayMessage(void)
{ cout << “Hello from the function”

}
<< “DisplayMessage.\n”; Body }
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;}

04/21/25 15
Return type Function Parameter
name List
void DisplayMessage(void)

}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 16
Return type Function Parameter
name List
void DisplayMessage(void)

}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 17
// FUNCTION DEFINITION
void DisplayMessage(void)
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
}
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 18
void DisplayMessage(void); //fn declaration
void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
// FUNCTION DEFINITION
void DisplayMessage(void)
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
04/21/25
} 19
void First (void)
{ cout << “I am now inside function First\n”;}
void Second (void)
{ cout << “I am now inside function Second\n”;
} // book has now as not
void main ()
{ cout << “I am starting in function main\n”;
First ();
Second ();
cout << “Back in function main again.\n”;
04/21/25 } 20
Function- definition & call
Return type Function Parameter
name List
void DisplayMessage(void)

}
{ cout << “Hello from the function”
<< “DisplayMessage.\n”;
} Body

void main()
{ cout << “Hello from main”;
DisplayMessage(); // FUNCTION CALL
cout << “Back in function main again.\n”;
}
04/21/25 21
Arguments ( parameters)
 Both arguments ( parameters) are variables used in a
program & function.
 Variables used in the function reference or function
call are called as arguments. These are written
within the parenthesis followed by the name of the
function. They are also called actual parameters.
 Variables used in function definition are called
parameters, They are also referred to as formal
parameters.

04/21/25 22
Home Work:
To be solved …Functions
Write appropriate functions to
1. Find the factorial of a number ‘n’.
2. Reverse a number ‘n’.
3. Check whether the number ‘n’ is a palindrome.
4. Generate the Fibonacci series for given limit ‘n’.
5. Check whether the number ‘n’ is prime.
6. Generate the prime series using the function
written for prime check, for a given limit.

04/21/25 23
To be solved … Functions
Factorial of a given number ‘n’
long factFn(int); long factFn(int num) //factorial
//prototype calculation
{
void main() { int i, fact=1;
long n, f;
cout<<"Enter the for (i=1; i<=num; i++)
number to evaluate its fact=fact * i;
factorial:";
cin>>n; return (fact); //returning the
f =factFn(n); //function factorial
call }
cout<<"\nFact of
"<<n<<" is "<< f;
04/21/25 24
}
To be solved …Functions

Try to convert all (non function)


programs into functions…

04/21/25 28
Function- definition & call
void dispChar(int n, char c);//proto-type
l r sdeclaration
a te void dispChar(int n, char c) // function
r m e
m
F o ra definition
p a
{
cout<<" You have entered "<< n<<
“&” <<c;
void } main(){ //calling program
int no; char ch;
cout<<"\nEnter a number & a character: \
n"; Actual
cin>>no>>ch; parameters
dispChar( no, ch); //Function
04/21/25 reference 29

}
Functions- points to note
1. The parameter list must be separated by commas.
dispChar( int n, char c);
2. The parameter names do not need to be the same in the
prototype declaration and the function definition.
3. The types must match the types of parameters in the function
definition, in number and order.
void dispChar ( int n, char c);//proto-type
void dispChar ( int num, char letter) // function
definition
{ cout<<" You have entered "<< n<< “&”
<<c; }
4. Use of parameter names in the declaration is optional.
void dispChar ( int , char);//proto-type
04/21/25 30
Functions- points to note

4. If the function has no formal parameters, the list is written as


(void).
5. The return type must be void if no value is returned.
6. When the declared types do not match with the types in the
function definition, compiler will produce error.

04/21/25 31
Functions- Categories

1. Functions with no arguments and no return

values.

2. Functions with arguments and no return values.

3. Functions with arguments and one return value.

4. Functions with no arguments but return a value.

5. Functions that return multiple values .


04/21/25 32
Fn with No Arguments/parameters
& No return values
void dispPattern(void )
{ int i;
for (i=1;i<=20 ; i++)
cout << “*”;}

void dispPattern(void); // prototype


void main()
{ cout << “\nfn to display a line of stars\n”;
dispPattern();
}

04/21/25 33
Fn with Arguments/parameters &
No return values
void dispPattern(char c )
{ int i;
for (i=1;i<=20 ; i++)
cout << c;
cout<<“\n”;}

void dispPattern(char ch); // prototype


void main()
{ cout << “\nfn to display a line of patterns\n”;
dispPattern(‘#’);
dispPattern(‘*’);
dispPattern(‘@’); }
04/21/25 34
Fn with Arguments/parameters &
One return value
int fnAdd(int x, int y )
{ int z;
z=x+y
return(z);}
int fnAdd(int a, int b); // prototype
void main()
{ int a,b,c;
cout << “\nEnter numbers to be added\n”;
cin>>a>>b;
c=fnAdd(a,b);
cout<<“Sum is “<< c;}
04/21/25 35
Fn with No Arguments but
A return value
int readNum()
{ int z;
cin>>z;
return(z); }

int readNum(void); // prototype


void main(){
int c;
cout << “\nEnter a number \n”;
c=readNum();
cout<<“\nThe number read is “<<c;
}
04/21/25 36
Passing 2D-Array to Function

Rules to pass a 2D- array to a function


 The function must be called by passing only the array name.
 In the function definition, we must indicate that the array has
two-dimensions by including two set of brackets.
 The size of the second dimension must be specified.
 The prototype declaration should be similar to function
header.

04/21/25 37

You might also like