Concept of functions

Function एक named, reusable block है जो inputs लेता है, एक काम करता है और result return करता है, ताकि marks program खुद को दोहराना बंद कर दे और main() एक पढ़ने लायक table of contents बन जाए।

10 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

खुद को दोहराने वाला program

अब तक बने marks program को ईमानदारी से देखिए: percentage का formula एक बार marksheet print करते समय आता है, फिर grade ladder के अंदर, और फिर class summary में। 3 copies।

पिछले हफ्ते formula बदला (300 की जगह 400 में से)। आपने 2 copies ठीक कीं और तीसरी भूल गए। अब 2 screens अलग-अलग बताती हैं, और किसी को नहीं पता कौन सी सही है।

दोहराया हुआ code सिर्फ ज़्यादा typing नहीं है; वह दोहराए हुए bugs है। इसका इलाज C का सबसे बड़ा structuring idea है: function।

Theory

Tiffin service

Hostel हर room में खाना नहीं बनाता। एक ही kitchen होती है: कच्चा सामान अंदर जाता है, तैयार tiffin बाहर आता है, और खाने वाले किसी को recipe की अंदरूनी बातें जानने की ज़रूरत नहीं। Function वही kitchen है: inputs अंदर (arguments), अंदर एक काम होता है, एक dish बाहर (return value)। Recipe एक बार सुधार दीजिए, हर meal सुधर जाता है, room-दर-room ठीक करने की ज़रूरत नहीं।

Theory

Function, औपचारिक रूप से

Function एक named block है जो एक काम करता है:

float percentage(int obtained, int total) {

return (obtained * 100.0) / total;

}

Header पढ़िए: return type (float वापस आता है), name, parameter list (क्या देना ज़रूरी है)। इसे इस्तेमाल करना एक call है:

p = percentage(marks, 300);

आप पहले दिन से functions call करते आ रहे हैं: printf, scanf, getch, किसी और की बनाई kitchens। यहाँ तक कि main भी एक function है, वही जिसे OS खुद call करता है।

Practical

एक kitchen, दो बार इस्तेमाल

#include <stdio.h>

float percentage(int obtained, int total) {
    return (obtained * 100.0) / total;
}

int main() {
    printf("Riya : %.2f%%\n", percentage(234, 300));
    printf("Aman : %.2f%%\n", percentage(198, 300));
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Jump का पीछा कीजिए

जब main, percentage(234, 300) तक पहुँचता है, तो step by step क्या होता है? Control कहाँ जाता है, उसके साथ क्या travel करता है, और main फिर से कैसे शुरू होता है? Reveal करने से पहले trace कीजिए।

Show the answer

main बीच line में रुक जाता है। Control, percentage तक jump करता है, values 234 और 300 को obtained और total में ले जाता है। Body 78.0 compute करता है और return उसे वापस भेजता है, control ठीक वहीं आता है जहाँ main रुका था, और लौटा हुआ 78.0 printf के अंदर call की जगह ले लेता है।

C यह याद रखने के लिए कि कहाँ से आगे बढ़ना है, calls का एक stack इस्तेमाल करता है, वही LIFO structure जो आप BCA304 में formally पढ़ेंगे। Foreshadowing, बिलकुल मुफ़्त।

Quiz

Percentage का formula फिर बदलता है (500 में से)। Program के function version में, कितनी जगह edit करनी पड़ेंगी?

  1. एक: percentage() function के अंदर
  2. हर call पर एक: percentage() को call करती हर line
  3. तीन, copy-paste version जितनी ही
  4. शून्य: functions खुद को update कर लेते हैं
Show the answer

एक: percentage() function के अंदर

Callers में कभी formula था ही नहीं, सिर्फ उसकी request थी, इसलिए recipe अपनी अकेली kitchen में बदलती है और हर caller तुरंत सही हो जाता है। यह DRY principle है (Don't Repeat Yourself), और "functions के advantages बताइए" में ठीक यही अपेक्षित है: एक बार ठीक करो, हर जगह सही, साथ में readability, testability, reuse।

Quiz

main() के बारे में कौन सा statement सही है?

  1. main खुद एक function है, वही जिसे operating system call करता है
  2. main एक keyword है, function नहीं
  3. main दूसरे functions को call नहीं कर सकता
  4. एक C program में कई main functions हो सकते हैं
Show the answer

main खुद एक function है, वही जिसे operating system call करता है

main एक सच्चा function है जिसका return type (int) और return value (OS को 0) है: इसका इकलौता विशेषाधिकार यह है कि यही वह तय entry point है जिसे OS invoke करता है। यह दूसरों को खुलकर call करता है, ठीक एक ही main हो सकता है (Unit 1 का नियम), और यह keyword नहीं है, इसीलिए C आपको, नासमझी से ही सही, किसी दूसरे function के अंदर main नाम का variable रखने देता है।

Watch out

Marks कहाँ कटते हैं

Definition बनाम call: function define करना (kitchen लिखना) कुछ भी नहीं चलाता; code तभी execute होता है जब उसे call किया जाए, exams में define-किया-पर-कभी-call-न-हुआ function दिखाकर पूछते हैं कि क्या print होगा (जवाब: उससे कुछ नहीं)। कई-काम वाले functions: "computeAndPrintAndSave" एक design smell है; एक function, एक काम, theory के जवाबों में यही लिखिए। Return गायब: float का वादा करने वाला function हर path पर एक return ज़रूर दे।

Theory

table of contents जैसा main()

Professionally refactor करने पर marks program का main() एक index जैसा पढ़ा जाएगा: readMarks(...); computeResults(...); printMarksheet(...), एक ऐसी कहानी जिसे कोई भी follow कर सके, हर chapter अकेले testable। इस unit का बाकी हिस्सा यही कौशल बनाता है: ready-made function libraries (string और math), अपना खुद का सही तरीके से लिखना, arguments pass करने के दो तरीके, और एक function जो खुद को call करता है।

Summary

Key takeaways

  • Function: named block, एक काम, inputs (parameters), एक return value।
  • Calls, argument values के साथ अंदर jump करती हैं और ठीक वहीं से आगे बढ़ती हैं जहाँ छोड़ा था, call stack के ज़रिए।
  • एक बार ठीक करो, हर जगह सही: दोहराया code दोहराए bugs है (DRY)।
  • printf और scanf functions हैं; main एक function है जिसे OS call करता है।
  • Definition कुछ नहीं चलाती; सिर्फ calls body को execute करती हैं।
  • Exam के लिए फायदे: reuse, readability, testability, teamwork।
  • याद रखने का hook: tiffin kitchen, एक recipe, हर meal सुधरता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Functions and Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concept of functions · Computer Programming and Programming Methodology (CPPM) · Gri-Learn