• facebook
  • whatsapp
  • telegram

FUNCTIONS - Introduction

14.1. Introduction
ఇప్పటి వరకు మనం scanf(), printf(), sqrt(), pow(), strlen(), strcmp(), strcpy(), మొదలైన readymade functions ను ఉపయోగించి ప్రోగ్రాంలు రాశాం. అంటే ఎవరో ముందుగా రాసిన ఈ function ల కోడ్ ను వినియోగించుకుంటున్నారు. అంటే, వాటి కోడ్ కు re-usability పెరిగింది. ఇవి అందుబాటులో లేనట్లయితే మనం ఈ ప్రోగ్రాంలు రాయాల్సి వచ్చేది. అంటే, వీటి ని ఉపయోగించడం ద్వారా మన ప్రోగ్రాం డెవలప్ మెంట్ సులభతరం అయింది. అంతే కాకుండా ఈ function ల కోడ్ ను వాడిన ప్రతీ ప్రోగ్రాంలో test చేయాల్సిన అవసరం లేదు.


ఈ విధంగా, Functions వల్ల కింది ఉపయోగాలు పొందవచ్చు.
1. ప్రోగ్రాం డెవలప్ మెంట్ సులభతరం అవుతుంది.
2. ప్రోగ్రాం డెవలప్ మెంట్ modular అవుతుంది.
3. ప్రోగ్రాం testing సులభతరం అవుతుంది.
4. Codeను share చేసుకోవచ్ఛు.
5. Code re-usability పెరుగుతుంది
6. ప్రోగ్రాం size తగ్గుతుంది.
7. ప్రోగ్రాం readability పెరుగుతుంది.


ఇప్పుడు, మనం కొత్త function లను రాసే విధానాన్ని ఈ పాఠంలో నేర్చుకుందాం. ప్రతీ function కు ఒక రెస్పాన్సిబిలిటి ఉంటుంది. అంటే అది కొన్ని argument లను తీసుకుని ఒక రకమైన విలువను ఇస్తుంది. (return చేస్తుంది). ఉదాహరణకు, strlen function ఒక string ను argument గా తీసుకొని, దానిలో ఎన్ని characters ఉన్నాయో ఇస్తుంది. ఏదైనా Function రాస్తున్నపుడు, ఈ కింది విషయాలు గుర్తుంచుకోవాలి.

1. Function పేరు unique గా ఉండాలి. అది C లాంగ్వేజి Key-words ఉండకూడదు, మరింకే function పేరులా కూడా ఉండకూడదు.
2. అది ఎన్ని, ఏ రకమైన argument లను తీసుకుంటుంది .
3. ఏ రకమైన విలువ ఇవ్వాలి.
ఒక function definition ఈ విధంగా ఉంటుంది.

return_type function_name( type arg1, type arg2, …., type argn)
     {
     --
     }
(లేదా)
return_type function_name( arg1, arg2,arg3 …..,argn)
     type agr1, arg2;
     type arg3, agrn;
     {
     --
     }

Example 1: రెండు పూర్ణాంకాలు(integers) తీసుకొని వాటిలో maximum return చేసే ఒక function రాద్దాం. ఒక main program దానిని call చెయ్యడం ఎలాగో చూపించడానికి రాశాం.
Solution:1. దీని పేరును Max అని అనుకుందాం (వేరే ఇంకేదైనా కూడా తీసుకోవచ్చు).
2. దీనికి రెండు arguments కావాలి కనుక వాటిని, int a, int b అని అనుకుందాం (వేరే ఇంకేవైనా కూడా తీసుకోవచ్చు).
3. మనం రాయాల్సిన function, ఇచ్చిన రెండు integers లో maximum return చెయ్యాలంటే ఈ function int ను return చేయాలి. ఎందుకంటే, రెండు integersలో maximum integer అవుతుంది.

పై points ను , function రాసి దానిని ఎలా call చెయ్యాలో కూడా ఇచ్చాం.


పైన ఇచ్చిన ప్రోగ్రాం main() నుంచి మనం రాసిన Max() function రెండు సార్లు call చేస్తున్నాం. మొదటిసారి, p, q లను actual arguments గా పంపుతాం.వీటి maximum మన Max() function రిటర్న్ చేస్తే దానిని s లో పెట్టాం. అలాగే, Max() functionను రెండో సారి call చేస్తూ s, r లను పంపించాం. వచ్చిన దానిని s లో స్టోర్ చేసి ప్రింట్ చేసాం. అవసరమైతే ఈ రెండు function call లను s=Max(Max(p,q),r) గా కూడా రాయవచ్చు.

Function definition కాబట్టి Function call తర్వాత ఉంటే, function prototype (function declaration లేదా function signature), function call కన్నా ముందు ఉండేటట్లు చూసుకోవాలి. Function p.

Prototype ఈ కింది విధంగా ఉంటుంది. Function prototype అంటే ఒక విధంగా Function definitionలో మొదటి లైను అనుకోవచ్చు. కానీ దీనిలో చివరలో సెమీకోలన్ (;)వాడాలి.

(Note: Header fileలలో readymade functions కి సంబంధించినా prototypes ఉంటాయి). Function prototype ఇలా ఉంటుంది.)

return_type function_name( type arg1, type arg2, ….,type argn);.

Example 2: ఈ ప్రోగ్రాం, function prototypeను ఎలా వాడాలో చూపిస్తుంది. , ఒక main program దానిని call చేయడం ఎలాగో చూపించడానికి రాశాం.
Solution:
#include
int main()
{
     int p, q, r, s;
     int Max(int xxx, int y); Function Prototype
     printf(“Enter Three Integers\n”);
     scanf(“%d %d %d”, &p, &q, &r);
     s = Max(p, q);
     s = Max(s, r);
     printf(“Max of Three Numbers=%d\n”, s);
     return (0);
     }
     int Max(int a, int b)
      {
     return(a }

అవసరమైతే function definition ను ఒక separate file లో స్టోర్ చేసి దానిని ఏ ప్రోగ్రాంలో కావాలి అంటే అందులో వాడుకోవచ్చు. అలా వాడుకోవాలంటే, మనం ఆ fileను మన ప్రోగ్రాంలో include చేయాలి.

Example 3: ఈ ప్రోగ్రాం, మన function code file “a.c” అనుకుంటుంది. ఒక main program దానిని call చేయడం ఎలాగో చూపించడానికి రాశాం.
Solution:
#include
#include “a.c”
int main()
{
     int p, q, r, s;
     printf(“Enter Three Integers\n”);
     scanf(“%d %d %d”, &p, &q, &r);
     s = Max(Max(p, q), r);
     printf(“%d\n”, s);
     return (0);
}


 

 

 

NB Venkateswarlu
M.Tech(IIT-Kanpur)
Ph.D (BITS),PDF(UK),
Sr.Prof., CSE, AITAM, Tekkali

Posted Date : 03-02-2021 .