0
0
Cprogramming~5 mins

Function calling

Choose your learning style9 modes available
Introduction

Function calling lets you run a set of instructions saved in one place. It helps you reuse code and keep your program organized.

When you want to repeat a task many times without writing the same code again.
When you want to break a big problem into smaller, easier parts.
When you want to test or fix one part of your program without changing everything.
When you want to share code between different parts of your program.
Syntax
C
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 ().

Examples
Calls a function named printHello that takes no arguments.
C
printHello();
Calls a function named sum with two numbers, 5 and 10.
C
sum(5, 10);
Calls multiply with 3 and 4, and saves the returned value in result.
C
int result = multiply(3, 4);
Sample Program

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.

C
#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;
}
OutputSuccess
Important Notes

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.

Summary

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.