MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Definition of function

A function is a block of code that you can run from different parts of your program. If you have code you need to use in multiple places, you put it in a function. You can then call the function whenever you need it. When a function is called, the program runs the function's code and then goes back to where it left off.

Example:

#include
// Function declaration void greet(); int main() { printf("Program started.\n"); // Calling the function greet(); printf("Program ended.\n"); return 0; } // Function definition void greet() { printf("Hello, welcome to the program!\n"); }

In this example, the greet function is defined separately. When greet() is called in the main function, it prints a welcome message. After the greet function finishes executing, the program continues with the next statement in main().

Report an issue

Reporting: Definition of function (topic)

Related Posts