Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programming language

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.






Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.