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.
  • યાદ રાખવાની યુક્તિ: 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