📞 C Calling Functions – How to Call a Function in C
In C programming, a function call means executing a function that’s been defined elsewhere in your code. You “call” a function by using its name followed by parentheses, and passing arguments (if any).
🔹 Function Call Basics
When a function is called, the control jumps to its definition, executes the code, and then returns back to the point where it was called.
📝 Example 1: Calling a Simple Function
Here we call the greet() function from inside the main() function.
#include <stdio.h>
// Function declaration
void greet();
int main() {
greet(); // Function call
return 0;
}
// Function definition
void greet() {
printf("Hello, C Learner!\n");
}
📝 Example 2: Calling a Function with Parameters
This function takes an argument and uses it in its output.
#include <stdio.h>
// Function declaration
void greetUser(char name[]);
int main() {
greetUser("Alice"); // Function call with argument
return 0;
}
// Function definition
void greetUser(char name[]) {
printf("Hello, %s!\n", name);
}
📝 Example 3: Calling a Function That Returns a Value
This function returns an integer result back to the calling function.
#include <stdio.h>
// Function declaration
int square(int num);
int main() {
int result = square(5); // Function call with value returned
printf("Square: %d\n", result);
return 0;
}
// Function definition
int square(int num) {
return num * num;
}
💡 Tips
- Always declare the function before calling it (unless it’s already defined above the call).
- Match the number and type of arguments when calling a function.
- You can reuse a function as many times as needed by calling it multiple times!
🎯 Summary
- Use function calls to execute reusable blocks of code.
- Functions can take arguments and return values.
- They improve readability, maintainability, and structure of your code.
🚀 Ready to Explore?
Try modifying the function calls to pass different values and experiment with the results. Functions are like magic spells—call them right and they do the work!