Functions & Parameters in C Programming

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Standard Library Functions in C Programming

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Functions in C Programming
  • 0:33 Types of Functions
  • 1:18 Components of a Function
  • 2:36 How a Function Is Called
  • 4:30 Call by Value/Reference
  • 4:55 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Meghalee Goswami
This article explains what a function is in C programming language and how it can be used. It also explains the variables associated in declaring and naming functions. We also see example of functions that return a value.

Functions in C Programming

A function exists in almost all programming languages. As the word suggests, a function is a group of statements clubbed together to perform a particular task. Each function is accompanied by a set of parenthesis, the opening bracket ( and the closing bracket ). There may be a group of variable declarations called parameters inside these parenthesis. We will come to the detailed explanation of this as we move forward with the lesson. Please note that a function can also be termed as a subroutine or a procedure.

Types of Functions

Functions can be broadly categorized into two major types: user defined functions and built-in functions.

You may have used some built-in functions in C without realizing that it's a function. For example, the strcat() is a built-in function provided by C standard library, which allows you to concatenate two strings. User defined functions are functions defined by the user or the person writing the program. If you recall, the main() is also a function but is often coined as neither a user-defined function, not a built-in function or in some way behaves as both.

General Form of a Function

Here's a general form of a function:

return_type function_name( parameter list ) {
/*the body of the function*/
}

For example, the figure here is a simple example of a function with all its major components pointed out.

Function Explanation
Function Explanation

Let's go through these, one by one.

Components of a Function

Return-type

This specifies the data type of the value being returned by the function. A function may or may not return a value. If the function does not return a value, then the return type is void. In this case, the return value is an integer value c, which means the return type would be int.

Parameter list

The list of formal parameters being passed onto the function. In this case, there are two parameters of type int passed to the function.

Local variables or local declarations

The variables that are declared inside the function are called local variables. The scope of these variables lies within the function and they are not accessible outside the function.

Function body

Comprised of everything inside the curly brackets { and } following the return type, function name and the parameter list.

Function name

The name of the function can be anything that you want. The standard is to make it something related to what it's supposed to do. The naming convention follows the same rule as that of variable naming convention in C.

Function declaration

Tells the compiler all about the function. These include the function's name, the return type, and the number and types of parameters. The body of the function having the function definition can be defined somewhere else. A function declaration has the following parts:

return_type function_name( parameter list );

The example here can be declared as follows:

int sum(int num1, int num2);

or

int sum(int, int);

How a Function Is Called

You must be thinking, 'Why do we need to call a function when we have already written the code somewhere in our program?' A function cannot be used unless it's called from somewhere. When a function is called, the program control is transferred to the function. It processes the function's parameter list and then executes all the function body till the function's closing bracket. After reaching the end bracket the control goes back to the main program or more clearly the line after the function was called. The general syntax of calling a function if it returns some value, is as follows:

return-type result-storage-variable-name = name-of-the-function(parameter list);

In the example, the function sum() can be called as follows:

int result = sum(5,6);

The result of the function, which is the sum of a and b, in this case, is stored in variable result.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support