Function calling lets you run a set of instructions saved in one place. It helps you reuse code and keep your program organized.
Function calling
function_name(arguments);
The function_name is the name of the function you want to run.
arguments are the values you give to the function to work with. If no values are needed, leave empty parentheses ().
printHello that takes no arguments.printHello();
sum with two numbers, 5 and 10.sum(5, 10);
multiply with 3 and 4, and saves the returned value in result.int result = multiply(3, 4);
This program shows two functions: greet prints a message, and add returns the sum of two numbers. In main, we call both functions and print the results.
#include <stdio.h> // Function that prints a greeting void greet() { printf("Hello, friend!\n"); } // Function that adds two numbers and returns the sum int add(int a, int b) { return a + b; } int main() { greet(); // Call greet function int sum = add(7, 8); // Call add function with 7 and 8 printf("Sum is %d\n", sum); return 0; }
Always declare or define a function before calling it, so the compiler knows about it.
Functions can return values or just perform actions without returning anything.
Arguments must match the function's expected types and order.
Function calling runs code saved in a function to reuse and organize your program.
You call a function by writing its name followed by parentheses and any needed values.
Functions help break big tasks into smaller, manageable parts.
Practice
call a function in C?Solution
Step 1: Understand function calling
Calling a function means running the code inside it by using its name followed by parentheses.Step 2: Differentiate from defining or declaring
Defining or declaring a function is writing it, not running it. Calling is executing it.Final Answer:
Execute the code inside the function by using its name and parentheses -> Option AQuick Check:
Function call = run code by name() [OK]
- Confusing calling with defining the function
- Thinking calling means declaring the function prototype
- Believing calling is writing comments
printMessage in C?Solution
Step 1: Recall function call syntax
In C, calling a function requires its name followed by parentheses and a semicolon.Step 2: Check each option
printMessage(); uses correct syntax: function name + parentheses + semicolon. Others are invalid syntax.Final Answer:
printMessage(); -> Option AQuick Check:
Function call syntax = name() [OK]
- Omitting parentheses when calling
- Using keywords like 'call' which don't exist in C
- Missing semicolon at the end
void greet() {
printf("Hello!\n");
}
int main() {
greet();
return 0;
}Solution
Step 1: Understand function greet()
The function greet() prints "Hello!" followed by a newline.Step 2: Check main() function call
Main calls greet(), so the program prints "Hello!" on the screen.Final Answer:
Hello! -> Option BQuick Check:
Calling greet() prints Hello! [OK]
- Thinking function name prints output
- Assuming no output without explicit main print
- Confusing function call with declaration
void sayHi() {
printf("Hi!\n");
}
int main() {
sayHi;
return 0;
}Solution
Step 1: Check function call syntax
In main(), sayHi is written without parentheses, so it is not a function call.Step 2: Correct function call
To call sayHi, it must be written as sayHi(); with parentheses.Final Answer:
Missing parentheses when calling sayHi -> Option DQuick Check:
Function call needs () [OK]
- Omitting parentheses when calling functions
- Confusing function name with variable
- Assuming semicolon alone calls function
multiply to multiply 4 and 5 and print the result?int multiply(int a, int b) {
return a * b;
}
int main() {
// Your code here
return 0;
}Solution
Step 1: Understand multiply function
It returns the product of two integers.Step 2: Call multiply and print result
To show output, call multiply(4, 5) inside printf with %d format.Final Answer:
printf("%d", multiply(4, 5)); -> Option CQuick Check:
Use printf to display function return [OK]
- Calling multiply without printing result
- Using print() instead of printf() in C
- Assigning result but not printing
Ready to prove it?
Pass the test and this pattern moves unknown
