×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

The scope of function parameters in C programming language

In this tutorial, we are going to learn about the scope of function parameters in C programming language. Here, we will learn what the function parameters are and what are their scopes?
Submitted by IncludeHelp, on May 12, 2018

The list of the variables which are declared with function declaration/definition are known as function parameters.

Note: There is no need to declare variables as function parameters while declaring a function, function declaration also works with the types of function parameters.

For example,

If we want to declare a function that will be used to find sum of two integer numbers. In the case, function will accept two integer parameters and return the sum as an integer.

Declaration1:

 int sum(int, int );

Declaration2:

 int sum ( int a, int b );

Declaration1 has only types of the function parameters. While, declaration2 has its name also, in second case, while defining a function, variable names should be the same which we have declared during the declaring a function.


Scope of function parameters

Local variables have their scope to the function only in which they are declared, while global variables have scope to the program and they can be accessed by any function in the program.

Coming up to scope of the function parameters - “function parameters are local variables for that function only, we can say function parameter’s scopes are local to that function, in which they are declared.”

Look at following function:

    int findSum(int x, int y )
    {
	    int sum;
	    sum = x+y;
	    return sum;
    }

Here, x and y are the function parameters and sum is the local variable. All three variables have the same scope. But, if you will be thinking that x and y can also be declared inside the findSum() function – then, its totally wrong. Function parameters are used to make communication between calling and called function.

Functions are used to do some specific task and they need input. So, to provide input - we use function parameters. When we pass the values to the called function from calling function, the values will be copied to the function parameters and then function can operate on those values.

Consider the following function declaration and calling statements

#include <stdio.h>
int findSum(int x, int y)
{
	int sum;
	sum = x+y;
	return sum;
}

//main code
int main ()
{
	printf("SUM = %d\n", findSum(10,20));
	return 0;
}

Output

    SUM = 30

Here, int findSum(int x, int y) {...} is the function definition and findSum(10, 20) is the function calling, when function is called, actual values from main() function will be copied to x and y – which are the local variables for findSum() function.


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.