🔹 Functions in C
A function is a code segment or sub-program defined separately with an identifier, so it can be reused whenever needed.
✨ Advantages of Functions
- Reusability – Write once, use multiple times.
- Modularity – Break complex problems into smaller parts.
- Readability – Code becomes easier to understand and maintain.
📌 Two Forms of Functions
- Definition – Specifies the code to be executed when invoked.
- Invocation (Calling) – Requests the function to run its definition.
🔗 Relationship Between Definition & Invocation
- No function can be invoked unless it is defined.
- A defined function cannot run unless it is invoked.
- Functions can be invoked inside other functions.
- Invocation completely depends on definition.
💻 Example 1: Simple Function
void test() {
printf("\nHi from Test");
}
void main() {
printf("Start of Main");
test(); // invocation
printf("\nEnd of main");
}
Illustration: Function Definition & Invocation
👥 Caller vs Called Function
- Caller – The function that calls another function.
- Called – The function being invoked.
💻 Example 2: Nested Function Calls
#includevoid test2() { printf("\nHi of test2"); } void test1() { printf("\nStart of test1"); test2(); // test1 calls test2 printf("\nEnd of test1"); } void main() { printf("Start of main"); test1(); // main calls test1 printf("\nEnd of main"); }
Illustration: Caller and Called Functions
🔹 Types of Functions in C
- Pre-defined / Built-in / Library Functions – Already available in C libraries.
Examples:printf(),scanf(),gets(),strcmp() - User-defined Functions – Created by the programmer to perform specific tasks.
📘 Structure of a Function Definition
A function definition contains two parts:
- Header – Identifies the function.
- Body – Contains executable statements.
🧩 Header Components
- Datatype of return value
- Name of the function
- Parentheses with or without arguments
Function Header Structure
🔁 Return Value
The return value is sent back to the calling function after execution. It can belong to any basic or user-defined datatype.
Syntax of Return Statement
return; return val; return var; return expr;
Example: Function Returning a Value
int RetVal() {
return 5;
}
void main() {
int x = RetVal();
printf("x=%d", x);
}
Example: Function Returning Expression
int RetExp() {
return 5 + 7;
}
void main() {
int x = RetExp();
printf("x=%d", x);
}
Example: Function Returning Variable
int RetVar() {
int a = 10;
return a;
}
void main() {
int x = RetVar();
printf("x=%d", x);
}
🚫 Functions Without Return Value
Use the keyword void when a function does not return any value. The return; statement may be used to indicate the end of the function.
Example: Function Returning Nothing
void NoRet() {
printf("\nHello from NoRet");
return;
}
void main() {
NoRet();
return;
}
Return Flow Diagram
🧠 Example: printf() Returning a Value
void main() {
int x;
x = printf("Best Computer Institute");
printf("\nx=%d", x);
return;
}
📝 Practice Questions
- Define a function
getNumber()that returns 100. Display it inmain(). - Define a function
getCharacter()that returns 'A'. Display it inmain(). - Define a function
getPI()that returns 3.1415 as float. Print it inmain(). - Define a function
getMessage()that returns "Hello, C!". Display it inmain(). - Define a function
calculateSum()that returns sum of 10 and 20. Print it inmain(). - Define a function
isEven()that returns 1 if 10 is even, else 0. Display result inmain(). - Define a function
getMax()that returns larger of 25 and 40. Display result inmain(). - Define a function
getSquare()that returns square of 5. Display result inmain(). - Define a function
calculateFactorial()that returns factorial of 5. Display result inmain(). - Define a function
reverseNumber()that returns reverse of 123 (→ 321). Display result inmain().
🔹 Parameters or Arguments
Parameters (or arguments) are values passed by the caller function at the time of invocation.
Example: Function Accepting No Arguments
void NoArgs(void) {
printf("I won't accept arguments");
return;
}
void withArgs(int x) {
printf("\nx=%d", x);
return;
}
void main() {
NoArgs();
withArgs(5);
return;
}
🧩 Types of Parameters
- Formal Parameters – Appear within parentheses inside the function header. Must be variables.
- Actual Parameters – Appear inside the function invocation. Can be numbers or variables.
Example: Formal vs Actual Parameters
void test(int x, float y) { // x, y are formal parameters
printf("x=%d", x);
printf("\ny=%f", y);
return;
}
void main() {
test(23, 5.46); // 23, 5.46 are actual parameters
return;
}
Illustration: Formal vs Actual Parameters
🔢 Types of Function Definitions
There are four types of function definitions based on arguments and return values:
- No arguments, no return value
- No arguments, with return value
- With arguments, no return value
- With arguments, with return value
Example 1: No Arguments, No Return Value
void add(void) {
int x, y;
printf("Enter any two numbers:");
scanf("%d%d", &x, &y);
printf("sum = %d", x + y);
return;
}
void main() {
add();
return;
}
Example 2: No Arguments, With Return Value
int add(void) {
int x, y;
printf("Enter any two numbers:");
scanf("%d%d", &x, &y);
return x + y;
}
void main() {
int res = add();
printf("Sum=%d", res);
return;
}
Example 3: With Arguments, No Return Value
void add(int a, int b) {
printf("Sum=%d", a + b);
return;
}
void main() {
int x, y;
printf("Enter any two numbers:");
scanf("%d%d", &x, &y);
add(x, y);
return;
}
Example 4: With Arguments, With Return Value
int add(int a, int b) {
return a + b;
}
void main() {
int x, y;
printf("Enter any two numbers:");
scanf("%d%d", &x, &y);
int res = add(x, y);
printf("Sum=%d", res);
return;
}
Function Type Matrix
📝 Practice Questions
- Write a function without arguments and without return value that prints "Hello, World!".
- Write a function with arguments and no return value that takes two integers and prints their sum.
- Write a function without arguments but with return value that returns the number 100.
- Write a function with arguments and return value that takes two numbers and returns their product.
- Calculate the square of a number using functions:
- (a) Without arguments and without return value
- (b) With arguments and with return value
- Define a function that takes a character and prints whether it is a vowel or not.
- Create a function that takes radius and returns area of circle.
- Write a function to find maximum of three numbers using arguments and return value.
- Define
void input()to accept integer andint double_it(int)to return its double. - Create a menu-driven program for arithmetic operations: Add, Subtract, Multiply, Divide.
- Explain argument passing in C (by value vs by reference) with example.
- Write a function to reverse a number using arguments and return result.
- Define and use a function to check if a number is prime and print all primes between 1–100.