KwickAcademy C · 8 min · free
Functions in C: Call by Value and Call by Reference
A function is a named block of code; call by value passes copies, and call by reference passes addresses so originals change.
Follows the syllabus of: GSEB Std 10 Computer Studies
On screen in this lesson
What is a function?
| A named block of code that does one job |
| Write it once, call it many times |
| main is a function; so are printf and scanf |
| Small functions are easier to read and test |
Parts of a function
| Part | Example | Meaning |
|---|---|---|
| Return type | int | type sent back |
| Name | add | what we call it |
| Parameters | int a, int b | inputs it receives |
| Body | { return a + b; } | the work it does |
Actual and formal parameters
| Name | Where | In our example |
|---|---|---|
| Actual arguments | in the call | 40 and 35 |
| Formal parameters | in the definition | a and b |
Function prototype
| A declaration: tells the compiler the function exists |
| Gives return type, name and parameter types |
| Ends with a semicolon, has no body |
| Example: int add(int, int); |
Why prototypes matter
| The compiler reads the file from top to bottom |
| It checks the number and type of arguments |
| No prototype: modern compilers warn or give an error |
| Header files like stdio.h are full of prototypes |
Call by value, step by step
| Step | main: x, y | swap: a, b |
|---|---|---|
| 1. Call swap(x, y) | 5, 9 | 5, 9 (copies) |
| 2. Swap inside | 5, 9 | 9, 5 |
| 3. Return to main | 5, 9 | destroyed |
Quick answers
Why does swap(x, y) not swap x and y?
It gets copies. The originals in main stay the same.
What is a prototype?
A declaration like int add(int, int); before the function is used.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What are the four parts of a definition?41 sec
Does a prototype have a body?42 sec
Which call changes x and y?43 sec
How many values can return send?37 secThe full lesson, in text
Hello students, welcome to Kwickprep. You write a swap function in C, but the two numbers do not swap. Why? Today we will learn to define, declare and call functions, and to return values. Then we will see call by value and call by reference, step by step, and fix that swap.
A function is a small machine inside your program. It is a named block of code that does one job, like adding two numbers. You write it once and call it whenever you need that job done. You already know some functions, because main, printf and scanf are all functions. Breaking a big program into small functions makes it easier to read, test and fix.
Every function definition has four parts. The return type says what kind of value the function sends back, here an int. The name is what we use to call it, here add. The parameters are the inputs, written in brackets with their types. The body, inside curly braces, holds the statements that do the work.
Here we define the function add above main. Defining means writing its full body. Inside main, we call it by writing add, with forty and thirty five in brackets. Calling means asking the function to run. The value forty goes into a, and thirty five goes into b. The function returns seventy five, and printf prints it.
Two terms appear often in exams. Actual arguments are the values written in the call, here forty and thirty five. Formal parameters are the variables in the definition, here a and b, which receive those values.
What if you want to write main first and the function later? Then you must declare the function with a prototype. A prototype tells the compiler, this function exists, trust me. It gives the return type, the name and the types of the parameters. It ends with a semicolon and has no body. For add, the prototype is int add, bracket int comma int, semicolon.
Look at line two. That is the prototype, placed before main. Now main can call add, even though the body is written at the bottom. The compiler uses the prototype to check that we pass two ints and use the answer as an int. The output is still seventy five.
Why do we need prototypes at all? The compiler reads your file from top to bottom, like you read a book. With a prototype, it can check that every call passes the right number and type of arguments. Without one, modern compilers give an implicit declaration warning or even an error. Fun fact, stdio.h is full of prototypes, which is why we include it before using printf.
Now our swap problem. In call by value, the function receives copies of the values. Here x is five and y is nine. We call swap of x and y. Inside swap, a and b are copies, and they do swap using a temporary variable t. But x and y in main never change. So the output is still five nine.
Let us watch the memory step by step. In step one, the call copies five and nine into new boxes a and b. In step two, swap exchanges a and b, but the boxes x and y in main are untouched. In step three, swap ends, its copies are destroyed, and main still has five and nine.
In call by reference, we send the addresses of the variables instead of their values. The ampersand sign means address of, so ampersand x is the address of x. In swap, int star a means a stores an address, and a is called a pointer. Star a means the value at that address, so changing star a changes x itself. Now the output is nine five.
Watch the memory again. In step one, a and b receive the addresses of x and y, not copies of their values. In step two, star a and star b reach into main's boxes, so x becomes nine and y becomes five. In step three, swap ends, but the change stays in x and y.
Here is the comparison exams love. By value passes a copy of the value, while by reference passes the address. The parameter is a plain int by value, and a pointer to int by reference. The call uses plain names by value, and ampersand before each name by reference. Only call by reference can change the original variables. Strictly speaking, C always copies arguments, and passing an address is how C gives us call by reference.
The return statement sends a value back to the caller and ends the function at once. The function big returns the larger of two marks. If a is greater than b, it returns a, and the last line never runs. Pause and predict. What prints if both marks are seventy two? A is not greater than b, so it returns b, which is also seventy two.
Remember five rules about return. A return statement sends back only one value, and the function stops there. That value should match the return type written before the name. A void function returns nothing, so we write only return with a semicolon, or leave it out. If you need two results, pass pointers, just like swap did. Finally, return zero in main tells the operating system that the program finished successfully.
Let us revise what we learned today. A definition has a return type, a name, parameters and a body. A prototype declares the function before it is used, and ends with a semicolon. Call by value passes copies, so the original variables do not change. Call by reference passes addresses, so the originals can change. And return sends back one value and ends the function. Try writing your own swap both ways, and predict the output first.
Courses that teach this
| Course | Unit |
|---|---|
| GSEB Std 10 Computer Studies | Programming Fundamentals & C Language |
| Programming All levels C | Functions and Storage Classes |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

