Concept of functions

Function એ એક જ કામ કરતો નામવાળો block છે, જે ફક્ત call થાય ત્યારે જ ચાલે છે: arguments અંદર, વધુમાં વધુ એક value બહાર.

5 min read · 10 cards · 5 checks

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


Theory

ભુલાયેલી copy

Marks program 3 જગ્યાએ percentage ગણે છે. હવે paper 400 marks નું છે; તમે 2 copies સુધારી લીધી, પણ class summary હજી પણ 300 થી ભાગે છે. Riya ના 312 marks આવ્યા: class summary શું બતાવે છે?

Think first

પહેલાં તમારો અંદાજ

તમારા મતે જે percentage દેખાશે તે type કરો.

Show the answer

104, જે અશક્ય percentage છે; marksheet 78 બતાવે છે. Copies ધીમે ધીમે એકબીજાથી અલગ પડી જાય છે, એટલે formula એક વાર લખો, એને નામ આપો, અને દરેક જગ્યાએ એને call કરો: એ જ function છે.

Theory

એક kitchen, અનેક ભોજન

Function એ એક કામ માટેનો નામવાળો block છે, kitchen જેવો: સામગ્રી arguments તરીકે અંદર જાય છે, અને વધુમાં વધુ એક વાનગી બહાર આવે છે, એટલે કે return value. લખેલી definition ત્યાં સુધી કંઈ રાંધતી નથી જ્યાં સુધી કોઈ order ન આપે, અને એ order એટલે call.

Practical

એક kitchen, 2 calls

#include <stdio.h>

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

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

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

Think first

કૂદકા પર નજર રાખો

percentage(312, 400) ને trace કરો: control ક્યાં જાય છે, અને શું પાછું આવે છે?

Show the answer

main થોભી જાય છે; control percentage ની અંદર કૂદે છે અને arguments 312 અને 400 ને parameters obtained અને total માં લઈ જાય છે. return 78.0 પાછું મોકલે છે, અને main જ્યાં થોભ્યું હતું ત્યાંથી જ ફરી ચાલુ થાય છે. C આ થોભેલી calls નો હિસાબ એક call stack પર રાખે છે, જે BCA304 માં ભણાવાય છે.

Quiz

ધારો કે ઉપરના program ના formula માં typo રહી ગયો છે. કેટલી જગ્યાએ સુધારો કરવો પડે?

  1. 1 જગ્યાએ: percentage() ની અંદરના formula માં
  2. 2 જગ્યાએ: percentage() માં અને main માં
  3. 2 જગ્યાએ: main માં, દરેક call માં એક-એક વાર
  4. 3 જગ્યાએ: definition માં અને main ના બંને calls માં
Show the answer

1 જગ્યાએ: percentage() ની અંદરના formula માં

દરેક call માં ફક્ત order હોય છે, recipe ક્યારેય નહીં: એક વાર સુધારો, દરેક જગ્યાએ સાચું (DRY, Don't Repeat Yourself). Exam માં લખવાના બીજા ફાયદા: reusability, readability (main વાંચવામાં table of contents જેવું લાગે છે), સહેલું testing અને debugging, teamwork.

Think first

તો main શું છે?

printf અને scanf library functions છે, જે તૈયાર મળે છે; percentage() user-defined છે. શું main એક function છે? શું એક program માં 2 main હોઈ શકે?

Show the answer

હા, અને એ keyword નથી: program શરૂ કરવા માટે operating system એને call કરે છે. ના: main બરાબર એક જ હોય; બીજું main લખવું એ compile error છે.

Practical

Exam-style: output કહો

#include <stdio.h>

void printHeader() {
    printf("MARKSHEET ");
}

int addGrace(int marks) {
    printf("grace ");
    return marks + 5;
}

int main() {
    int riya = 30;

    printf("start ");
    riya = addGrace(riya);
    printf("%d", riya);
    return 0;
}

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

Quiz

Exam-style: ઉપરના program નું output શું છે?

  1. start grace 35
  2. MARKSHEET start grace 35
  3. MARKSHEET grace start 35
  4. start grace 30
Show the answer

start grace 35

Program દર વખતે main થી જ શરૂ થાય છે, એટલે start સૌથી પહેલાં આવે છે; addGrace grace print કરે છે અને 35 return કરે છે, જે પાછું riya માં જ store થાય છે. printHeader define કરેલું છે પણ ક્યારેય call થતું નથી, એટલે MARKSHEET ક્યારેય print થતું નથી.

Summary

Key takeaways

  • Function: એક જ કામ કરતો નામવાળો block, arguments અંદર, વધુમાં વધુ એક value બહાર.
  • Definition કંઈ ચલાવતી નથી; call, return થાય ત્યાં સુધી caller ને થોભાવી રાખે છે.
  • ફાયદા: reusability, readability, સહેલું testing, teamwork, એક વાર સુધારો (DRY).
  • યાદ રાખવાની યુક્તિ: એક kitchen, અનેક ભોજન; order નહીં, તો રસોઈ નહીં.

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